type bform = V of string | Neg of bform
             | And of bform * bform | Or of bform * bform

(* o formulă, (p/\q \/ r) /\ ~p *)
let f1 = And (Or (And (V "p", V "q"), V "r"), Neg (V "p"))
       
module M = Map.Make(String)
(* o interpretare *)
let interp1 = M.singleton "p" true |> M.add "r" false |>  M.add "q" true

let eval interp =
  let rec eval1 = function
    | V s -> M.find s interp
    | Neg f -> not (eval1 f)
    | And (f1, f2) -> eval1 f1 &&  eval1 f2
    | Or (f1, f2) -> eval1 f1 || eval1 f2
  in eval1
   
let t1 = eval interp1 f1

module S = Set.Make(String)

(* colectează toate propozițiile (variabilele propoziționale) dintr-o formulă *)
let rec allvars = function
  | V s -> S.singleton s
  | Neg f -> allvars f
  | And (f1, f2) | Or (f1, f2) -> S.union (allvars f1) (allvars f2)

let t2 = allvars f1 |> S.elements
   

This document was generated using caml2html