tp5.ml 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. type formule =
  2. | Var of string
  3. | Neg of formule
  4. | Et of formule * formule
  5. | Ou of formule * formule
  6. ;;
  7. let ex = Et(Var "p", Neg(Ou(Var "q", Var "r")));;
  8. let rec string_of_formule formule = match formule with
  9. | Var (s) -> "s"
  10. | Neg (f) -> "neg" ^ (string_of_formule f)
  11. | Et (f1,f2) -> (string_of_formule f1) ^ "et" ^ (string_of_formule f2)
  12. | Ou (f1,f2) -> (string_of_formule f1) ^ "ou" ^ (string_of_formule f2)
  13. ;;
  14. let rec reunion l1 l2 = match (l1,l2) with
  15. | ([],[]) -> []
  16. | l1,[] -> l1
  17. | ([],l2) -> l2
  18. | t1::q1,l2 -> if not (List.mem t1 l2) then t1::(reunion q1 l2)
  19. else (reunion q1 l2)
  20. ;;
  21. let rec list_of_vars formule acc = match formule with
  22. | Var x -> reunion [x] acc
  23. | Neg (f) -> list_of_vars f acc
  24. | Et (f1,f2) -> reunion (list_of_vars f1 acc) (list_of_vars f2 acc)
  25. | Ou (f1,f2) -> reunion (list_of_vars f1 acc) (list_of_vars f2 acc)
  26. ;;
  27. let rec eval_formule formule machin = match formule with
  28. | Var x -> List.assoc x machin == true
  29. | Neg (f) -> eval_formule f machin == false
  30. | Et (f1,f2) -> (eval_formule f1 machin) && (eval_formule f2 machin)
  31. | Ou (f1,f2) -> (eval_formule f1 machin) || (eval_formule f2 machin)
  32. ;;
  33. let ex2 = Et(Var "p", Neg(Ou(Var "q", Var "r")));;
  34. let ex3 = [("p",true); ("q",false); ("r", true)];;
  35. let add_to_all a truc = List.map (function l0 ->a::l0) truc;;
  36. let rec affectation_vars sl = match sl with
  37. | [] -> []
  38. | t::q -> (affectation_vars q)@[(add_to_all t q)@(add_to_all (t,false))]
  39. ;;