-
Notifications
You must be signed in to change notification settings - Fork 0
/
formula_check_syntax.scm
107 lines (81 loc) · 3.45 KB
/
formula_check_syntax.scm
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
;scheme.eval("(formula-check-syntax '(bigand c (Red Blue Green Yellow) ((not (France c)) or (not (Germany c)))))")
;scheme.eval("(formula-check-syntax 'p)")
;scheme.eval("(formula-check-syntax '((p or (not t)) or (q or t) and t))")
(define (formula-check-syntax phi)
(match phi
(('bigand i s condition psi)
(+ (if (list? i) "After bigand, there should be an identifier. " "") (formula-check-syntax psi)))
(('bigor i s condition psi)
(+ (if (list? i) "After bigand, there should be an identifier. " "") (formula-check-syntax psi)))
(('bigand i s psi)
(+ (if (list? i) "After bigand, there should be an identifier. " "") (formula-check-syntax psi)))
(('bigor i s psi)
(+ (if (list? i) "After bigand, there should be an identifier. " "") (formula-check-syntax psi)))
(('bigxor i s psi)
(+ (if (list? i) "After bigand, there should be an identifier. " "") (formula-check-syntax psi)))
((phi 'and psi)
(+ (formula-check-syntax phi) (formula-check-syntax psi)))
((phi 'or psi)
(+ (formula-check-syntax phi) (formula-check-syntax psi)))
((phi 'xor psi)
(+ (formula-check-syntax phi) (formula-check-syntax psi)))
((phi 'imply psi)
(+ (formula-check-syntax phi) (formula-check-syntax psi)))
((phi 'equiv psi)
(+ (formula-check-syntax phi) (formula-check-syntax psi)))
(('not phi)
(formula-check-syntax phi))
(a
(if (list? a)
(if (null? a)
"La construction () n'est pas licite."
(if (null? (cdr a))
"Une construction comme (phi) n'est pas licite. On ne doit pas surparenthéser des formules."
(if (formula-and-at-second-position? a)
(formula-check-syntax-and a)
(if (formula-or-at-second-position? a)
(formula-check-syntax-or a)
(check-contains-no-connectors a)))))
(check-is-not-connector a)
))))
;(formula-or-at-second-position? '(p and q and r))
(define (formula-or-at-second-position? l)
(if (list? l)
(if (equal? (cadr l) 'or)
#t
#f)
#f))
;(formula-and-at-second-position? '(p and q and r))
(define (formula-and-at-second-position? l)
(if (list? l)
(if (equal? (cadr l) 'and)
#t
#f)
#f))
;(andByTwo '(a and b and c))
;(andByTwo '((r i) and (not (a i)) and (not (b i)) ))
(define (formula-check-syntax-and l)
(if (null? (cdr l))
""
(if (not (equal? (cadr l) 'and))
"Il faut un and pour continuer la conjonction."
(+ (formula-check-syntax (car l)) (formula-check-syntax-and (cddr l))))))
;(andByTwo '(a and b and c))
;(andByTwo '((r i) and (not (a i)) and (not (b i)) ))
(define (formula-check-syntax-or l)
(if (null? (cdr l))
""
(if (not (equal? (cadr l) 'or))
"Il faut un or pour continuer la disjonction."
(+ (formula-check-syntax (car l)) (formula-check-syntax-or (cddr l))))))
(define (check-contains-no-connectors a)
(if (list? a)
(if (null? a)
""
(+ (check-contains-no-connectors (car a)) (check-contains-no-connectors (cdr a))))
(check-is-not-connector a)))
;scheme.eval("(check-is-not-connector 'or)")
(define (check-is-not-connector a)
(if (contains? a '(and or bigand bigor bigxor))
"Il y a un connecteur qui n'est pas à la bonne place. On exige ici que les constructions sont toutes parenthésées. Par exemple 'p or q' n'est pas correct mais '(p or q)' l'est."
""))