-- editorconfig-checker-disable
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ViewPatterns #-}

{-| Property tests for the `lessThanInteger` and `lessThanEqualsInteger` builtins
   and their interaction with the arithmetic functions. -}
module Evaluation.Builtins.Integer.OrderProperties
  ( test_integer_order_properties
  , integer_order_property_names
  , generate_integer_order_properties
  )
where

import Prelude hiding (and, not, or)

import Evaluation.Builtins.Common
import Evaluation.Builtins.Integer.Common
import Evaluation.Builtins.Integer.Conformance

import Evaluation.Builtins.Conformance (CheckMode, OutputFormat, PropertyFilter, PropertyId (..))

import Test.Cardano.Base.QuickCheck qualified as BaseQC
import Test.QuickCheck.Random (QCGen)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck

numberOfTests :: Int
numberOfTests :: Int
numberOfTests = Int
200

{- Tests for standard properties of order relations.  In most of these we create
   totally random inputs and then create terms checking that the expected
   properties are satisfied.  The inputs are completely random, so in some cases
   we'll be checking vacuous implications (for example, in `add_pairs`, where
   only one quarter of the cases will be checking the property that we really
   want to check).  Instead we could have used `suchThat` to generate inputs
   that always satisified the preconditions and then created terms to check that
   the conclusion holds.  It's arguable which of these is better, but the way
   it's done here exercises the comparison builtins more so perhaps increases
   the probability of detecting unexpected behaviour. -}

lte_reflexive :: BigInteger -> Check
lte_reflexive :: BigInteger -> Check
lte_reflexive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
a PlcTerm
a

lte_antisymmetric :: BigInteger -> BigInteger -> Check
lte_antisymmetric :: BigInteger -> BigInteger -> Check
lte_antisymmetric (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) (BigInteger -> PlcTerm
biginteger -> PlcTerm
b) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$
    (PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
a PlcTerm
b PlcTerm -> PlcTerm -> PlcTerm
`and` PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
b PlcTerm
a)
      PlcTerm -> PlcTerm -> PlcTerm
`implies` PlcTerm -> PlcTerm -> PlcTerm
equalsInteger PlcTerm
a PlcTerm
b

lte_transitive :: BigInteger -> BigInteger -> BigInteger -> Check
lte_transitive :: BigInteger -> BigInteger -> BigInteger -> Check
lte_transitive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) (BigInteger -> PlcTerm
biginteger -> PlcTerm
b) (BigInteger -> PlcTerm
biginteger -> PlcTerm
c) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$
    (PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
a PlcTerm
b PlcTerm -> PlcTerm -> PlcTerm
`and` PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
b PlcTerm
c)
      PlcTerm -> PlcTerm -> PlcTerm
`implies` PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
a PlcTerm
c

-- This implies that lessThanEqualsInteger is a total order.
trichotomy :: BigInteger -> BigInteger -> Check
trichotomy :: BigInteger -> BigInteger -> Check
trichotomy (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) (BigInteger -> PlcTerm
biginteger -> PlcTerm
b) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$
    PlcTerm -> PlcTerm -> PlcTerm
lessThanInteger PlcTerm
a PlcTerm
b PlcTerm -> PlcTerm -> PlcTerm
`xor` PlcTerm -> PlcTerm -> PlcTerm
equalsInteger PlcTerm
a PlcTerm
b PlcTerm -> PlcTerm -> PlcTerm
`xor` PlcTerm -> PlcTerm -> PlcTerm
lessThanInteger PlcTerm
b PlcTerm
a

-- This establishes a connection between < and <=, allowing us to limit
-- ourselves to checking properties of <=.
lt_versus_lte :: BigInteger -> BigInteger -> Check
lt_versus_lte :: BigInteger -> BigInteger -> Check
lt_versus_lte (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) (BigInteger -> PlcTerm
biginteger -> PlcTerm
b) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$
    PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
a PlcTerm
b PlcTerm -> PlcTerm -> PlcTerm
`iff` (PlcTerm -> PlcTerm -> PlcTerm
lessThanInteger PlcTerm
a PlcTerm
b PlcTerm -> PlcTerm -> PlcTerm
`xor` PlcTerm -> PlcTerm -> PlcTerm
equalsInteger PlcTerm
a PlcTerm
b)

