]> matita.cs.unibo.it Git - fireball-separation.git/blobdiff - ocaml/parser.ml
Parser.from_file, and p* problems moved to problems/p
[fireball-separation.git] / ocaml / parser.ml
index b496070b690e9077d6b482774f06d0a8ab1c776b..379fb2edff3697e3e8c9740909223b1d705f61b6 100644 (file)
@@ -1,15 +1,9 @@
-type term =\r
-  | Var of int\r
-  | App of term * term\r
-  | Lam of term\r
-;;\r
-\r
-let mk_app x y = App(x, y);;\r
-let mk_lam x = Lam x;;\r
-let mk_var x = Var x;;\r
-\r
 exception ParsingError of string;;\r
 \r
+let mk_app x y = Num.mk_app x y;;\r
+let mk_lam x = `Lam(true, x);;\r
+let mk_var x = `Var(x, -666);;\r
+\r
 let isAlphaNum c = let n = Char.code c in\r
  (48 <= n && n <= 90) || (95 <= n && n <= 122) ;;\r
 let isSpace c = c = ' ' || c = '\n' || c = '\t' ;;\r
@@ -147,3 +141,123 @@ let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j")
 \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 `Bottom\r
+     else if v = lev + n_pac then `Pacman\r
+      else if v = lev + n_bomb then `Lam(true, `Bottom)\r
+       else `Var(v,1) in (* 1 by default when variable not applied *)\r
+   (* Fix arity *)\r
+   let open Num in\r
+   let exclude_bottom = function\r
+   | #nf_nob as t -> t\r
+   | `Bottom -> raise (ParsingError "Input term not in normal form") in\r
+   let rec aux_nob lev : nf_nob -> nf = function\r
+   | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> exclude_bottom (aux_nob lev t)) args)\r
+   | `Var(n,_) -> fix lev n\r
+   | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
+   | `Match _ | `N _ -> assert false\r
+   | `Pacman -> assert false\r
+   and aux lev : Num.nf -> Num.nf = function\r
+    | #nf_nob as t -> aux_nob lev t\r
+    | `Bottom -> 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 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 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 rec aux' chr line =\r
+   if chr = "#"\r
+    then chr, line, div, conv, ps\r
+   else if chr = "D"\r
+    then chr, name, line, conv, ps\r
+   else if chr = "C"\r
+    then chr, name, 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
+  aux' chr line in\r
+ let _, name, div, conv, ps = List.fold_left aux ("#", "?", "", [], []) lines in\r
+ let div_provided = div <> "" in\r
+ let div = if div_provided then div else "BOT" in\r
+ let strs = [div] @ ps @ conv in\r
+\r
+ (* parse' *)\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 `Bottom\r
+   else if v = lev + n_pac then `Pacman\r
+    else if v = lev + n_bomb then `Lam(true, `Bottom)\r
+     else `Var(v,1) in (* 1 by default when variable not applied *)\r
+ (* Fix arity *)\r
+ let open Num in\r
+ let exclude_bottom = function\r
+ | #nf_nob as t -> t\r
+ | `Bottom -> raise (ParsingError "Input term not in normal form") in\r
+ let rec aux_nob lev : nf_nob -> nf = function\r
+ | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> exclude_bottom (aux_nob lev t)) args)\r
+ | `Var(n,_) -> fix lev n\r
+ | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
+ | `Match _ | `N _ -> assert false\r
+ | `Pacman -> `Pacman\r
+ and aux lev : Num.nf -> Num.nf = function\r
+  | #nf_nob as t -> aux_nob lev t\r
+  | `Bottom -> assert false in\r
+let all_tms = List.map (aux 0) (tms :> Num.nf list) in\r
+\r
+(* problem_of *)\r
+let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in\r
+\r
+let div = if not div_provided || div = `Bottom\r
+ then None\r
+ else match div with\r
+  | `I _ as t -> Some t\r
+  | _ -> raise (ParsingError "div is not an inert or BOT in the initial problem") in\r
+let conv = Util.filter_map (\r
+ function\r
+ | #i_n_var as t -> Some t\r
+ | `Lam _ -> None\r
+ | _ -> raise (ParsingError "A term in conv is not i_n_var")\r
+ ) conv in\r
+let ps = List.map (\r
+ function\r
+  | #i_n_var as y -> y\r
+  | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y))\r
+ ) ps in\r
+ name, div, conv, ps, free\r
+;;\r
+\r
+\r
+let from_file path =\r
+ let lines = ref [] in\r
+ let chan = open_in path in\r
+ let _ = try\r
+  while true; do\r
+    lines := input_line chan :: !lines\r
+  done\r
+ 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
+;;\r