Common.hs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. {-# LANGUAGE TemplateHaskell #-}
  2. module Internal.Syntax.Common ( tests ) where
  3. import Control.Applicative
  4. import Agda.Syntax.Common
  5. import Agda.Syntax.Position ( noRange )
  6. import Internal.Helpers
  7. ------------------------------------------------------------------------------
  8. -- Instances
  9. instance CoArbitrary Modality
  10. instance Arbitrary Modality where
  11. arbitrary = Modality <$> arbitrary <*> arbitrary <*> arbitrary
  12. instance Arbitrary a => Arbitrary (UnderAddition a) where
  13. arbitrary = UnderAddition <$> arbitrary
  14. instance Arbitrary a => Arbitrary (UnderComposition a) where
  15. arbitrary = UnderComposition <$> arbitrary
  16. instance CoArbitrary Q0Origin where
  17. coarbitrary = \case
  18. Q0Inferred -> variant 0
  19. Q0{} -> variant 1
  20. Q0Erased{} -> variant 2
  21. instance CoArbitrary Q1Origin where
  22. coarbitrary = \case
  23. Q1Inferred -> variant 0
  24. Q1{} -> variant 1
  25. Q1Linear{} -> variant 2
  26. instance CoArbitrary QωOrigin where
  27. coarbitrary = \case
  28. QωInferred -> variant 0
  29. Qω{} -> variant 1
  30. QωPlenty{} -> variant 2
  31. instance Arbitrary Q0Origin where
  32. arbitrary = elements [ Q0Inferred, Q0 noRange, Q0Erased noRange ]
  33. instance Arbitrary Q1Origin where
  34. arbitrary = elements [ Q1Inferred, Q1 noRange, Q1Linear noRange ]
  35. instance Arbitrary QωOrigin where
  36. arbitrary = elements [ QωInferred, Qω noRange, QωPlenty noRange ]
  37. instance CoArbitrary Quantity
  38. instance Arbitrary Quantity where
  39. arbitrary = elements [ Quantity0 mempty, {-Quantity1 mempty,-} Quantityω mempty ]
  40. -- Andreas, 2019-07-04, TODO:
  41. -- The monoid laws hold only modulo origin information.
  42. -- Thus, we generate here only origin-free quantities.
  43. -- arbitrary = oneof
  44. -- [ Quantity0 <$> arbitrary
  45. -- , Quantity1 <$> arbitrary
  46. -- , Quantityω <$> arbitrary
  47. -- ]
  48. instance CoArbitrary Relevance
  49. instance Arbitrary Relevance where
  50. arbitrary = elements allRelevances
  51. instance CoArbitrary Cohesion
  52. instance Arbitrary Cohesion where
  53. arbitrary = elements $ filter (/= Squash) allCohesions
  54. -- left division does not respect laws for Squash on the left.
  55. instance Arbitrary NameId where
  56. arbitrary = elements [ NameId x (ModuleNameHash y) | x <- [0, 1], y <- [0, 1] ]
  57. instance CoArbitrary ModuleNameHash where
  58. coarbitrary (ModuleNameHash h) = coarbitrary h
  59. instance CoArbitrary NameId
  60. instance Arbitrary Induction where
  61. arbitrary = elements [Inductive, CoInductive]
  62. instance CoArbitrary Induction where
  63. coarbitrary Inductive = variant 0
  64. coarbitrary CoInductive = variant 1
  65. instance Arbitrary MetaId where
  66. arbitrary = elements
  67. [ MetaId x (ModuleNameHash y)
  68. | x <- [0, 1]
  69. , y <- [0, 1]
  70. ]
  71. instance Arbitrary Hiding where
  72. arbitrary = elements [ Hidden, NotHidden, Instance NoOverlap, Instance YesOverlap ]
  73. instance (Arbitrary a, Arbitrary b) => Arbitrary (ImportedName' a b) where
  74. arbitrary = do
  75. a <- arbitrary
  76. b <- arbitrary
  77. elements [ ImportedModule a, ImportedName b ]
  78. deriving instance (Show a, Show b) => Show (Using' a b)
  79. instance (Arbitrary a, Arbitrary b) => Arbitrary (Using' a b) where
  80. arbitrary = do
  81. xs <- arbitrary
  82. elements [ UseEverything, Using xs ]
  83. ------------------------------------------------------------------------------
  84. -- Properties
  85. -- Quantity is a POMonoid
  86. prop_monoid_Quantity_comp :: Property3 (UnderComposition Quantity)
  87. prop_monoid_Quantity_comp = isMonoid
  88. prop_monotone_comp_Quantity_comp :: Property3 (UnderComposition Quantity)
  89. prop_monotone_comp_Quantity_comp = isMonotoneComposition
  90. prop_monoid_Quantity_add :: Property3 (UnderAddition Quantity)
  91. prop_monoid_Quantity_add = isMonoid
  92. prop_monotone_comp_Quantity_add :: Property3 (UnderAddition Quantity)
  93. prop_monotone_comp_Quantity_add = isMonotoneComposition
  94. -- -- | Quantities ω=ℕ, 1={1}, 0={0} do not form a Galois connection.
  95. --
  96. -- Counterexample 1: ω \ 1 ≤ 0 is true, but 1 ≤ ω · 0 is false
  97. -- since {1} and {0} are not related.
  98. --
  99. -- Counterexample 2: 0 \ 1 ≤ ω is true, but 1 ≤ 0 · ω is false
  100. --
  101. -- prop_Galois_Quantity :: Prop3 Quantity
  102. -- prop_Galois_Quantity = isGaloisConnection
  103. -- Relevance is a LeftClosedPOMonoid
  104. prop_monoid_Relevance_comp :: Property3 (UnderComposition Relevance)
  105. prop_monoid_Relevance_comp = isMonoid
  106. prop_monotone_comp_Relevance_comp :: Property3 (UnderComposition Relevance)
  107. prop_monotone_comp_Relevance_comp = isMonotoneComposition
  108. prop_Galois_Relevance_comp :: Prop3 (UnderComposition Relevance)
  109. prop_Galois_Relevance_comp = isGaloisConnection
  110. prop_left_identity_invcomp_Relevance :: Relevance -> Bool
  111. prop_left_identity_invcomp_Relevance x = Relevant `inverseComposeRelevance` x == x
  112. prop_right_absorptive_invcomp_Relevance :: Relevance -> Bool
  113. prop_right_absorptive_invcomp_Relevance x = x `inverseComposeRelevance` Relevant == Relevant
  114. prop_monoid_Relevance_add :: Property3 (UnderAddition Relevance)
  115. prop_monoid_Relevance_add = isMonoid
  116. prop_monotone_comp_Relevance_add :: Property3 (UnderAddition Relevance)
  117. prop_monotone_comp_Relevance_add = isMonotoneComposition
  118. -- Modality is a POMonoid
  119. prop_monoid_Modality_comp :: Property3 (UnderComposition Modality)
  120. prop_monoid_Modality_comp = isMonoid
  121. prop_monotone_comp_Modality :: Property3 (UnderComposition Modality)
  122. prop_monotone_comp_Modality = isMonotoneComposition
  123. prop_monoid_Modality_add :: Property3 (UnderAddition Modality)
  124. prop_monoid_Modality_add = isMonoid
  125. prop_monotone_comp_Modality_add :: Property3 (UnderAddition Modality)
  126. prop_monotone_comp_Modality_add = isMonotoneComposition
  127. -- -- | The following does not hold, see prop_Galois_Quantity.
  128. -- prop_Galois_Modality :: Prop3 Modality
  129. -- prop_Galois_Modality = isGaloisConnection
  130. -- ASR (2017-01-23): Commented out because 'Hiding' is *partial*
  131. -- monoid.
  132. -- -- | 'Hiding' is a monoid.
  133. -- prop_monoid_Hiding :: Prop3 Hiding
  134. -- prop_monoid_Hiding = isMonoid
  135. -- | 'Using'' is a monoid.
  136. prop_monoid_Using' :: Property3 (Using' Int Int)
  137. prop_monoid_Using' = isMonoid
  138. ------------------------------------------------------------------------
  139. -- * All tests
  140. ------------------------------------------------------------------------
  141. -- Template Haskell hack to make the following $allProperties work
  142. -- under ghc-7.8.
  143. return [] -- KEEP!
  144. -- | All tests as collected by 'allProperties'.
  145. --
  146. -- Using 'allProperties' is convenient and superior to the manual
  147. -- enumeration of tests, since the name of the property is added
  148. -- automatically.
  149. tests :: TestTree
  150. tests = testProperties "Internal.Syntax.Common" $allProperties