Advertisement
Guest User

tp logique scheme

a guest
Mar 25th, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 8.81 KB | None | 0 0
  1. #lang racket
  2.  
  3. ; 1 Représentation des propositions
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5.  
  6. (define F '(<-> (^ a c) (v (! b) (-> c (^ B T)))))
  7.  
  8. ; Q1
  9. (define F1 '(<-> (^ a b) (! (v a b))))
  10. (define F2 '(v (! (^ a (! b))) (! (-> a b))))
  11. (define F3 '(^ (! (-> a (v a b))) (! (! (^ a (v b (! c)))))))
  12.  
  13. (define F5 '())
  14.  
  15. (define FX '(v a b)
  16.   )
  17.  
  18. (define F4 '(^
  19.              (v (v
  20.                  (! a)
  21.                  b)
  22.                 d)
  23.              (^
  24.               (v
  25.                (! d)
  26.                c)
  27.               (^
  28.                (v c a)
  29.                (^
  30.                 (v
  31.                  (! c)
  32.                  b)
  33.                 (^
  34.                  (v
  35.                   (! c)
  36.                   (! b))
  37.                  (v
  38.                   (! b)
  39.                   d)
  40.                  )
  41.                 )
  42.                )
  43.               )
  44.              )
  45.   )
  46.  
  47.  
  48. ; Q2
  49. (define (neg? f) (eq? f '!))
  50. (define (and? f) (eq? f '^))
  51. (define (or? f) (eq? f 'v))
  52. (define (imp? f) (eq? f '->))
  53. (define (equ? f) (eq? f '<->))
  54. (define (top? f) (eq? f 'T))
  55. (define (bot? f) (eq? f 'B))
  56. (define (symbLog? f) (or (top? f) (bot? f) (and? f) (or? f) (neg? f) (imp? f) (equ? f)))
  57. (define (symbProp? f) (and (symbol? f) (not (symbLog? f))))
  58. (define (atomicFbf? f) (or (symbProp? f) (top? f) (bot? f)))
  59. (define (conBin? f) (or (and? f) (or? f) (imp? f) (equ? f)))
  60.  
  61. ; Q2
  62. (define (fbf? f)
  63.   (cond ((atomicFbf? f) #t)
  64.         ((list? f) (cond ((and (= (length f) 2) (neg? (car f))) (fbf? (cadr f)))
  65.                          ((and (= (length f) 3) (conBin? (car f))) (and (fbf? (cadr f)) (fbf? (caddr f))))
  66.                          (else #f)))
  67.         (else #f)))
  68.  
  69. (define (fbf2? f)
  70.   (cond ((atomicFbf? f) (begin (display " case1 : " ) (display f) #t))
  71.         ((list? f) (cond ((and (= (length f) 2) (neg? (car f)))  (begin (display " case2.1 : f = ") (display f) (display " ; car f = " ) (display (car f)) (display " cdr f = ") (display (cdr f)) (fbf2? (cadr f))))
  72.                          ((and (= (length f) 3) (conBin? (car f))) (begin (display " case2.2 : f = " ) (display f) (display " ; car f = " ) (display (car f)) (display " cadr f = ") (display (cadr f)) (display " caddr f = ") (display (caddr f)) (and (fbf2? (cadr f)) (fbf2? (caddr f)))))
  73.                          (else (begin (display " case2.1 : f = " ) (display f) #f))))
  74.         (else (and (display " case3 : ") #f)))
  75.   )
  76.  
  77. (define (fbf3? f)
  78.   (cond ((atomicFbf? f) (begin (display 't1) #t))
  79.         ((list? f) (cond ((and (= (length f) 2) (neg? (car f)))  (begin (display 't2) (fbf3? (cadr f))))
  80.                          ((and (= (length f) 3) (conBin? (car f))) (begin (display 't3) (and (fbf3? (cadr f)) (fbf3? (caddr f)))))
  81.                          (else (begin (display 't4) #f))))
  82.         (else (begin (display 't5) #f))
  83.         )
  84.   )
  85.  
  86. (define (fbfp p)
  87.   (define  (X p1)
  88.     (cond
  89.       ((eq? p1 '()) #t)
  90.       ((not (eq? '() p1)) (and (fbfp (car p1))))
  91.       )
  92.     )
  93.   (X (cdr p))
  94.   )
  95.  
  96.  
  97. ; 2 Syntaxe des propositions
  98. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  99. (display " ")
  100. (define (conRac f) (car f))
  101. (define (fils f) (cadr f))
  102. (define (filsG f) (cadr f))
  103. (define (filsD f) (caddr f))
  104. (define (negFbf? f) (and (not (atomicFbf? f)) (neg? (conRac f))))
  105.  
  106. ; Q3
  107. (define (nbc? f)
  108.   (cond ((atomicFbf? f) 0)
  109.         ((negFbf? f) (+ 1 (nbc? (fils f))))
  110.         (#t (+ 1 (nbc? (filsG f)) (nbc? (filsD f))))))
  111.  
  112.  
  113. ; Q4
  114. (define (prof? f)
  115.   (cond ((atomicFbf? f) 0)
  116.         ((negFbf? f) (+ 1 (prof? (fils f))))
  117.         (#t (max (+ 1 (prof? (filsG f))) (+ 1 (prof? (filsD f)))))
  118.         )
  119.   )
  120.  
  121. ; Q5
  122.  
  123. (define (ensSP? f)
  124.   (cond
  125.     [(atomicFbf? f) (set-add '() f)]
  126.     [(negFbf? f) (set-union (ensSP? (cadr f)))]
  127.     [#t (set-union (ensSP? (cadr f)) (ensSP? (caddr f)))]
  128.     )
  129.   )
  130.  
  131. ; Q6
  132. (define (affiche fbf)
  133.   (cond
  134.     [(atomicFbf? fbf) (display fbf)]
  135.     [(negFbf? fbf) (begin (display "(") (display "¬") (affiche (fils fbf)) (display ")"))]
  136.     [#t
  137.      (begin (display "(") (affiche (filsG fbf)) (display " ") (display (car fbf)) (display " ") (affiche (filsD fbf)) (display ")"))]
  138.     )
  139.   )
  140.  
  141.  
  142. (define (affiche2 fbf)
  143.   (begin (display "(")
  144.          (display
  145.           (cond
  146.             [(atomicFbf? fbf) (symbol->string fbf)]
  147.             [(negFbf? fbf) (string-append " ! " (affiche (fils fbf)))]
  148.             [#t (string-append (affiche (filsG fbf)) (string-append " " (string-append (car fbf) (string-append " " (affiche (filsD fbf))))))]
  149.             )
  150.           )
  151.          (display ")")
  152.          )
  153.   )
  154.  
  155. ; Q7
  156. (define I1(cons (cons 'a 1) (cons (cons 'c 1) (cons (cons 'b 0) null))))
  157. (define I2(cons (cons 'a 0) (cons (cons 'c 0) (cons (cons 'b 0) null))))
  158. (define I3(cons (cons 'a 1) (cons (cons 'c 1) (cons (cons 'b 1) null))))
  159.  
  160. ; Q8
  161. (define (intSymb symb itp)
  162.   (cond
  163.     [(eq? symb (caar itp)) (cdar itp)]
  164.     [#t (intSymb symb (cdr itp))]
  165.     )
  166.   )
  167.  
  168. ; Q9
  169. (define (intNeg b)
  170.   (if (eq? b 0) 1 0)
  171.   )
  172. (define (intAnd b1 b2)
  173.   (if (and (eq? b1 1) (eq? b2 1)) 1 0)
  174.   )
  175. (define (intOr b1 b2)
  176.   (if (or (eq? b1 1) (eq? b2 1)) 1 0)
  177.   )
  178. (define (intImp b1 b2)
  179.   (if (eq? b1 0) 1 b2)
  180.   )
  181. (define (intEqu b1 b2)
  182.   (if (eq? (if (eq? b1 0) 1 b2) (if (eq? b2 0) 1 b1)) 1 0)
  183.   )
  184. (define intTop 1)
  185. (define intBot 0)
  186.  
  187. ; Q10
  188. (define (valV f i)
  189.   (cond [(symbProp? f) (intSymb f i)]
  190.         [(top? f) intTop]
  191.         [(bot? f) intBot]
  192.         [(negFbf? f) (intNeg (valV (fils f) i))]
  193.         [(and? (conRac f)) (intAnd (valV (filsG f) i) (valV (filsD f) i))]
  194.         [(or? (conRac f)) (intOr (valV (filsG f) i) (valV (filsD f) i))]
  195.         [(imp? (conRac f)) (intImp (valV (filsG f) i) (valV (filsD f) i))]
  196.         [(equ? (conRac f)) (intEqu (valV (filsG f) i) (valV (filsD f) i))]
  197.         [#t 0]
  198.         )
  199.   )
  200.  
  201. ; Q11
  202. (define (modele? i fbf)
  203.   (eq? (valV fbf i) 1)
  204.   )
  205.  
  206. ; Q12
  207. (define ensISP
  208.   (list
  209.    (list (cons 'p 0) (cons 'q 0))
  210.    (list (cons 'p 0) (cons 'q 1))
  211.    (list (cons 'p 1) (cons 'q 0))
  212.    (list (cons 'p 1) (cons 'q 1))
  213.    )
  214.   )
  215.  
  216. ; Q13
  217. (define (ensInt ESP)
  218.   (cond ((set-empty? ESP) (list null))
  219.         (else (let ((ensIntCdr(ensInt (cdr ESP))))
  220.                 (set-union
  221.                  (map [lambda (i) (cons (cons (car ESP) 0) i)] ensIntCdr)
  222.                  (map [lambda (i) (cons (cons (car ESP) 1) i)] ensIntCdr)
  223.                  )
  224.                 )
  225.               )
  226.         )
  227.   )
  228.  
  229. (define (ensInt2 ESP)
  230.   (cond ((null? ESP) '(()))
  231.         (else (let ((I (ensInt (cdr ESP))))
  232.                 (append
  233.                  (map [lambda (i) (set-add i (cons (car ESP) 0))] I)
  234.                  (map [lambda (i) (set-add i (cons (car ESP) 1))] I)
  235.                  )
  236.                 )
  237.               )
  238.         )
  239.   )
  240.  
  241. ; Q14
  242. (define (satisfiable? fbf)
  243.   (ormap (lambda (i) (modele? i fbf)) (ensInt (ensSP? fbf)))
  244.   )
  245.  
  246. ; Q15
  247. (define (valide? fbf)
  248.   (andmap (lambda (i) (modele? i fbf)) (ensInt (ensSP? fbf)))
  249.   )
  250.  
  251. ; Q16
  252. (define (insatisfiable? fbf)
  253.   (not (andmap (lambda (i) (modele? i fbf)) (ensInt (ensSP? fbf)))
  254.        )
  255.   )
  256.  
  257. ; Q17
  258. (define (test2FbfAvecInterpretations fbf1 fbf2 i)
  259.   (cond ((null? i) #t)
  260.         ((eq? (valV fbf1 (car i)) (valV fbf2 (car i))) (test2FbfAvecInterpretations fbf1 fbf2 (cdr i)))
  261.         [else #f]
  262.         )
  263.   )
  264.  
  265. (define (equivalent1? f1 f2)
  266.   (test2FbfAvecInterpretations f1 f2 (ensInt (set-union (ensSP? f1) (ensSP? f2))))
  267.   )
  268.  
  269. (define (equivalent1v2? f1 f2)
  270.   (andmap (lambda (i)
  271.             (= (valV f1 i) (valV f2 i)))
  272.           (ensInt (set-union (ensSP? f1) (ensSP? f2)))
  273.           )
  274.   )
  275.  
  276. (define (equivalent2? f1 f2)
  277.   (valide? (list '<-> f1 f2))
  278.   )
  279.  
  280. ; Q18
  281. (define (consequence2? f g)
  282.   (andmap (lambda (i)
  283.             (if (and (eq? (valV f i) 1) (= (valV g i) 0)) #f #t)
  284.             )
  285.           (ensInt (set-union (ensSP? f) (ensSP? g)))
  286.           )
  287.   )
  288.  
  289. (define (consequence2v2? f g)
  290.   (insatisfiable? (list '^ f (list '! g)))
  291.   )
  292.  
  293. (define (consequence2v3? f g)
  294.   (valide? (list '-> f g))
  295.   )
  296.  
  297. ; Q19
  298. (define (ensSPallFbf eFbf)
  299.   (if (set-empty? eFbf) '()
  300.       (set-union (ensSP? (car eFbf))
  301.                  (ensSPallFbf (cdr eFbf))
  302.                  )
  303.       )
  304.   )
  305.  
  306. ; Q20
  307. (define (modeleCommun? i ensFBF)
  308.   (cond [(set-empty? ensFBF)]
  309.         [(modele? i (car ensFBF)) (modeleCommun? i (cdr ensFBF))]
  310.         [else #f]
  311.         )
  312.   )
  313. (define (modeleCommunv2? i ensFBF)
  314.   (andmap (lambda (fbf) (modele? i fbf)) ensFBF)
  315.   )
  316.  
  317. ; Q21
  318. (define (contradictoire? ensFBF)
  319.   (not (ormap (lambda (i) (modeleCommun? i ensFBF)) (ensInt (ensSPallFbf ensFBF))
  320.           )
  321.        )
  322.   )
  323. (define (contradictoirev2? ensFBF)
  324.   (andmap (lambda (i) (not (modeleCommun? i ensFBF))) (ensInt (ensSPallFbf ensFBF))
  325.           )
  326.   )
  327.  
  328. ; Q22
  329. (define (consequence? ensFBF f)
  330.   (let ((ensF (set-union ensFBF f)))
  331.     (ormap (lambda (i) (modeleCommun? i ensF)) (ensInt (set-union (ensSPallFbf ensFBF) (ensSP? f))))
  332.     )
  333.   )
  334.  
  335. ; Q23
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement