-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathAlgebra.hs
3192 lines (2565 loc) · 95.6 KB
/
Algebra.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE CPP,MagicHash,UnboxedTuples #-}
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
{-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
-- | This module defines the algebraic type-classes used in subhask.
-- The class hierarchies are significantly more general than those in the standard Prelude.
module SubHask.Algebra
(
-- * Comparisons
Logic
, ValidLogic
, ClassicalLogic
, Eq_ (..)
, Eq
, ValidEq
, law_Eq_reflexive
, law_Eq_symmetric
, law_Eq_transitive
, defn_Eq_noteq
, POrd_ (..)
, POrd
, law_POrd_commutative
, law_POrd_associative
, theorem_POrd_idempotent
, Lattice_ (..)
, Lattice
, isChain
, isAntichain
, POrdering (..)
, law_Lattice_commutative
, law_Lattice_associative
, theorem_Lattice_idempotent
, law_Lattice_infabsorption
, law_Lattice_supabsorption
, law_Lattice_reflexivity
, law_Lattice_antisymmetry
, law_Lattice_transitivity
, defn_Lattice_greaterthan
, MinBound_ (..)
, MinBound
, law_MinBound_inf
, Bounded (..)
, law_Bounded_sup
, supremum
, supremum_
, infimum
, infimum_
, Complemented (..)
, law_Complemented_not
, Heyting (..)
, modusPonens
, law_Heyting_maxbound
, law_Heyting_infleft
, law_Heyting_infright
, law_Heyting_distributive
, Boolean
, law_Boolean_infcomplement
, law_Boolean_supcomplement
, law_Boolean_infdistributivity
, law_Boolean_supdistributivity
, Ord_ (..)
, law_Ord_totality
, law_Ord_min
, law_Ord_max
, Ord
, Ordering (..)
, min
, max
, maximum
, maximum_
, minimum
, minimum_
, argmin
, argmax
, Graded (..)
, law_Graded_fromEnum
, law_Graded_pred
, defn_Graded_predN
, (>.)
, (<.)
, Enum (..)
, law_Enum_toEnum
, law_Enum_succ
, defn_Enum_succN
-- ** Boolean helpers
, (||)
, (&&)
, true
, false
, and
, or
-- * Set-like
, Elem
, infDisjoint
, SetElem
, Container (..)
, law_Container_preservation
, Constructible (..)
, Constructible0
, law_Constructible_singleton
, defn_Constructible_cons
, defn_Constructible_snoc
, defn_Constructible_fromList
, defn_Constructible_fromListN
, theorem_Constructible_cons
, fromString
, fromList
, fromListN
, generate
, insert
, empty
, isEmpty
, Foldable (..)
, law_Foldable_sum
, theorem_Foldable_tofrom
, defn_Foldable_foldr
, defn_Foldable_foldr'
, defn_Foldable_foldl
, defn_Foldable_foldl'
, defn_Foldable_foldr1
, defn_Foldable_foldr1'
, defn_Foldable_foldl1
, defn_Foldable_foldl1'
, foldtree1
, convertUnfoldable
, length
, reduce
, concat
, headMaybe
, tailMaybe
, lastMaybe
, initMaybe
-- *** indexed containers
, Index
, SetIndex
, IxContainer (..)
, law_IxContainer_preservation
, defn_IxContainer_bang
, defn_IxContainer_findWithDefault
, defn_IxContainer_hasIndex
, (!?)
, Sliceable (..)
, law_Sliceable_restorable
, law_Sliceable_preservation
, IxConstructible (..)
, law_IxConstructible_lookup
, defn_IxConstructible_consAt
, defn_IxConstructible_snocAt
, defn_IxConstructible_fromIxList
, theorem_IxConstructible_preservation
, insertAt
-- * Types
, CanError (..)
, Maybe' (..)
, justs'
, Labeled' (..)
-- * Number-like
-- ** Classes with one operator
, Semigroup (..)
, law_Semigroup_associativity
, defn_Semigroup_plusequal
, associator
, cycle
, Actor
, Action (..)
, law_Action_compatibility
, defn_Action_dotplusequal
, (+.)
, Cancellative (..)
, law_Cancellative_rightminus1
, law_Cancellative_rightminus2
, defn_Cancellative_plusequal
, Monoid (..)
, isZero
, notZero
, law_Monoid_leftid
, law_Monoid_rightid
, defn_Monoid_isZero
, Abelian
, law_Abelian_commutative
, Group (..)
, law_Group_leftinverse
, law_Group_rightinverse
, defn_Group_negateminus
-- ** Classes with two operators
, Rg(..)
, law_Rg_multiplicativeAssociativity
, law_Rg_multiplicativeCommutivity
, law_Rg_annihilation
, law_Rg_distributivityLeft
, theorem_Rg_distributivityRight
, defn_Rg_timesequal
, Rig(..)
, isOne
, notOne
, law_Rig_multiplicativeId
, Rng
, defn_Ring_fromInteger
, Ring(..)
, indicator
, Integral(..)
, law_Integral_divMod
, law_Integral_quotRem
, law_Integral_toFromInverse
, roundUpToNearest
-- , roundUpToNearestBase2
, fromIntegral
, Field(..)
, OrdField
, RationalField(..)
, convertRationalField
, toFloat
, toDouble
, BoundedField(..)
, infinity
, negInfinity
, ExpRing (..)
, (^)
, ExpField (..)
, Real (..)
, QuotientField(..)
-- ** Sizes
, Normed (..)
, abs
, Metric (..)
, isFartherThan
, lb2distanceUB
, law_Metric_nonnegativity
, law_Metric_indiscernables
, law_Metric_symmetry
, law_Metric_triangle
-- ** Linear algebra
, Scalar
, IsScalar
, HasScalar
, type (><)
, Cone (..)
, Module (..)
, law_Module_multiplication
, law_Module_addition
, law_Module_action
, law_Module_unital
, defn_Module_dotstarequal
, (*.)
, FreeModule (..)
, law_FreeModule_commutative
, law_FreeModule_associative
, law_FreeModule_id
, defn_FreeModule_dotstardotequal
, FiniteModule (..)
, VectorSpace (..)
, Reisz (..)
, Banach (..)
, law_Banach_distance
, law_Banach_size
, Hilbert (..)
, squaredInnerProductNorm
, innerProductDistance
, innerProductNorm
, TensorAlgebra (..)
-- * Spatial programming
, Any (..)
, All
-- * Helper functions
, simpleMutableDefn
, module SubHask.Mutable
)
where
import qualified Prelude as P
import qualified Data.Number.Erf as P
import qualified Math.Gamma as P
import qualified Data.List as L
import Control.Monad hiding (liftM)
import Control.Monad.ST
import Data.Ratio
import Data.Typeable
import Test.QuickCheck (frequency)
import Control.Parallel.Strategies
import GHC.Prim hiding (Any)
import GHC.Types
import SubHask.Internal.Prelude
import SubHask.Category
import SubHask.Mutable
-------------------------------------------------------------------------------
-- Helper functions
-- | Creates a quickcheck property for a simple mutable operator defined using "immutable2mutable"
simpleMutableDefn :: (Eq_ a, IsMutable a)
=> (Mutable (ST s) a -> b -> ST s ()) -- ^ mutable function
-> (a -> b -> a) -- ^ create a mutable function using "immutable2mutable"
-> (a -> b -> Logic a) -- ^ the output property
simpleMutableDefn mf f a b = unsafeRunMutableProperty $ do
ma1 <- thaw a
ma2 <- thaw a
mf ma1 b
immutable2mutable f ma2 b
a1 <- freeze ma1
a2 <- freeze ma2
return $ a1==a2
-------------------------------------------------------------------------------
-- relational classes
-- | Every type has an associated logic.
-- Most types use classical logic, which corresponds to the Bool type.
-- But types can use any logical system they want.
-- Functions, for example, use an infinite logic.
-- You probably want your logic to be an instance of "Boolean", but this is not required.
--
-- See wikipedia's articles on <https://en.wikipedia.org/wiki/Algebraic_logic algebraic logic>,
-- and <https://en.wikipedia.org/wiki/Infinitary_logic infinitary logic> for more details.
type family Logic a :: *
type instance Logic Bool = Bool
type instance Logic Char = Bool
type instance Logic Int = Bool
type instance Logic Integer = Bool
type instance Logic Rational = Bool
type instance Logic Float = Bool
type instance Logic Double = Bool
type instance Logic (a->b) = a -> Logic b
type instance Logic () = ()
-- FIXME:
-- This type is only needed to due an apparent ghc bug.
-- See [#10592](https://ghc.haskell.org/trac/ghc/ticket/10592).
-- But there seems to be a workaround now.
type ValidLogic a = Complemented (Logic a)
-- | Classical logic is implemented using the Prelude's Bool type.
type ClassicalLogic a = Logic a ~ Bool
-- | Defines equivalence classes over the type.
-- The values need not have identical representations in the machine to be equal.
--
-- See <https://en.wikipedia.org/wiki/Equivalence_class wikipedia>
-- and <http://ncatlab.org/nlab/show/equivalence+class ncatlab> for more details.
class Eq_ a where
infix 4 ==
(==) :: a -> a -> Logic a
-- | In order to have the "not equals to" relation, your logic must have a notion of "not", and therefore must be "Boolean".
{-# INLINE (/=) #-}
infix 4 /=
(/=) :: ValidLogic a => a -> a -> Logic a
(/=) = not (==)
law_Eq_reflexive :: Eq a => a -> Logic a
law_Eq_reflexive a = a==a
law_Eq_symmetric :: Eq a => a -> a -> Logic a
law_Eq_symmetric a1 a2 = (a1==a2)==(a2==a1)
law_Eq_transitive :: Eq a => a -> a -> a -> Logic a
law_Eq_transitive a1 a2 a3 = (a1==a2&&a2==a3) ==> (a1==a3)
defn_Eq_noteq :: (Complemented (Logic a), Eq a) => a -> a -> Logic a
defn_Eq_noteq a1 a2 = (a1/=a2) == (not $ a1==a2)
instance Eq_ () where
{-# INLINE (==) #-}
() == () = ()
{-# INLINE (/=) #-}
() /= () = ()
instance Eq_ Bool where (==) = (P.==); (/=) = (P./=); {-# INLINE (==) #-}; {-# INLINE (/=) #-}
instance Eq_ Char where (==) = (P.==); (/=) = (P./=); {-# INLINE (==) #-}; {-# INLINE (/=) #-}
instance Eq_ Int where (==) = (P.==); (/=) = (P./=); {-# INLINE (==) #-}; {-# INLINE (/=) #-}
instance Eq_ Integer where (==) = (P.==); (/=) = (P./=); {-# INLINE (==) #-}; {-# INLINE (/=) #-}
instance Eq_ Rational where (==) = (P.==); (/=) = (P./=); {-# INLINE (==) #-}; {-# INLINE (/=) #-}
instance Eq_ Float where (==) = (P.==); (/=) = (P./=); {-# INLINE (==) #-}; {-# INLINE (/=) #-}
instance Eq_ Double where (==) = (P.==); (/=) = (P./=); {-# INLINE (==) #-}; {-# INLINE (/=) #-}
instance Eq_ b => Eq_ (a -> b) where
{-# INLINE (==) #-}
(f==g) a = f a == g a
type Eq a = (Eq_ a, Logic a~Bool)
type ValidEq a = (Eq_ a, ValidLogic a)
-- class (Eq_ a, Logic a ~ Bool) => Eq a
-- instance (Eq_ a, Logic a ~ Bool) => Eq a
--
-- class (Eq_ a, ValidLogic a) => ValidEq a
-- instance (Eq_ a, ValidLogic a) => ValidEq a
--------------------
-- | This is more commonly known as a "meet" semilattice
class Eq_ b => POrd_ b where
inf :: b -> b -> b
{-# INLINE (<=) #-}
infix 4 <=
(<=) :: b -> b -> Logic b
b1 <= b2 = inf b1 b2 == b1
{-# INLINE (<) #-}
infix 4 <
(<) :: Complemented (Logic b) => b -> b -> Logic b
b1 < b2 = inf b1 b2 == b1 && b1 /= b2
type POrd a = (Eq a, POrd_ a)
-- class (Eq b, POrd_ b) => POrd b
-- instance (Eq b, POrd_ b) => POrd b
law_POrd_commutative :: (Eq b, POrd_ b) => b -> b -> Bool
law_POrd_commutative b1 b2 = inf b1 b2 == inf b2 b1
law_POrd_associative :: (Eq b, POrd_ b) => b -> b -> b -> Bool
law_POrd_associative b1 b2 b3 = inf (inf b1 b2) b3 == inf b1 (inf b2 b3)
theorem_POrd_idempotent :: (Eq b, POrd_ b) => b -> Bool
theorem_POrd_idempotent b = inf b b == b
#define mkPOrd_(x) \
instance POrd_ x where \
inf = (P.min) ;\
(<=) = (P.<=) ;\
(<) = (P.<) ;\
{-# INLINE inf #-} ;\
{-# INLINE (<=) #-} ;\
{-# INLINE (<) #-}
mkPOrd_(Bool)
mkPOrd_(Char)
mkPOrd_(Int)
mkPOrd_(Integer)
mkPOrd_(Float)
mkPOrd_(Double)
mkPOrd_(Rational)
instance POrd_ () where
{-# INLINE inf #-}
inf () () = ()
instance POrd_ b => POrd_ (a -> b) where
{-# INLINE inf #-}
inf f g = \x -> inf (f x) (g x)
{-# INLINE (<) #-}
(f<=g) a = f a <= g a
-------------------
-- | Most Lattice literature only considers 'Bounded' lattices, but here we have both upper and lower bounded lattices.
--
-- prop> minBound <= b || not (minBound > b)
--
class POrd_ b => MinBound_ b where
minBound :: b
type MinBound a = (Eq a, MinBound_ a)
-- class (Eq b, MinBound_ b) => MinBound b
-- instance (Eq b, MinBound_ b) => MinBound b
law_MinBound_inf :: (Eq b, MinBound_ b) => b -> Bool
law_MinBound_inf b = inf b minBound == minBound
-- | "false" is an upper bound because `a && false = false` for all a.
{-# INLINE false #-}
false :: MinBound_ b => b
false = minBound
instance MinBound_ () where minBound = () ; {-# INLINE minBound #-}
instance MinBound_ Bool where minBound = False ; {-# INLINE minBound #-}
instance MinBound_ Char where minBound = P.minBound ; {-# INLINE minBound #-}
instance MinBound_ Int where minBound = P.minBound ; {-# INLINE minBound #-}
instance MinBound_ Float where minBound = -1/0 ; {-# INLINE minBound #-}
instance MinBound_ Double where minBound = -1/0 ; {-# INLINE minBound #-}
-- FIXME: should be a primop for this
instance MinBound_ b => MinBound_ (a -> b) where minBound = \_ -> minBound ; {-# INLINE minBound #-}
-------------------
-- | Represents all the possible ordering relations in a classical logic (i.e. Logic a ~ Bool)
data POrdering
= PLT
| PGT
| PEQ
| PNA
deriving (Read,Show)
type instance Logic POrdering = Bool
instance Arbitrary POrdering where
arbitrary = frequency
[ (1, P.return PLT)
, (1, P.return PGT)
, (1, P.return PEQ)
, (1, P.return PNA)
]
instance Eq_ POrdering where
{-# INLINE (==) #-}
PLT == PLT = True
PGT == PGT = True
PEQ == PEQ = True
PNA == PNA = True
_ == _ = False
-- | FIXME: there are many semigroups over POrdering;
-- how should we represent the others? newtypes?
instance Semigroup POrdering where
{-# INLINE (+) #-}
PEQ + x = x
PLT + _ = PLT
PGT + _ = PGT
PNA + _ = PNA
type instance Logic Ordering = Bool
instance Eq_ Ordering where
{-# INLINE (==) #-}
EQ == EQ = True
LT == LT = True
GT == GT = True
_ == _ = False
instance Semigroup Ordering where
{-# INLINE (+) #-}
EQ + x = x
LT + _ = LT
GT + _ = GT
instance Monoid POrdering where
{-# INLINE zero #-}
zero = PEQ
instance Monoid Ordering where
{-# INLINE zero #-}
zero = EQ
-- |
--
--
-- See <https://en.wikipedia.org/wiki/Lattice_%28order%29 wikipedia> for more details.
class POrd_ b => Lattice_ b where
sup :: b -> b -> b
{-# INLINE (>=) #-}
infix 4 >=
(>=) :: b -> b -> Logic b
b1 >= b2 = sup b1 b2 == b1
{-# INLINE (>) #-}
infix 4 >
(>) :: Boolean (Logic b) => b -> b -> Logic b
b1 > b2 = sup b1 b2 == b1 && b1 /= b2
-- | This function does not make sense on non-classical logics
--
-- FIXME: there are probably related functions for all these other logics;
-- is there a nice way to represent them all?
{-# INLINABLE pcompare #-}
pcompare :: Logic b ~ Bool => b -> b -> POrdering
pcompare a b = if a==b
then PEQ
else if a < b
then PLT
else if a > b
then PGT
else PNA
type Lattice a = (Eq a, Lattice_ a)
-- class (Eq b, Lattice_ b) => Lattice b
-- instance (Eq b, Lattice_ b) => Lattice b
law_Lattice_commutative :: (Eq b, Lattice_ b) => b -> b -> Bool
law_Lattice_commutative b1 b2 = sup b1 b2 == sup b2 b1
law_Lattice_associative :: (Eq b, Lattice_ b) => b -> b -> b -> Bool
law_Lattice_associative b1 b2 b3 = sup (sup b1 b2) b3 == sup b1 (sup b2 b3)
theorem_Lattice_idempotent :: (Eq b, Lattice_ b) => b -> Bool
theorem_Lattice_idempotent b = sup b b == b
law_Lattice_infabsorption :: (Eq b, Lattice b) => b -> b -> Bool
law_Lattice_infabsorption b1 b2 = inf b1 (sup b1 b2) == b1
law_Lattice_supabsorption :: (Eq b, Lattice b) => b -> b -> Bool
law_Lattice_supabsorption b1 b2 = sup b1 (inf b1 b2) == b1
law_Lattice_reflexivity :: Lattice a => a -> Logic a
law_Lattice_reflexivity a = a<=a
law_Lattice_antisymmetry :: Lattice a => a -> a -> Logic a
law_Lattice_antisymmetry a1 a2
| a1 <= a2 && a2 <= a1 = a1 == a2
| otherwise = true
law_Lattice_transitivity :: Lattice a => a -> a -> a -> Logic a
law_Lattice_transitivity a1 a2 a3
| a1 <= a2 && a2 <= a3 = a1 <= a3
| a1 <= a3 && a3 <= a2 = a1 <= a2
| a2 <= a1 && a1 <= a3 = a2 <= a3
| a2 <= a3 && a3 <= a1 = a2 <= a1
| a3 <= a2 && a2 <= a1 = a3 <= a1
| a3 <= a1 && a1 <= a2 = a3 <= a2
| otherwise = true
defn_Lattice_greaterthan :: Lattice a => a -> a -> Logic a
defn_Lattice_greaterthan a1 a2
| a1 < a2 = a2 >= a1
| a1 > a2 = a2 <= a1
| otherwise = true
#define mkLattice_(x)\
instance Lattice_ x where \
sup = (P.max) ;\
(>=) = (P.>=) ;\
(>) = (P.>) ;\
{-# INLINE sup #-} ;\
{-# INLINE (>=) #-} ;\
{-# INLINE (>) #-}
mkLattice_(Bool)
mkLattice_(Char)
mkLattice_(Int)
mkLattice_(Integer)
mkLattice_(Float)
mkLattice_(Double)
mkLattice_(Rational)
instance Lattice_ () where
{-# INLINE sup #-}
sup () () = ()
instance Lattice_ b => Lattice_ (a -> b) where
{-# INLINE sup #-}
sup f g = \x -> sup (f x) (g x)
{-# INLINE (>=) #-}
(f>=g) a = f a >= g a
{-# INLINE (&&) #-}
infixr 3 &&
(&&) :: Lattice_ b => b -> b -> b
(&&) = inf
{-# INLINE (||) #-}
infixr 2 ||
(||) :: Lattice_ b => b -> b -> b
(||) = sup
-- | A chain is a collection of elements all of which can be compared
{-# INLINABLE isChain #-}
isChain :: Lattice a => [a] -> Logic a
isChain [] = true
isChain (x:xs) = all (/=PNA) (map (pcompare x) xs) && isChain xs
-- | An antichain is a collection of elements none of which can be compared
--
-- See <http://en.wikipedia.org/wiki/Antichain wikipedia> for more details.
--
-- See also the article on <http://en.wikipedia.org/wiki/Dilworth%27s_theorem Dilward's Theorem>.
{-# INLINABLE isAntichain #-}
isAntichain :: Lattice a => [a] -> Logic a
isAntichain [] = true
isAntichain (x:xs) = all (==PNA) (map (pcompare x) xs) && isAntichain xs
-------------------
-- | An element of a graded lattice has a unique predecessor.
--
-- See <https://en.wikipedia.org/wiki/Graded_poset wikipedia> for more details.
class Lattice b => Graded b where
-- | Algebrists typically call this function the "rank" of the element in the poset;
-- however we use the name from the standard prelude instead
fromEnum :: b -> Int
-- | The predecessor in the ordering
pred :: b -> b
-- | Repeatedly apply the "pred" function
predN :: Int -> b -> b
predN i b
| i < 0 = error $ "predN called on negative number "++show i
| i == 0 = b
| i > 0 = predN (i-1) $ pred b
law_Graded_fromEnum :: (Lattice b, Graded b) => b -> b -> Bool
law_Graded_fromEnum b1 b2
| b1 < b2 = fromEnum b1 < fromEnum b2
| b1 > b2 = fromEnum b1 > fromEnum b2
| b1 == b2 = fromEnum b1 == fromEnum b2
| otherwise = True
law_Graded_pred :: Graded b => b -> b -> Bool
law_Graded_pred b1 _ = fromEnum (pred b1) == fromEnum b1-1
|| fromEnum (pred b1) == fromEnum b1
defn_Graded_predN :: Graded b => Int -> b -> Bool
defn_Graded_predN i b
| i < 0 = true
| otherwise = go i b == predN i b
where
go 0 b' = b'
go i' b' = go (i'-1) $ pred b'
instance Graded Bool where
{-# INLINE pred #-}
pred True = False
pred False = False
{-# INLINE fromEnum #-}
fromEnum True = 1
fromEnum False = 0
instance Graded Int where
{-# INLINE pred #-}
pred i = if i == minBound
then i
else i-1
{-# INLINE predN #-}
predN n i = if i-n <= i
then i-n
else minBound
{-# INLINE fromEnum #-}
fromEnum = id
instance Graded Char where
{-# INLINE pred #-}
pred c = if c=='\NUL'
then '\NUL'
else P.pred c
{-# INLINE fromEnum #-}
fromEnum = P.fromEnum
instance Graded Integer where
{-# INLINE pred #-}
pred = P.pred
{-# INLINE predN #-}
predN n i = i - toInteger n
{-# INLINE fromEnum #-}
fromEnum = P.fromEnum
{-# INLINE (<.) #-}
(<.) :: (Lattice b, Graded b) => b -> b -> Bool
b1 <. b2 = b1 == pred b2
-- | In a well founded ordering, every element (except possibly the "maxBound" if it exists) has a successor element.
-- We use the "Enum" to represent well founded orderings to maintain consistency with the standard Prelude.
--
-- See <http://ncatlab.org/nlab/show/well-founded+relation ncatlab> for more info.
class (Graded b, Ord_ b) => Enum b where
-- | The next element in the ordering
succ :: b -> b
-- | Advance many elements into the ordering.
-- This value may be negative to move backwards.
succN :: Int -> b -> b
succN i b = toEnum $ fromEnum b + i
-- | Given an index (also called a rank) of an element, return the element
toEnum :: Int -> b
law_Enum_toEnum :: Enum b => b -> Bool
law_Enum_toEnum b = toEnum (fromEnum b) == b
law_Enum_succ :: Enum b => b -> b -> Bool
law_Enum_succ b1 _ = fromEnum (succ b1) == fromEnum b1+1
|| fromEnum (succ b1) == fromEnum b1
defn_Enum_succN :: Enum b => Int -> b -> Logic b
defn_Enum_succN i b = succN i b == toEnum (fromEnum b + i)
instance Enum Bool where
{-# INLINE succ #-}
succ True = True
succ False = True
{-# INLINE toEnum #-}
toEnum i
| i > 0 = True
| otherwise = False
instance Enum Int where
{-# INLINE succ #-}
succ i = if i == maxBound
then i
else i+1
{-# INLINE toEnum #-}
toEnum = id
instance Enum Char where
{-# INLINE succ #-}
succ = P.succ
{-# INLINE toEnum #-}
toEnum i = if i < 0
then P.toEnum 0
else P.toEnum i
instance Enum Integer where
{-# INLINE succ #-}
succ = P.succ
{-# INLINE toEnum #-}
toEnum = P.toEnum
{-# INLINE (>.) #-}
(>.) :: (Lattice b, Enum b) => b -> b -> Bool
b1 >. b2 = b1 == succ b2
---------------------------------------
-- | This is the class of total orderings.
--
-- See https://en.wikipedia.org/wiki/Total_order
class Lattice_ a => Ord_ a where
compare :: (Logic a~Bool, Ord_ a) => a -> a -> Ordering
compare a1 a2 = case pcompare a1 a2 of
PLT -> LT
PGT -> GT
PEQ -> EQ
PNA -> error "PNA given by pcompare on a totally ordered type"
law_Ord_totality :: Ord a => a -> a -> Bool
law_Ord_totality a1 a2 = a1 <= a2 || a2 <= a1
law_Ord_min :: Ord a => a -> a -> Bool
law_Ord_min a1 a2 = min a1 a2 == a1
|| min a1 a2 == a2
law_Ord_max :: Ord a => a -> a -> Bool
law_Ord_max a1 a2 = max a1 a2 == a1
|| max a1 a2 == a2
{-# INLINE min #-}
min :: Ord_ a => a -> a -> a
min = inf
{-# INLINE max #-}
max :: Ord_ a => a -> a -> a
max = sup
type Ord a = (Eq a, Ord_ a)
instance Ord_ ()
instance Ord_ Char where compare = P.compare ; {-# INLINE compare #-}
instance Ord_ Int where compare = P.compare ; {-# INLINE compare #-}
instance Ord_ Integer where compare = P.compare ; {-# INLINE compare #-}
instance Ord_ Float where compare = P.compare ; {-# INLINE compare #-}
instance Ord_ Double where compare = P.compare ; {-# INLINE compare #-}
instance Ord_ Rational where compare = P.compare ; {-# INLINE compare #-}
instance Ord_ Bool where compare = P.compare ; {-# INLINE compare #-}
-------------------
-- | A Bounded lattice is a lattice with both a minimum and maximum element
--
class (Lattice_ b, MinBound_ b) => Bounded b where
maxBound :: b
law_Bounded_sup :: (Eq b, Bounded b) => b -> Bool
law_Bounded_sup b = sup b maxBound == maxBound
-- | "true" is an lower bound because `a && true = true` for all a.
{-# INLINE true #-}
true :: Bounded b => b
true = maxBound
instance Bounded () where maxBound = () ; {-# INLINE maxBound #-}
instance Bounded Bool where maxBound = True ; {-# INLINE maxBound #-}
instance Bounded Char where maxBound = P.maxBound ; {-# INLINE maxBound #-}
instance Bounded Int where maxBound = P.maxBound ; {-# INLINE maxBound #-}
instance Bounded Float where maxBound = 1/0 ; {-# INLINE maxBound #-}
instance Bounded Double where maxBound = 1/0 ; {-# INLINE maxBound #-}
-- FIXME: should be a primop for infinity
instance Bounded b => Bounded (a -> b) where
{-# INLINE maxBound #-}
maxBound = \_ -> maxBound
--------------------
class Bounded b => Complemented b where
not :: b -> b
law_Complemented_not :: (ValidLogic b, Complemented b) => b -> Logic b
law_Complemented_not b = not (true `asTypeOf` b) == false
&& not (false `asTypeOf` b) == true
instance Complemented () where
{-# INLINE not #-}
not () = ()
instance Complemented Bool where
{-# INLINE not #-}
not = P.not
instance Complemented b => Complemented (a -> b) where
{-# INLINE not #-}
not f = \x -> not $ f x
-- | Heyting algebras are lattices that support implication, but not necessarily the law of excluded middle.
--
-- ==== Laws
-- There is a single, simple law that Heyting algebras must satisfy:
--
-- prop> a ==> b = c ===> a && c < b
--
-- ==== Theorems
-- From the laws, we automatically get the properties of:
--
-- distributivity
--
-- See <https://en.wikipedia.org/wiki/Heyting_algebra wikipedia> for more details.
--
-- Note that while Heyting algebras are abelian semigroups with respect to &&, they are not in general cancellative.
class Bounded b => Heyting b where
-- | FIXME: think carefully about infix
infixl 3 ==>
(==>) :: b -> b -> b
law_Heyting_maxbound :: (Eq b, Heyting b) => b -> Bool
law_Heyting_maxbound b = (b ==> b) == maxBound
law_Heyting_infleft :: (Eq b, Heyting b) => b -> b -> Bool
law_Heyting_infleft b1 b2 = (b1 && (b1 ==> b2)) == (b1 && b2)
law_Heyting_infright :: (Eq b, Heyting b) => b -> b -> Bool
law_Heyting_infright b1 b2 = (b2 && (b1 ==> b2)) == b2
law_Heyting_distributive :: (Eq b, Heyting b) => b -> b -> b -> Bool
law_Heyting_distributive b1 b2 b3 = (b1 ==> (b2 && b3)) == ((b1 ==> b2) && (b1 ==> b3))
-- | FIXME: add the axioms for intuitionist logic, which are theorems based on these laws
--
-- | Modus ponens gives us a default definition for "==>" in a "Boolean" algebra.
-- This formula is guaranteed to not work in a "Heyting" algebra that is not "Boolean".
--
-- See <https://en.wikipedia.org/wiki/Modus_ponens wikipedia> for more details.
modusPonens :: Boolean b => b -> b -> b
modusPonens b1 b2 = not b1 || b2
instance Heyting () where
{-# INLINE (==>) #-}
() ==> () = ()
instance Heyting Bool where
{-# INLINE (==>) #-}
(==>) = modusPonens
instance Heyting b => Heyting (a -> b) where
{-# INLINE (==>) #-}
(f==>g) a = f a ==> g a
-- | Generalizes Boolean variables.
--
-- See <https://en.wikipedia.org/wiki/Boolean_algebra_%28structure%29 wikipedia> for more details.
class (Complemented b, Heyting b) => Boolean b where
law_Boolean_infcomplement :: (Eq b, Boolean b) => b -> Bool
law_Boolean_infcomplement b = (b || not b) == true
law_Boolean_supcomplement :: (Eq b, Boolean b) => b -> Bool
law_Boolean_supcomplement b = (b && not b) == false
law_Boolean_infdistributivity :: (Eq b, Boolean b) => b -> b -> b -> Bool
law_Boolean_infdistributivity b1 b2 b3 = (b1 || (b2 && b3)) == ((b1 || b2) && (b1 || b3))
law_Boolean_supdistributivity :: (Eq b, Boolean b) => b -> b -> b -> Bool
law_Boolean_supdistributivity b1 b2 b3 = (b1 && (b2 || b3)) == ((b1 && b2) || (b1 && b3))