-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stdm.lhs
1710 lines (1432 loc) · 65.2 KB
/
Stdm.lhs
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
-------------------------------------------------------------------------------
Software Tools for Discrete Mathematics
Cordelia Hall and John O'Donnell
-------------------------------------------------------------------------------
Last modified: June 2006
This software is intended to be used with the book "Discrete
Mathematics Using a Computer, Second Edition", by John O'Donnell,
Cordelia Hall, and Rex Page. Published by Springer, 2006.
The software is written in Haskell 98, the standard functional
programming language. It runs interactively under the Hugs 98 and the
ghci implementation. Under Unix, you can start hugs with the command
hugs -98
which tells it to use the standard Haskell 98 language, but not the
experimental language extensions. Alternatively, you can use ghci.
Notice: the option -c may need to be modified so as this file be loaded. -- *2
If there is problem when loading this file, use the command: -- *2
:set "-c50" -- *2
This software, and its documentation, can be downloaded from the book
web page:
www.dcs.gla.ac.uk/~jtod/discrete-mathematics/
It's a good idea to check the web page periodically to keep the
software up to date.
The program is a "literate script". Each line that begins with "> "
is Haskell source code that can be executed; all other lines are
documentation which will be ignored by the compiler.
> module Stdm where
> -- REF: https://stackoverflow.com/questions/42609508/not-in-scope-catch/70728491#70728491
> import System.IO.Error (catchIOError)
-------------------------------------------------------------------------------
Operator Precedence
-------------------------------------------------------------------------------
> infix 1 <=>
> infix 2 ==>
> infix 3 \/
> infix 4 /\
> infix 3 +++
> infix 4 ~~~
> infix 4 ***
> infix 5 !!!
-------------------------------------------------------------------------------
Chapter 2. Propositional Logic
-------------------------------------------------------------------------------
> (<=>) :: Bool -> Bool -> Bool
> (<=>) a b = a == b
> (==>) :: Bool -> Bool -> Bool
> (==>) True False = False
> (==>) a b = True
> (\/) = (||)
> (/\) = (&&)
-------------------------------------------------------------------------------
A Mini-tutorial on Using the Proof Checker for Propositional Logic
-------------------------------------------------------------------------------
And Introduction
~~~~~~~~~~~~~~~~
Here is a theorem and a valid proof using the And Introduction rule:
Theorem AI1. q,r |- q&r
> thAI1 = Theorem [Q,R] (Q `And` R)
> prAI1 = AndI (Assume Q, Assume R) (And Q R)
To check that Proof prAI1 is a valid proof of Theorem thAI1, execute
the following expression interactively:
check_proof thAI1 prAI1
The next theorem is similar, but it's incorrect because Q `And` S
doesn't follow from assuming Q and R.
Theorem AI2. q,r |- q&s
Here are the theorem and an invalid proof of it, using the And
Introduction rule incorrectly.
> thAI2 = Theorem [Q,R] (Q `And` S)
> prAI2 =
> AndI (Assume Q, Assume R)
> (And Q S)
Try it out with the proof checker by evaluating the following
interactively:
check_proof thAI2 prAI2
And Elimination (Left)
~~~~~~~~~~~~~~~~~~~~~~
Here is a theorem and a valid proofs using And Elimination (Left).
Test it by evaluating
check_proof thAEL1 prAEL1
Theorem AEL1. p&q |- p
> thAEL1 = Theorem [P `And` Q] P
> prAEL1 = AndEL (Assume (And P Q)) P
This is a slightly more complex theorem, again with a valid proof
using And Elimination (Left). Check it with
check_proof thAEL2 prAEL2
Theorem AEL2. (P|Q)&R |- (P|Q)
> thAEL2 = Theorem [(P `Or` Q) `And` R] (P `Or` Q)
> prAEL2 = AndEL (Assume (And (Or P Q) R)) (Or P Q)
Now we try some invalid proofs. Consider the following theorem, the
theorem itself is valid but its proof is incorrect. Check it by
evaluating the expression check_AEL3; the proof checker will tell you
exactly what is wrong with the proof. Notice that we're moving to a
slightly more concise style, where we aren't naming both the theorem
and the proof separately.
Theorem AEL3. p&q |- p
> check_AEL3 =
> check_proof
> (Theorem [P `And` Q] P)
> (AndEL (Assume (Or P Q)) P)
> p6 = -- p&q |- p
> AndEL (Assume (And P Q)) Q
> p7 = -- P&Q |- R
> AndEL (Assume (And P Q)) R
And Elimination (Right)
~~~~~~~~~~~~~~~~~~~~~~~
The following theorem and proof are correct; they are the mirror image
of the AEL1 example above.
> check_AER1 =
> check_proof
> (Theorem [P `And` Q] Q)
> (AndER (Assume (P `And` Q)) Q)
This example is similar; the theorem is ok and the proof would be
valid if it used the AndEL rule, but the AndER rule does not justify
the conclusion, so the proof fails to establish the theorem.
> check_AER2 =
> check_proof
> (Theorem [P `And` Q] Q)
> (AndER (Assume (P `And` Q)) P)
Imply Introduction
~~~~~~~~~~~~~~~~~~
Theorem II1. Q |- P => P&Q
> check_II1 =
> check_proof
> (Theorem [Q] (P `Imp` (P `And` Q)))
> (ImpI (AndI (Assume P, Assume Q)
> (And P Q))
> (Imp P (And P Q)))
Theorem II2. |- Q => (P => (P&Q))
> check_II2 =
> check_proof
> (Theorem [] (Q `Imp` (P `Imp` (P `And` Q))))
> (ImpI (ImpI (AndI (Assume P, Assume Q)
> (And P Q))
> (Imp P (And P Q)))
> (Imp Q (Imp P (And P Q))))
Imply Elimination
~~~~~~~~~~~~~~~~~
Theorem IE1. P, P=>Q |- Q
> check_IE1 =
> check_proof
> (Theorem [P, P `Imp` Q] Q)
> (ImpE (Assume P, Assume (Imp P Q))
> Q)
Or Introduction (Left)
~~~~~~~~~~~~~~~~~~~~~~
Theorem OEL1. P&Q |- P&Q \/ S&P
> check_OIL1 =
> check_proof
> (Theorem [P `And` Q, Q `And` R]
> ((P `And` Q) `Or` (S `And` P)))
> (OrIL (Assume (P `And` Q))
> ((P `And` Q) `Or` (S `And` P)))
Or Introduction (Right)
~~~~~~~~~~~~~~~~~~~~~~~
Theorem OIR1. ~S |- P \/ ~S
> check_OIR1 =
> check_proof
> (Theorem [Not S] (P `Or` (Not S)))
> (OrIR (Assume (Not S))
> (P `Or` (Not S)))
Identity
~~~~~~~~
> check_ID0 =
> check_proof
> (Theorem [P `And` Q] (P `And` Q))
> (Assume (P `And` Q))
Now the same is proved using the ID inference rule.
Theorem ID1. P&Q |- P&Q
> check_ID1 =
> check_proof
> (Theorem [P `And` Q] (P `And` Q))
> (ID (Assume (P `And` Q)) (P `And` Q))
Contradiction
~~~~~~~~~~~~~
Theorem CTR1. False |- P & ~P
> check_CTR1 =
> check_proof
> (Theorem [FALSE] (P `And` (Not P)))
> (CTR (Assume FALSE) (P `And` (Not P)))
The next theorem isn't valid, because the premise P isn't enough to
establish P & ~P, and it isn't FALSE so the Contradiction inference
rule doesn't apply.
Theorem CTR2. P |- P & ~P
> check_CTR2 =
> check_proof
> (Theorem [P] (P `And` (Not P)))
> (CTR (Assume P) (P `And` (Not P)))
Reductio ab Absurdum
~~~~~~~~~~~~~~~~~~~~
Theorem RAA1. ~~P |- P
> check_RAA1 =
> check_proof
> (Theorem [P `Imp` FALSE, (P `Imp` FALSE) `Imp` FALSE]
> P)
> (RAA (ImpE (Assume (P `Imp` FALSE),
> Assume ((P `Imp` FALSE) `Imp` FALSE))
> FALSE)
> P)
Examples from the book
~~~~~~~~~~~~~~~~~~~~~~
Here is the theorem and proofs that are used in the book;
run them like this:
check_proof example_theorem example_proof1 (should be valid)
check_proof example_theorem example_proof2 (should give error message)
> example_theorem :: Theorem
> example_theorem =
> Theorem
> []
> (Imp Q (Imp (And P R) (And R Q)))
> example_proof1 =
> ImpI
> (ImpI
> (AndI
> ((AndER
> (Assume (And P R))
> R),
> Assume Q)
> (And R Q))
> (Imp (And P R) (And R Q)))
> (Imp Q (Imp (And P R) (And R Q)))
The following proof is incorrect proof, because Q^R was inferred where
R^Q was needed.
> example_proof2 =
> ImpI
> (ImpI
> (AndI
> (Assume Q,
> (AndER
> (Assume (And P R))
> R))
> (And R Q))
> (Imp (And P R) (And R Q)))
> (Imp Q (Imp (And P R) (And R Q)))
-------------------------------------------------------------------------------
Implementation of the Proof Checker for Propositional Logic
-------------------------------------------------------------------------------
> data Prop
> = FALSE
> | TRUE
> | A | B | C | D | E | G | H | I | J | K | L | M
> | N | O | P | Q | R | S | U | V | W | X | Y | Z
> | Pvar String
> | And Prop Prop
> | Or Prop Prop
> | Not Prop
> | Imp Prop Prop
> deriving Show -- Eq is not derived anymore, but completly defined-- *1
> data Theorem
> = Theorem [Prop] Prop
> deriving (Eq,Show)
> type TheoremDB = [String] -- a representation of all the known theorems-- *2
> data Proof
> = Assume Prop
> | AndI (Proof,Proof) Prop
> | AndEL Proof Prop
> | AndER Proof Prop
> | OrIL Proof Prop
> | OrIR Proof Prop
> | OrE (Proof,Proof,Proof) Prop
> | ImpI Proof Prop
> | ImpE (Proof,Proof) Prop
> | ID Proof Prop
> | CTR Proof Prop
> | RAA Proof Prop
> | Use Theorem [Proof] Prop -- *2
> | UseTh (Theorem,Proof) [Proof] Prop -- *2
> | UseDB Theorem [Proof] Prop -- *2
> deriving (Eq,Show)
When a proof is checked, several pieces of information are gathered
together into a data structure called Status.
> type Status =
> (Bool, -- True iff the proof is valid
> [String], -- messages describing errors found in the proof
> [Prop], -- undischarged assumptions required by the proof
> Prop) -- conclusion
The following type is used for comparisons, with the ordering on the
propositions
> data Comparison = Lesser | Equal | Greater deriving Eq
-------------------- definition of the Eq for a Prop ------------------------ *1
> instance Eq Prop where -- *1
> -- the basic definitions for the equality: -- *1
> (==) FALSE FALSE = True -- *1
> (==) TRUE TRUE = True -- *1
> (==) A A = True -- *1
> (==) B B = True -- *1
> (==) C C = True -- *1
> (==) D D = True -- *1
> (==) E E = True -- *1
> (==) G G = True -- *1
> (==) H H = True -- *1
> (==) I I = True -- *1
> (==) J J = True -- *1
> (==) K K = True -- *1
> (==) L L = True -- *1
> (==) M M = True -- *1
> (==) N N = True -- *1
> (==) O O = True -- *1
> (==) P P = True -- *1
> (==) Q Q = True -- *1
> (==) R R = True -- *1
> (==) S S = True -- *1
> (==) U U = True -- *1
> (==) V V = True -- *1
> (==) W W = True -- *1
> (==) X X = True -- *1
> (==) Y Y = True -- *1
> (==) Z Z = True -- *1
> (==) (Pvar x1) (Pvar x2) = x1 == x2 -- *1
> (==) (And x1 y1) (And x2 y2) = (x1 == x2) && (y1 == y2) -- *1
> (==) (Or x1 y1) (Or x2 y2) = (x1 == x2) && (y1 == y2) -- *1
> (==) (Not x) (Not y) = (x == y) -- *1
> (==) (Imp x1 y1) (Imp x2 y2) = (x1 == x2) && (y1 == y2) -- *1
> -- *1
> -- the definitions of (Not A) and TRUE -- *1
> (==) TRUE y = (Imp FALSE FALSE) == y -- *1
> (==) x TRUE = x == (Imp FALSE FALSE) -- *1
> (==) (Not x) y = ((Imp x FALSE) == y) -- *1
> (==) x (Not y) = (x == (Imp y FALSE)) -- *1
> -- *1
> -- all other cases: there is not equality: -- *1
> (==) _ _ = False -- *1
> -- *1
> -- definition of inequality, very basic... -- *1
> (/=) x y = not (x == y) -- *1
----------------------------- auxilary functions ---------------------------- *
'setelem' was deleted (use 'elem' instead) -- *
'jsubset' was deleted, and it's not needed anymore -- *
> union :: Eq a => [a] -> [a] -> [a]
> union xs ys = xs ++ [y | y <- ys, notElem y xs] -- *
> setdif :: Eq a => [a] -> [a] -> [a]
> setdif xs ys = [x | x <- xs, notElem x ys] -- *
> putmessages :: [String] -> IO () -- *
> putmessages [] = return () -- *
> putmessages (x:xs) = do putStr (" ." ++ x) -- *
> putmessages xs -- *
> putassumptions :: [Prop] -> IO () -- *
> putassumptions [] = return () -- *
> putassumptions (x:[]) = do putStr (show x) -- *
> putassumptions [] -- *
> putassumptions (x:xs) = do putStr (show x ++ ", ") -- *
> putassumptions xs -- *
------------------------------ the proof-checker ---------------------------- *
the following 'check_proof' is equivalent to the 'check_proof' of the -- *2
previous versions. -- *2
> check_proof :: Theorem -> Proof -> IO () -- *2
> check_proof t p = -- *2
> do check_proof_with_db [] t p -- *2
> return () -- *2
'check_proof_with_db' allows to use theorems stored in a data-base -- *2
> check_proof_with_db :: TheoremDB -> Theorem -> Proof -> IO (Bool) -- *2
> check_proof_with_db db (Theorem thas thc) proof = -- *2
> do let (valid, ms, uda, concl) = traverse' db proof -- *2
> let missingassum = setdif uda thas -- *
> let uselessassum = setdif thas uda -- *
> let allok = valid && (missingassum == []) && (concl == thc) -- *
> if allok -- *
> then do putStr "The proof is valid\n" -- *
> if (uselessassum == []) -- *
> then putStr "" -- *
> else do putStr "notice: these assumptions are useless: "-- *
> putassumptions uselessassum -- *
> putStr "\n" -- *
> else do putStr "*** The proof is NOT valid ***\n" -- *
> if valid -- *
> then do putStr "The proof does not match the sequent.\n"-- *
> putStr " .what is actually proved is:\n " -- *
> putassumptions uda -- *
> putStr (" |- " ++ show concl ++ "\n") -- *
> if (missingassum == []) -- *
> then putStr "" -- *
> else do putStr (" .these assumptions are used"-- *
> ++ " but not part of the sequ"-- *
> ++ "ent:\n ") -- *
> putassumptions missingassum -- *
> putStr "\n" -- *
> else do if (ms == []) -- *
> then putStr "" -- *
> else do putStr "Reported errors:\n" -- *
> putmessages ms -- *
> return (allok) -- *2
The real work is performed in the traverse' function, which has a
separate case for checking each inference rule. Nearly all the
complexity in the following code results from an attempt to provide
meaningful error messages; if the aim were merely to decide whether a
proof is valid, then the implementation could rely much more on
Haskell's pattern matching mechanism, and everything would be much
more concise.
> traverse' :: TheoremDB -> Proof -> Status -- *2
> traverse' _ (Assume a) = (True, [], [a], a) -- *2
> traverse' db (AndI (a,b) c) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (bvalid, bmsgs, buda, bconcl) = traverse' db b -- *2
> (ok, msg) = -- *
> case c of -- *
> And p q -> if (p==aconcl) && (q==bconcl) -- *
> then (True, []) -- *
> else (False, [err1]) -- *
> otherwise -> (False, [err2]) -- *
> err1 = "AndI: the conclusion (" ++ show c ++ ") is not the " -- *
> ++ "logical And of the assumption (" ++ show aconcl -- *
> ++ ") with the assumption (" ++ show bconcl ++ ")\n" -- *
> err2 = "AndI: the conclusion (" ++ show c ++ ") is not an " -- *
> ++ "And expression\n" -- *
> valid = avalid && bvalid && ok
> msgs = amsgs ++ bmsgs ++ msg -- *
> uda = auda `union` buda
> in (valid, msgs, uda, c)
> traverse' db (AndEL a b) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (ok,msg) =
> case aconcl of
> And p q -> if p == b then (True,[])
> else (False, [err1])
> otherwise -> (False, [err2])
> err1 = "AndEL: the left term of And assumption (" ++ show aconcl -- *
> ++ ") doesn't match the conclusion (" ++ show b ++ ")\n" -- *
> err2 = "AndEL: the assumption (" ++ show aconcl ++ ") is not an " -- *
> ++ "And expression\n" -- *
> valid = avalid && ok
> msgs = amsgs ++ msg
> uda = auda
> in (valid, msgs, uda, b)
> traverse' db (AndER a b) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (ok,msg) =
> case aconcl of
> And p q -> if q == b then (True,[])
> else (False, [err1])
> otherwise -> (False, [err2])
> err1 = "AndER: the right term of And assumption (" ++ show aconcl -- *
> ++ ") doesn't match the conclusion (" ++ show b ++ ")\n" -- *
> err2 = "AndER: the assumption (" ++ show aconcl ++ ") is not an " -- *
> ++ "And expression\n" -- *
> valid = avalid && ok
> msgs = amsgs ++ msg
> uda = auda
> in (valid, msgs, uda, b)
> traverse' db (OrIL a b) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (ok,msg) = -- *
> case b of
> Or p q -> if aconcl == p then (True,[])
> else (False,[err1])
> otherwise -> (False,[err2])
> err1 = "OrIL: the left term of OR conclusion (" ++ show b -- *
> ++ ") doesn't match the assumption (" ++ show aconcl -- *
> ++ ")\n" -- *
> err2 = "OrIL: the conclusion (" ++ show b ++ ") is not an Or " -- *
> ++ "expression\n" -- *
> valid = avalid && ok
> msgs = amsgs ++ msg -- *
> uda = auda
> in (valid, msgs, uda, b)
> traverse' db (OrIR a b) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (ok,msg) = -- *
> case b of
> Or p q -> if aconcl == q then (True,[])
> else (False,[err1])
> otherwise -> (False,[err2])
> err1 = "OrIL: the right term of OR conclusion (" ++ show b -- *
> ++ ") doesn't match the assumption (" ++ show aconcl -- *
> ++ ")\n" -- *
> err2 = "OrIL: the conclusion (" ++ show b ++ ") is not an Or " -- *
> ++ "expression\n" -- *
> valid = avalid && ok
> msgs = amsgs ++ msg -- *
> uda = auda
> in (valid, msgs, uda, b)
> traverse' db (OrE (a,b,c) d) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (bvalid, bmsgs, buda, bconcl) = traverse' db b -- *2
> (cvalid, cmsgs, cuda, cconcl) = traverse' db c -- *2
> (ok,msg,uda) = -- *
> case aconcl of -- *
> Or p q -> let ok2 = p `elem` buda -- *
> ok3 = q `elem` cuda -- *
> ok4 = bconcl == cconcl -- *
> ok5 = bconcl == d -- *
> in if ok2 && ok3 && ok4 && ok5 -- *
> then (True,[], auda `union` (buda `setdif` [p])-- *
> `union` (cuda `setdif` [q]))-- *
> else (False, -- *
> (if ok2 then [] else [err2 p]) -- *
> ++ (if ok3 then [] else [err3 q]) -- *
> ++ (if ok4 then if ok5 then [] else [err5] -- *
> else [err4]), -- *
> auda `union` buda `union` cuda) -- *
> otherwise -> (False, [err1], -- *
> auda `union` buda `union` cuda) -- *
> err1 = "OrE: the first term above line (" ++ show aconcl -- *
> ++ ") is not an Or expression\n" -- *
> err2 x = "OrE: the left term (" ++ show x ++ ") of the first " -- *
> ++ "term above line (" ++ show aconcl ++ ") is not an " -- *
> ++ "undischarged assumption of the proof of the second "-- *
> ++ "term above line (" ++ show bconcl ++ ")\n" -- *
> err3 x = "OrE: the right term (" ++ show x ++ ") of the first " -- *
> ++ "term above line (" ++ show aconcl ++ ") is not an " -- *
> ++ "undischarged assumption of the proof of the third " -- *
> ++ "term above line (" ++ show bconcl ++ ")\n" -- *
> err4 = "OrE: the conclusion of the second term above line (" -- *
> ++ show bconcl ++ ") doesn't match the conclusion of the " -- *
> ++ "third term above line (" ++ show cconcl ++ ")\n" -- *
> err5 = "OrE: the conclusion (" ++ show d ++ ") doesn't match the "-- *
> ++ "conclusion of the second and third terms above line (" -- *
> ++ show bconcl ++ ")\n" -- *
> valid = avalid && bvalid && cvalid && ok -- *
> msgs = amsgs ++ bmsgs ++ cmsgs ++ msg -- *
> in (valid, msgs, uda, d) -- *
> traverse' db (ImpI a b) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (ok,msg) = match b -- *1
> match x = -- *1
> case x of -- *1
> Imp p q -> if p `elem` auda && aconcl==q -- *
> then (True,[])
> else if not (p `elem` auda) && aconcl==q -- *
> then (False,[err2])
> else if p `elem` auda && aconcl/=q -- *
> then (False,[err3])
> else (False,[err2,err3])
> Not p -> match (Imp p FALSE) -- *1
> FALSE -> match (Imp TRUE TRUE) -- *1
> otherwise -> (False,[err1])
> err1 = "ImpI: the conclusion (" ++ show b ++ ") is not an " -- *
> ++ "implication\n" -- *
> err2 = "ImpI: the antecedent in (" ++ show b ++ ") is not an " -- *
> ++ "undischarged assumption above line\n" -- *
> err3 = "ImpI: the conclusion in (" ++ show b ++ ") doesn't match "-- *
> ++ "the conclusion above line (" ++ show aconcl ++ ")\n" -- *
> valid = avalid && ok
> msgs = amsgs ++ msg -- *
> uda = case b of
> Imp p q -> auda `setdif` [p]
> Not p -> auda `setdif` [p] -- *1
> FALSE -> auda `setdif` [TRUE] -- *1
> otherwise -> auda
> in (valid, msgs, uda, b)
> traverse' db (ImpE (a,b) c) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (bvalid, bmsgs, buda, bconcl) = traverse' db b -- *2
> (ok,msg) = match bconcl -- *1
> match x = -- *1
> case x of -- *1
> Imp p q ->if aconcl==p && c==q
> then (True,[])
> else if aconcl/=p && c==q
> then (False,[err2])
> else if aconcl==p && c/=q
> then (False,[err3])
> else (False,[err2,err3])
> Not p -> match (Imp p FALSE) -- *1
> FALSE -> match (Imp TRUE TRUE) -- *1
> otherwise -> (False,[err1])
> err1 = "ImpE: second term above line (" ++ show bconcl -- *
> ++ ") is not an implication\n" -- *
> err2 = "ImpE: first term (" ++ show aconcl ++ ") doesn't match " -- *
> ++ " the antecedent of the second term (" ++ show bconcl -- *
> ++ ")\n" -- *
> err3 = "ImpE: the conclusion (" ++ show c ++ ") doesn't match " -- *
> ++ "the conclusion of the second term (" ++ show bconcl -- *
> ++ ")\n"
> valid = avalid && bvalid && ok
> msgs = amsgs ++ bmsgs ++ msg -- *
> uda = auda `union` buda
> in (valid, msgs, uda, c)
> traverse' db (ID a c) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (ok,msg) = if aconcl == c -- *
> then (True,[])
> else (False,[err1])
> err1 = "ID: the conclusion (" ++ show c ++ ") doesn't match " -- *
> ++ "the antecedent (" ++ show aconcl ++ ")\n" -- *
> valid = avalid && ok
> msgs = amsgs ++ msg -- *
> uda = auda -- *
> in (valid, msgs, uda, c) -- *
> traverse' db (CTR a c) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (ok,msg) = if aconcl == FALSE -- *
> then (True,[])
> else (False,[err1])
> err1 = "CTR: the antecedent (" ++ show aconcl ++ ") is not " -- *
> ++ "FALSE\n" -- *
> valid = avalid && ok
> msgs = amsgs ++ msg -- *
> uda = auda -- *
> in (valid, msgs, uda, c) -- *
> traverse' db (RAA a c) = -- *2
> let (avalid, amsgs, auda, aconcl) = traverse' db a -- *2
> (ok,msg) = if not ((c `Imp` FALSE) `elem` auda) -- *
> then (False, [err1])
> else if aconcl /= FALSE
> then (False, [err2])
> else (True, [])
> err1 = "RAA: the negation of the conclusion (" ++ show c -- *
> ++ ") is not an undischarged assumption of the proof " -- *
> ++ "above the line\n" -- *
> err2 = "RAA: the conclusion (" ++ show aconcl ++ ") of the proof "-- *
> ++ "above the line is not FALSE\n" -- *
> valid = avalid && ok
> msgs = amsgs ++ msg -- *
> uda = auda `setdif` [(c `Imp` FALSE)] -- *
> in (valid, msgs, uda, c) -- *
(everything of chapter 2 that follows has been added as 2nd extension, and-- *2
every line should be marked with-- *2) -- *2
----------------------- traverse' function for 'USE' ------------------------- *2
Use assumes that the theorem is correct
> traverse' db (Use theo assums concl) =
> let (oks, msgs, udas, concls) = checkproofs db assums
> err2 = "Use: the theorem " ++ show theo ++ " cannot be applied "
> ++ "with the assumptions " ++ show concls
> ++ " and the conclusion ("++ show concl ++")\n"
> in if not oks then (False, msgs, udas, concl)
> else if (usetheorem theo concls concl) then
> (True, msgs, udas, concl)
> else (False, msgs++[err2], udas, concl)
UseTh needs the proof of a theorem to check if it's valid and then can be used.
> traverse' db (UseTh (theo,proof) assums concl) =
> -- check if the theorem is valid
> let (proofvalid, _, proofuda, proofconcl) = traverse' db proof
> (Theorem thas thc) = theo
> missingassum = setdif proofuda thas
> theook = proofvalid && (missingassum == []) && (proofconcl == thc)
> -- check if the assumptions of the theorems are valid
> (oks, msgs, udas, concls) = checkproofs db assums
> err1 = "UseTh: the proof of the theorem " ++ show theo
> ++ " is not valid and then the theorem cannot be used\n"
> err2 = "UseTh: the theorem " ++ show theo ++ " cannot be applied "
> ++ "with the assumptions " ++ show concls
> ++ " and the conclusion ("++ show concl ++")\n"
> in if not theook
> then (False, msgs++[err1], udas, concl)
> else if not oks then (False, msgs, udas, concl)
> else if (usetheorem theo concls concl) then
> (True, msgs, udas, concl)
> else (False, msgs++[err2], udas, concl)
UseDB checks if a theorem can be used by looking for it in a data-base
> traverse' db (UseDB theo assums concl) =
> let (oks, msgs, udas, concls) = checkproofs db assums
> err1 = "UseDB: there is no known theorem of which name is "
> ++ show theo ++ "\n"
> err2 = "UseDB: the theorem " ++ show theo ++ " cannot be applied "
> ++ "with the assumptions " ++ show concls
> ++ " and the conclusion ("++ show concl ++")\n"
> in if not (findtheorem db theo)
> then (False, msgs++[err1], udas, concl)
> else if not oks then (False, msgs, udas, concl)
> else if (usetheorem theo concls concl) then
> (True, msgs, udas, concl)
> else (False, msgs++[err2], udas, concl)
----------------------- the functions to use a theorem ---------------------- *2
the function 'checkproof' check the proofs of every assumptions of the
theorem, and return the list of the conclusions of these proofs, with
their validity, the error messages and the undischarged assumptions
> checkproofs :: TheoremDB -> [Proof] -> (Bool, [String], [Prop], [Prop])
> checkproofs _ [] = (True, [], [], [])
> checkproofs db (x:xs) =
> let (okx, msgx, udasx, cclx) = traverse' db x
> (okxs, msgxs, udasxs, cclxs) = checkproofs db xs
> in (okx && okxs, msgx ++ msgxs, udasx `union` udasxs, cclx:cclxs)
the function 'usetheorem' checks if a theorem can be applied with the given
assumption to reach the given conclusion. Actually, the work is mainly
performed by the functions 'unification' and 'allunifications'
> usetheorem :: Theorem -> [Prop] -> Prop -> Bool
> usetheorem theo assums concl =
> let (Theorem thassums thconcl) = theo
> (okconcl, _, unifconcl) = unif thconcl concl
> in (length thassums == length assums) && -- must be of same length
> (okconcl) && -- conclusions must match
> ((unification unifconcl thassums assums)
> || (allunifications unifconcl thassums assums))
the function 'unification' try to match the atoms of the first list of
proposition with sub-propositions of the second list. The propositions are
taken in the order they appear within the lists. The first argument is a
list of already matched pairs.
The 2 list are assumed to be of the same length.
> unification :: [(Prop, Prop)] -> [Prop] -> [Prop] -> Bool
> unification initialpairs l1 l2 =
> let (ok, _) = unifrec l1 l2 in ok
> where
> unifrec :: [Prop] -> [Prop] -> (Bool, [(Prop, Prop)])
> unifrec [] [] = (True, initialpairs)
> unifrec (x:xs) (y:ys) =
> let (okxy, _, unifxy) = unif x y
> (oks, unifs) = if okxy then (unifrec xs ys)
> else (False, [])
> (oku, unifu) = if oks then addunif unifxy unifs
> else (False, [])
> in (oku, unifu)
the function 'allunifiactions' is a kind of extended version of 'unification':
now, the order of the propositions does not matter, since every permutation may
be considered.
> allunifications :: [(Prop, Prop)] -> [Prop] -> [Prop] -> Bool
> allunifications initialpairs l1 l2 =
> let (ok,_) = allunifrec (order l1) ([],(order l2)) initialpairs in ok
> where
> allunifrec :: [Prop] -> ([Prop],[Prop]) -> [(Prop,Prop)]
> -> (Bool, [(Prop,Prop)])
> allunifrec [] ([],[]) pairs = (True, pairs)
> allunifrec (_:_) (_, []) _ = (False, [])
>
> allunifrec (x:xs) (prev, (y:ys)) pairs =
> let (okxy, eqxy, unifxy) = unif x y
> in if okxy then
> let (oku, unifu) = addunif pairs unifxy
> in if not oku then trynext
> else let (oks, unifs) = allunifrec xs ([], prev++ys) unifu
> in if not oks then trynext else (oks, unifs)
> else
> if eqxy == Greater then (False, [])
> else trynext
> where
> trynext = allunifrec (x:xs) (prev++[y], ys) pairs
>
> allunifrec _ _ _ = (False, [])
unification of 2 propositions: the function 'unif'
. the Bool-value returned is True iff the unification succeeded
. the Comparison-value returned is the comparison between the 2 trees of the
propositions (cf 'comparison')
. the list returned is the list of the matching pairs
> unif :: Prop -> Prop -> (Bool, Comparison, [(Prop, Prop)])
> unif x y =
> case x of
> TRUE -> if y == TRUE then (True, Equal, [])
> else (False, comparison x y, [])
> FALSE -> if y == FALSE then (True, Equal, [])
> else (False, comparison x y, [])
> And a b -> case y of
> And c d -> unifacbd a c b d
> otherwise -> (False, comparison x y, [])
> Or a b -> case y of
> Or c d -> unifacbd a c b d
> otherwise -> (False, comparison x y, [])
> Not a -> case y of
> Not b -> unif a b
> Imp b FALSE -> unif a b
> otherwise -> (False, comparison x y, [])
> Imp a b -> case y of
> Imp c d -> unifacbd a c b d
> Not c -> if b == FALSE then unif a c
> else (False, comparison x y, [])
> otherwise -> (False, comparison x y, [])
> _ -> (True, Equal, [(x,y)])
> where
> unifacbd a c b d =
> let (okac, eqac, unifac) = unif a c
> (okbd, eqbd, unifbd) = unif b d
> (oku, unifu) = addunif unifac unifbd
> eqacbd = if (eqac /= Equal) then eqac else eqbd
> in if oku then (True, eqacbd, unifu) else (False, eqacbd, [])
'addunif' performs the union of 2 sets of matching pairs, and checks whether
there is a conflict
> addunif :: [(Prop,Prop)] -> [(Prop, Prop)] -> (Bool, [(Prop,Prop)])
> addunif xs [] = (True, xs)
> addunif xs (y:ys) =
> let (exp,val) = y
> setexp = [v | (y,v) <- xs, y == exp]
> in if setexp == [] then addunif (y:xs) ys
> else if val == head setexp then addunif xs ys
> else (False, [])
comparison of 2 trees of proposition, with 3 possible values.
the order is:
And > Or > Imp > FALSE > (variable)
when its the same operator, the left sub-tree is considered first.
for TRUE and Not, their definitions are used.
> comparison :: Prop -> Prop -> Comparison
> comparison a b =
> case (a,b) of
> -- definitions of Not and TRUE:
> (Not x, _) -> comparison (x `Imp` FALSE) b
> (_, Not y) -> comparison a (y `Imp` FALSE)
> (TRUE, _) -> comparison (FALSE `Imp` FALSE) b
> (_, TRUE) -> comparison a (FALSE `Imp` FALSE)
>
> -- equality:
> (FALSE, FALSE) -> Equal
> (Imp x1 x2, Imp y1 y2) -> comparison2 x1 y1 x2 y2
> (Or x1 x2, Or y1 y2) -> comparison2 x1 y1 x2 y2
> (And x1 x2, And y1 y2) -> comparison2 x1 y1 x2 y2
>
> -- inequality (from the greatest to the least):
> (And _ _, _) -> Greater
> (_, And _ _) -> Lesser
> (Or _ _, _) -> Greater
> (_, Or _ _) -> Lesser
> (Imp _ _, _) -> Greater
> (_, Imp _ _) -> Lesser
> (FALSE, _) -> Greater
> (_, FALSE) -> Lesser
>
> -- equality of tree (2 variables):
> (_, _) -> Equal
> where
> comparison2 x1 y1 x2 y2 =
> let comp1 = comparison x1 y1
> in if (comp1 /= Equal) then comp1 else (comparison x2 y2)
the function 'order' sorts proposition from the greatest to the least,
according to the function 'comparison'
(note: the result depends on the initial list, since 2 differents propositions
may be considered equal as 'comparison' compares trees not values)
> order :: [Prop] -> [Prop]
> order [] = []
> order (x:xs) = (order greater_egal) ++ [x] ++ (order lesser)
> where
> (lesser, greater_egal) = split xs ([],[])
> split [] (le,g) = (le,g)
> split (y:ys) (le,g) =
> split ys (if (comparison y x == Lesser)
> then (y:le, g)
> else ( le, y:g))
----------------------- functions to use the data-base ---------------------- *2
the function 'findtheorem' looks if a theorem belongs to the data-base of known
theorems.
> findtheorem :: TheoremDB -> Theorem -> Bool
> findtheorem db t =
> case db of
> [] -> False
> (x:xs) -> x == s || findtheorem xs t
> where s = namefor t
the function 'namefor' give a name (String) to a theorem, which does not
depend on the name given to the variables of the theorem. For example,
(Theorem [A,B] (B `And` A)) and (Theorem [C,D] (D `And` C)) will receive
the same name.
> namefor :: Theorem -> String
> namefor t =
> let (Theorem assums concl) = t
> names = name (concl:assums)
> in write assums names ++ " |- " ++ write [concl] names
> where
> name :: [Prop] -> [(Prop,Int)]
> name xs = zip (atoms xs) [1..]
> where
> atoms :: [Prop] -> [Prop]
> atoms [] = []
> atoms (x:xs) =
> case x of
> TRUE -> atoms xs
> FALSE -> atoms xs
> And p q -> ((atoms [p]) `union` (atoms [q])) `union` (atoms xs)
> Or p q -> ((atoms [p]) `union` (atoms [q])) `union` (atoms xs)
> Imp p q -> ((atoms [p]) `union` (atoms [q])) `union` (atoms xs)
> Not p -> (atoms [p]) `union` (atoms xs)
> _ -> [x] `union` (atoms xs)
>