(* Question 1 *) let ou a b=if a then true else b;; let et a b=if a then b else false;; let implique a b=if a then b else true;; type variable == int;; type abd= Bool of bool | Test of variable*abd*abd;; (* Question 7 *) let abd_proj i=Test(i,Bool(true),Bool(false));; let rec abd_neg =function | Bool(b)->Bool(not(b)) | Test(v,g,d)->Test(v,abd_neg g,abd_neg d);; (* Question 8 *) let rec abd_egal a1 a2=match a1,a2 with | Bool(b1),Bool(b2) -> b1=b2 | Test(v1,g1,d1),Test(v2,g2,d2) -> (v1=v2)&&(abd_egal g1 g2)&&(abd_egal d1 d2) | _,_->false;; let rec abd_partiel i b=function | Bool(bb) -> Bool(bb) | Test(v,g,d)->match (v>i,v=i) with | (true,_)->Test(v,g,d) | (_,true)->if b then g else d | _->let (g1,d1)=(abd_partiel i b g,abd_partiel i b d) in if abd_egal g1 d1 then g1 else Test(v,g1,d1);; let rac=function Bool(_)->max_int|Test(i,_,_)->i;; let rec abd_test c v f=match c with | Bool(b)->if b then v else f | Test(i,gg,dd)->let m=min i (min (rac gg) (rac dd)) in let c0=abd_partiel m false c and c1=abd_partiel m true c and v0=abd_partiel m false v and v1=abd_partiel m true v and f0=abd_partiel m false f and f1=abd_partiel m true f in let g1=abd_test c1 v1 f1 and d1=abd_test c0 v0 f0 in if abd_egal g1 d1 then g1 else Test(m,g1,d1);; let rec abd_et f g=match (f,g) with | (Bool(true),_)->g | (Bool(false),_) | (_,Bool(false))->Bool(false) | (_,Bool(true))->f | (Test(i,g1,d1),Test(j,g2,d2))->let m=min i j in let g3=abd_et (abd_partiel m true f) (abd_partiel m true g) and d3=abd_et (abd_partiel m false f) (abd_partiel m false g) in if abd_egal g3 d3 then g3 else Test(m,g3,d3);; let rec abd_ou f g=match (f,g) with | (Bool(false),_)->g | (Bool(true),_) | (_,Bool(true))->Bool(true) | (_,Bool(false))->f | (Test(i,g1,d1),Test(j,g2,d2))->let m=min i j in let g3=abd_ou (abd_partiel m true f) (abd_partiel m true g) and d3=abd_ou (abd_partiel m false f) (abd_partiel m false g) in if abd_egal g3 d3 then g3 else Test(m,g3,d3);; (* let abd_ou f g=let (gg,dd)=(abd_neg f,abd_neg g) in abd_neg(abd_et gg dd);; fonctionne également... *) let abd_implique f g=abd_ou (abd_neg f) g;; (* Question 9 *) let v=abd_proj;; (* |v| = 1<6 = |abd_proj| *) let t1=abd_implique (abd_neg (v 0)) (v 1) and t2=abd_ou (abd_neg (v 1)) (v 2) and t3=abd_implique (v 3) (v 4) and t4=abd_neg (abd_et (v 0) (v 4)) and t5=abd_implique (v 2) (abd_et (v 0) (v 3));; let p=abd_et (abd_et (abd_et t1 t2) t3) (abd_et t4 t5);; let pa=abd_et p (abd_ou (abd_neg (v 0)) (v 2)) and pb=abd_ou p (v 2);; let pc=abd_ou (v (-1)) pb;; (* Question 10 *) let nom=[|"peluche";"petit";"vert";"electrique";"piles"|];; let abd_solutions a= let rec interne s i=function | Bool(false)->() | Bool(true)-> if i=5 then print_endline s else (interne (s^"("^nom.(i)^")") (i+1) (Bool(true)); interne (s^"(non "^nom.(i)^")") (i+1) (Bool(true)) ) | Test(j,g,d)->if i=j then begin interne (s^"("^nom.(i)^")") (i+1) g; interne (s^"(non "^nom.(i)^")") (i+1) d end else interne s i (Test(i,Test(j,g,d),Test(j,g,d))) in interne "" 0 a;; abd_solutions p;; abd_solutions pa;; abd_solutions pb;; abd_solutions (Bool(true));; (* *********************** TRACES D'EXÉCUTION *********************** *) > Caml Light version 0.74/Mac0.5 #cd "Disque dur:Desktop Folder:"; include "X99.ml";; ou : bool -> bool -> bool = et : bool -> bool -> bool = implique : bool -> bool -> bool = Le type variable est défini. Le type abd est défini. abd_proj : variable -> abd = abd_neg : abd -> abd = abd_egal : abd -> abd -> bool = abd_partiel : variable -> bool -> abd -> abd = rac : abd -> int = abd_test : abd -> abd -> abd -> abd = abd_et : abd -> abd -> abd = abd_ou : abd -> abd -> abd = abd_implique : abd -> abd -> abd = v : variable -> abd = t1 : abd = Test (0, Bool true, Test (1, Bool true, Bool false)) t2 : abd = Test (1, Test (2, Bool true, Bool false), Bool true) t3 : abd = Test (3, Test (4, Bool true, Bool false), Bool true) t4 : abd = Test (0, Test (4, Bool false, Bool true), Bool true) t5 : abd = Test (0, Test (2, Test (3, Bool true, Bool false), Bool true), Test (2, Bool false, Bool true)) p : abd = Test (0, Test (1, Bool false, Test (2, Bool false, Test (3, Bool false, Test (4, Bool false, Bool true)))), Bool false) pa : abd = Bool false pb : abd = Test (0, Test (1, Test (2, Bool true, Bool false), Test (2, Bool true, Test (3, Bool false, Test (4, Bool false, Bool true)))), Test (2, Bool true, Bool false)) pc : abd = Test (-1, Bool true, Test (0, Test (1, Test (2, Bool true, Bool false), Test (2, Bool true, Test (3, Bool false, Test (4, Bool false, Bool true)))), Test (2, Bool true, Bool false))) nom : string vect = [|"peluche"; "petit"; "vert"; "electrique"; "piles"|] abd_solutions : abd -> unit = (peluche)(non petit)(non vert)(non electrique)(non piles) - : unit = () - : unit = () (peluche)(petit)(vert)(electrique)(piles) (peluche)(petit)(vert)(electrique)(non piles) (peluche)(petit)(vert)(non electrique)(piles) (peluche)(petit)(vert)(non electrique)(non piles) (peluche)(non petit)(vert)(electrique)(piles) (peluche)(non petit)(vert)(electrique)(non piles) (peluche)(non petit)(vert)(non electrique)(piles) (peluche)(non petit)(vert)(non electrique)(non piles) (peluche)(non petit)(non vert)(non electrique)(non piles) (non peluche)(petit)(vert)(electrique)(piles) (non peluche)(petit)(vert)(electrique)(non piles) (non peluche)(petit)(vert)(non electrique)(piles) (non peluche)(petit)(vert)(non electrique)(non piles) (non peluche)(non petit)(vert)(electrique)(piles) (non peluche)(non petit)(vert)(electrique)(non piles) (non peluche)(non petit)(vert)(non electrique)(piles) (non peluche)(non petit)(vert)(non electrique)(non piles) - : unit = () (peluche)(petit)(vert)(electrique)(piles) (peluche)(petit)(vert)(electrique)(non piles) (peluche)(petit)(vert)(non electrique)(piles) ....... (non peluche)(non petit)(non vert)(electrique)(non piles) (non peluche)(non petit)(non vert)(non electrique)(piles) (non peluche)(non petit)(non vert)(non electrique)(non piles) - : unit = () - : unit = () #