]> matita.cs.unibo.it Git - fireball-separation.git/blobdiff - ocaml/parser.ml
Simplified arity_of, precompute_edible_data, critical_showsteppers
[fireball-separation.git] / ocaml / parser.ml
index a1a9d95a7dd85f91545009d81c132cfbc49676ef..79650955de2f5c5f7fa73606dee2e68a9613f73d 100644 (file)
@@ -118,86 +118,62 @@ let parse x =
   | _, _, _ -> raise (ParsingError "???")\r
 ;;\r
 \r
-let var_zero = "ω";; (* w is an invalid variable name *)\r
-\r
 let parse_many strs =\r
   let f (x, y) z = match read_smt y (explode z) with\r
   | Some[tm], [], vars -> (tm :: x, vars)\r
   | _, _, _ -> raise (ParsingError "???")\r
-  in let aux = List.fold_left f ([], ([], [var_zero])) (* index zero is reserved *)\r
+  in let aux = List.fold_left f ([], ([], [])) (* index zero is reserved *)\r
   in let (tms, (_, free)) = aux strs\r
   in (List.rev tms, free)\r
 ;;\r
 \r
-(**********************************************************************\r
-\r
-let rec string_of_term = function\r
-  | Tvar n -> string_of_int n\r
-  | Tapp(t1, t2) -> "(" ^ string_of_term t1 ^ " " ^ string_of_term t2 ^ ")"\r
-  | Tlam(t1) -> "(\\" ^ string_of_term t1 ^ ")"\r
-;;\r
-\r
-let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j"));;\r
-\r
-\r
-**********************************************************************)\r
-\r
- let parse' strs =\r
-   let (tms, free) = parse_many strs in\r
-   (* Replace pacmans and bottoms *)\r
-   let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in\r
-   let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in\r
-   let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in\r
-   let fix lev v =\r
-    if v = lev + n_bot then failwith "Example with `Bottom"\r
-     else if v = lev + n_pac then failwith "Example with `Pacman"\r
-      else if v = lev + n_bomb then failwith "Example with `Bomb"\r
-       else `Var(v,1) in (* 1 by default when variable not applied *)\r
-   (* Fix arity *)\r
-   let open Num in\r
-   let rec aux lev : nf -> nf = function\r
-   | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> aux lev t) args)\r
-   | `Var(n,_) -> fix lev n\r
-   | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
-   | `Match _ | `N _ -> assert false in\r
- List.map (aux 0) (tms :> Num.nf list), free\r
-;;\r
-\r
 let problem_of_string s =\r
-prerr_endline (s);\r
  let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in\r
- let lines = List.map String.trim lines in\r
+ let head, lines = List.hd lines, List.tl lines in\r
+ let name = String.trim (String.sub head 1 (String.length head - 1)) in\r
  let lines = List.filter ((<>) "") lines in\r
- prerr_endline("number of lines" ^ string_of_int (List.length lines));\r
- let aux (last, name, div, conv, ps) line =\r
+ let aux (last, div, conv, ps) line =\r
   let chr = String.sub line 0 1 in\r
-  let line = String.sub line 1 (String.length line - 1) in\r
-  if line = "" then chr, name, div, conv, ps else\r
+  let line = String.trim (String.sub line 1 (String.length line - 1)) in\r
+  if line = "" then chr, div, conv, ps else\r
   let rec aux' chr line =\r
    if chr = "#"\r
-    then chr, line, div, conv, ps\r
+    then chr, div, conv, ps\r
    else if chr = "D"\r
-    then chr, name, line, conv, ps\r
+    then chr, line, conv, ps\r
    else if chr = "C"\r
-    then chr, name, div, line::conv, ps\r
+    then chr, div, line::conv, ps\r
    else if chr = "N"\r
-    then chr, name, div, conv, line::ps\r
-   else if last = ""\r
-    then raise (ParsingError ("Unexpected at beginning of problem: " ^ chr))\r
-   else aux' last (chr ^ line) in\r
+    then chr, div, conv, line::ps\r
+   else if chr = " "\r
+    then aux' last line\r
+   else raise (ParsingError\r
+     ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in\r
   aux' chr line in\r
- let _, name, div, conv, ps = List.fold_left aux ("#", "?", "", [], []) lines in\r
+ let _, div, conv, ps = List.fold_left aux ("#", "", [], []) lines in\r
  let div_provided = div <> "" in\r
  let div = if div_provided then div else "xxxxxx" in\r
  let strs = [div] @ ps @ conv in\r
 \r
+ if List.length ps = 0 && List.length conv = 0\r
+  then raise (ParsingError "empty problem");\r
+\r
  (* parse' *)\r
  let (tms, free) = parse_many strs in\r
+ let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in\r
+ let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in\r
+ let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in\r
+ let fix lev v =\r
+ if v = lev + n_bot then failwith "Example with `Bottom"\r
+  else if v = lev + n_pac then failwith "Example with `Pacman"\r
+   else if v = lev + n_bomb then failwith "Example with `Bomb"\r
+    else if v = lev then `Var(v, min_int) (* zero *)\r
+      else `Var(v,1) in (* 1 by default when variable not applied *)\r
  (* Fix arity *)\r
  let open Num in\r
  let rec aux lev : nf -> nf = function\r
- | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> aux lev t) args)\r
- | `Var(n,_) -> `Var(n,1)\r
+ | `I((n,_), args) -> `I((n,(if lev = 0 then 0 else 1) + Listx.length args), Listx.map (aux lev) args)\r
+ | `Var(n,_) -> fix lev n\r
  | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
  | `Match _ | `N _ -> assert false in\r
 let all_tms = List.map (aux 0) (tms :> Num.nf list) in\r
@@ -226,7 +202,7 @@ let ps = List.map (
 \r
 \r
 let from_file path =\r
- let lines = ref [] in\r
+ let lines = ref ["#"] in\r
  let chan = open_in path in\r
  let _ = try\r
   while true; do\r
@@ -235,6 +211,6 @@ let from_file path =
  with End_of_file ->\r
   close_in chan in\r
  let txt = String.concat "\n" (List.rev !lines) in\r
- let problems = Str.split (Str.regexp "\r?\n\r?\n\\#") txt in\r
- List.map problem_of_string problems\r
+ let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in\r
+ List.map problem_of_string (List.tl (List.map ((^) "$") problems))\r
 ;;\r