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 n_z = try Util.index_of "Z" 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
+ else if v = lev + n_z then `Var(lev, 0) (* FIXME why 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
;;\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 (name, div, conv, ps) line =\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 chr = ":"\r
- then line, div, conv, ps\r
- else if chr = "D"\r
- then name, line, conv, ps\r
- else if chr = "C"\r
- then name, div, line::conv, ps\r
- else if chr = "N"\r
- then name, div, conv, line::ps\r
- else raise (ParsingError ("Unexpected at beginning of line: " ^ chr)) in\r
- let name, div, conv, ps = List.fold_left aux ("?", "", [], []) lines in\r
+ let line = String.trim (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, name, div, conv, ps\r
+ else if chr = "$"\r
+ then "#", 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 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 ("#", "no label", "", [], []) lines in\r
let div_provided = div <> "" in\r
- let div = if div_provided then div else "BOT" in\r
+ let div = if div_provided then div else "xxxxxx" in\r
let strs = [div] @ ps @ conv in\r
\r
+ if strs = ["BOT"]\r
+ then raise (ParsingError "empty problem");\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 assert false\r
- else if v = lev + n_pac then assert false\r
- else if v = lev + n_bomb then assert false\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
+ | `Var(n,_) -> `Var(n,1)\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
let ps = List.map (\r
function\r
| #i_n_var as y -> y\r
- | _ -> raise (ParsingError "A term in num is not i_n_var")\r
+ | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y))\r
) ps in\r
-\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 "[\n\r]+\\$") txt in\r
+ List.map problem_of_string (List.tl (List.map ((^) "$") problems))\r
+;;\r