module M = Map.Make(String)
open Scanf

(* Exp ::= sir | ( sir EList )            EList ::= eps | Exp Elist *)
type expr = Atom of string | T of string * expr list

(* citeste o s-expresie reprezentand o formula propozitionala
 * de ex. (and (or p q) (=> a (not b))) *)
let rec rd_expr () =
  try scanf " ( %s" (fun s -> T(s, rd_elist())) (* dupa ( vine sir si lista *)
  with Scan_failure _ -> scanf " %[^ \t()]" (fun s -> Atom s) (* sau doar sir *)
and rd_elist () =        (* lista vida sau expresie urmata de lista *)
  try scanf " )" (); []        (* daca apare ) gata, returneaza lista vida *)
  with Scan_failure _ -> let e = rd_expr() in e :: rd_elist()

let rec print_expr = function
  | Atom s -> print_string s
  | T (s, elst) -> print_char '('; print_string s;
                   List.iter (fun e -> print_char ' '; print_expr e) elst;
                   print_char ')'

let eval_d dict =        (* evalueaza formula avand dictionar de valori *)
  let rec imply = function
    | [] -> failwith "needs operand"
    | [e] -> eval e
    | h :: t -> not (eval h) || imply t
  and eval = function        (* implicit fals *)
    | Atom s -> (try M.find s dict with Not_found -> false)
    | T (op, elst) -> match op with
                      | "and" -> List.for_all eval elst
                      | "or" -> List.exists eval elst
                      | "not" -> (match elst with
                                 | [e] -> not (eval e)
                                 | _ -> failwith "not is unary")
                      | "=>" -> imply elst
                      | _ -> failwith "unknown operator"
  in eval

(* defineste propozitiile adevarate, restul sunt false *)
let dict = M.add "a" true (M.add "c" true (M.add "p" true
                                                 (M.singleton "r" true)))
let () = let e = rd_expr() in
         print_expr e; print_newline();
         print_endline (string_of_bool (eval_d dict e))

This document was generated using caml2html