-- Tests of some relations between the comparison operators and the arithmetic
-- operators.

-- Check that a <= b and c <= d => a+c <= b+d.  In conjunction with the ring
-- properties this implies, for example, that the sum of two positive integers
-- is positive and the sum of two negative integers is negative, and that a <=
-- a+k for positive k.
add_pairs :: BigInteger -> BigInteger -> BigInteger -> BigInteger -> Check
add_pairs :: BigInteger -> BigInteger -> BigInteger -> BigInteger -> Check
add_pairs (BigInteger -> PlcTerm
biginteger -> PlcTerm
a) (BigInteger -> PlcTerm
biginteger -> PlcTerm
b) (BigInteger -> PlcTerm
biginteger -> PlcTerm
c) (BigInteger -> PlcTerm
biginteger -> PlcTerm
d) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$
    (PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
a PlcTerm
b PlcTerm -> PlcTerm -> PlcTerm
`and` PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
c PlcTerm
d)
      PlcTerm -> PlcTerm -> PlcTerm
`implies` PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger (PlcTerm -> PlcTerm -> PlcTerm
addInteger PlcTerm
a PlcTerm
c) (PlcTerm -> PlcTerm -> PlcTerm
addInteger PlcTerm
b PlcTerm
d)

-- Test that the signs of various types of product are correct.
mul_pos_pos :: NonNegative BigInteger -> NonNegative BigInteger -> Check
mul_pos_pos :: NonNegative BigInteger -> NonNegative BigInteger -> Check
mul_pos_pos (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
zero (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger PlcTerm
a PlcTerm
b)

mul_pos_neg :: NonNegative BigInteger -> NonPositive BigInteger -> Check
mul_pos_neg :: NonNegative BigInteger -> NonPositive BigInteger -> Check
mul_pos_neg (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger PlcTerm
a PlcTerm
b) PlcTerm
zero

mul_neg_pos :: NonPositive BigInteger -> NonNegative BigInteger -> Check
mul_neg_pos :: NonPositive BigInteger -> NonNegative BigInteger -> Check
mul_neg_pos (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (NonNegative (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger PlcTerm
a PlcTerm
b) PlcTerm
zero

mul_neg_neg :: NonPositive BigInteger -> NonPositive BigInteger -> Check
mul_neg_neg :: NonPositive BigInteger -> NonPositive BigInteger -> Check
mul_neg_neg (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
a)) (NonPositive (BigInteger -> PlcTerm
biginteger -> PlcTerm
b)) =
  PlcTerm -> Check
CheckTrue (PlcTerm -> Check) -> PlcTerm -> Check
forall a b. (a -> b) -> a -> b
$ PlcTerm -> PlcTerm -> PlcTerm
lessThanEqualsInteger PlcTerm
zero (PlcTerm -> PlcTerm -> PlcTerm
multiplyInteger PlcTerm
a PlcTerm
b)

-- | Run the properties without writing files, retaining the usual test count.
test_integer_order_properties :: TestTree
test_integer_order_properties :: TestTree
test_integer_order_properties =
  TestName -> [TestTree] -> TestTree
testGroup TestName
"Property tests involving integer ordering" ([TestTree] -> TestTree) -> [TestTree] -> TestTree
forall a b. (a -> b) -> a -> b
$
    (Property -> Property) -> [PropertySpec (Gen Check)] -> [TestTree]
testPropertySpecs (Int -> Property -> Property
forall prop. Testable prop => Int -> prop -> Property
BaseQC.withNumTests Int
numberOfTests) [PropertySpec (Gen Check)]
integerOrderProperties

-- | Properties shared by the test suite and the conformance test generator.
integerOrderProperties :: [PropertySpec (Gen Check)]
integerOrderProperties :: [PropertySpec (Gen Check)]
integerOrderProperties =
  [ TestName
-> TestName -> (BigInteger -> Check) -> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"lte_reflexive" TestName
"lessThanEqualsInteger is reflexive" BigInteger -> Check
lte_reflexive
  , TestName
-> TestName
-> (BigInteger -> BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"lte_antisymmetric" TestName
"lessThanEqualsInteger is antisymmetric" BigInteger -> BigInteger -> Check
lte_antisymmetric
  , TestName
-> TestName
-> (BigInteger -> BigInteger -> BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"lte_transitive" TestName
"lessThanEqualsInteger is transitive" BigInteger -> BigInteger -> BigInteger -> Check
lte_transitive
  , TestName
-> TestName
-> (BigInteger -> BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"lt_versus_lte" TestName
"a <= b <=> a < b or a = b" BigInteger -> BigInteger -> Check
lt_versus_lte
  , TestName
-> TestName
-> (BigInteger -> BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"trichotomy" TestName
"a < b or a = b or b < a" BigInteger -> BigInteger -> Check
trichotomy
  , TestName
-> TestName
-> (BigInteger -> BigInteger -> BigInteger -> BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"add_pairs" TestName
"a <= b and c <= d => addInteger a c <= addInteger b d" BigInteger -> BigInteger -> BigInteger -> BigInteger -> Check
add_pairs
  , TestName
-> TestName
-> (NonNegative BigInteger -> NonNegative BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"mul_pos_pos" TestName
"multiplyInteger (>= 0) (>= 0) >= 0" NonNegative BigInteger -> NonNegative BigInteger -> Check
mul_pos_pos
  , TestName
-> TestName
-> (NonNegative BigInteger -> NonPositive BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"mul_pos_neg" TestName
"multiplyInteger (>= 0) (<= 0) <= 0" NonNegative BigInteger -> NonPositive BigInteger -> Check
mul_pos_neg
  , TestName
-> TestName
-> (NonPositive BigInteger -> NonNegative BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"mul_neg_pos" TestName
"multiplyInteger (<= 0) (>= 0) <= 0" NonPositive BigInteger -> NonNegative BigInteger -> Check
mul_neg_pos
  , TestName
-> TestName
-> (NonPositive BigInteger -> NonPositive BigInteger -> Check)
-> PropertySpec (Gen Check)
forall prop.
GenerateCases prop =>
TestName -> TestName -> prop -> PropertySpec (Gen Check)
propertySpec TestName
"mul_neg_neg" TestName
"multiplyInteger (<= 0) (<= 0) >= 0" NonPositive BigInteger -> NonPositive BigInteger -> Check
mul_neg_neg
  ]

generate_integer_order_properties
  :: OutputFormat -> CheckMode -> PropertyFilter -> FilePath -> Int -> QCGen -> IO [PropertyReport]
generate_integer_order_properties :: OutputFormat
-> CheckMode
-> PropertyFilter
-> TestName
-> Int
-> QCGen
-> IO [PropertyReport]
generate_integer_order_properties OutputFormat
format CheckMode
mode PropertyFilter
include TestName
outputDir Int
numFiles QCGen
seed =
  OutputFormat
-> CheckMode
-> PropertyFilter
-> TestName
-> Int
-> QCGen
-> [TestName]
-> [PropertySpec (Gen Check)]
-> IO [PropertyReport]
generatePropertySpecs
    OutputFormat
format
    CheckMode
mode
    PropertyFilter
include
    TestName
outputDir
    Int
numFiles
    QCGen
seed
    [TestName]
conformanceSuiteDirectory
    [PropertySpec (Gen Check)]
integerOrderProperties

-- | Qualified property names, without sampling inputs or evaluating checks.
integer_order_property_names :: [PropertyId]
integer_order_property_names :: [PropertyId]
integer_order_property_names =
  [TestName] -> [PropertySpec (Gen Check)] -> [PropertyId]
forall gen. [TestName] -> [PropertySpec gen] -> [PropertyId]
propertySpecPropertyIds [TestName]
conformanceSuiteDirectory [PropertySpec (Gen Check)]
integerOrderProperties

conformanceSuiteDirectory :: [String]
conformanceSuiteDirectory :: [TestName]
conformanceSuiteDirectory = [TestName
"integer", TestName
"OrderProperties"]