Advertisement
xavierm02

Untitled

Dec 12th, 2012
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.75 KB | None | 0 0
  1. (* sat.ml *)
  2. open BooleanExpression;;
  3.  
  4. let e = `And( [`Or([`Var( 1 ); `Var(3)]);`Var( 0 );`Var(3)] ) in
  5. print_string (string_of_boolean_expression e);;
  6. print_newline();
  7.  
  8. (* booleanExpression.ml *)
  9. type 'canonical boolean_expression = [
  10.   `Var of int |
  11.   `Not of 'canonical boolean_expression |
  12.   `And of 'canonical boolean_expression list |
  13.   `Or of 'canonical boolean_expression list
  14.   constraint 'canonical = [ < `Canonical | `Not_canonical ]
  15. ];;
  16.  
  17. let rec number_of_variables e =
  18.   match e with
  19.     | `Var i -> i
  20.     | `Not t -> number_of_variables t
  21.     | `And l | `Or l -> List.fold_left (fun m t -> max m (number_of_variables t)) 0 l
  22. ;;
  23.  
  24. let rec string_of_boolean_expression e =
  25.   let string_of_boolean_expression_list o l =
  26.     match l with
  27.       | [] -> ""
  28.       | h :: t ->
  29.         let rec loop l s =
  30.           match l with
  31.             | [] -> s
  32.             | h :: t ->
  33.               loop t (s ^ o ^ (string_of_boolean_expression h))
  34.         in "(" ^ (string_of_boolean_expression h) ^ (loop t "") ^ ")"
  35.   in
  36.   match e with
  37.     | `Var( i ) -> string_of_int i
  38.     | `Not( sub_e ) -> "¬" ^ (string_of_boolean_expression sub_e)
  39.     | `And l -> string_of_boolean_expression_list "∧" l
  40.     | `Or l -> string_of_boolean_expression_list "∨" l
  41. ;;
  42.  
  43. (* booleanExpression.mli *)
  44. type 'canonical boolean_expression constraint 'canonical = [< `Canonical | `Not_canonical ]
  45.  
  46. val string_of_boolean_expression : _ boolean_expression -> string
  47.  
  48. (* output *)
  49. File "sat.ml", line 4, characters 43-44:
  50. Error: This expression has type
  51.          [> `And of [> `Or of [> `Var of int ] list | `Var of int ] list ]
  52.        but an expression was expected of type
  53.          [< `Canonical | `Not_canonical ]
  54.          BooleanExpression.boolean_expression
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement