X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fparser.ml;h=379fb2edff3697e3e8c9740909223b1d705f61b6;hb=032edbca0cd6654adae5b5f06620723e74847435;hp=4c7b829b91d5669c65598b3cc6443b9e0e2d281c;hpb=e4aa4a66dd0a4946607245a0f43eab803f2770c4;p=fireball-separation.git diff --git a/ocaml/parser.ml b/ocaml/parser.ml index 4c7b829..379fb2e 100644 --- a/ocaml/parser.ml +++ b/ocaml/parser.ml @@ -1,29 +1,19 @@ -type term = - | Var of int - | App of term * term - | Lam of term -;; - -let mk_app x y = App(x, y);; -let mk_lam x = Lam x;; -let mk_var x = Var x;; - exception ParsingError of string;; -let isAlphaNum c = let n = Char.code c in 48 <= n && n <= 122 ;; +let mk_app x y = Num.mk_app x y;; +let mk_lam x = `Lam(true, x);; +let mk_var x = `Var(x, -666);; + +let isAlphaNum c = let n = Char.code c in + (48 <= n && n <= 90) || (95 <= n && n <= 122) ;; let isSpace c = c = ' ' || c = '\n' || c = '\t' ;; -let rec index_of x = - function - | [] -> raise (Failure "index_of: Not Found") - | h::t -> if x = h then 0 else 1 + index_of x t -;; (* FIXME *) let mk_var' (bound, free) x = if List.mem x bound - then free, mk_var (index_of x bound) + then free, mk_var (Util.index_of x bound) else if List.mem x free - then free, mk_var (List.length bound + index_of x free) + then free, mk_var (List.length bound + Util.index_of x free) else (free @ [x]), mk_var (List.length bound + List.length free) ;; @@ -151,3 +141,123 @@ let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j") **********************************************************************) + + let parse' strs = + let (tms, free) = parse_many strs in + (* Replace pacmans and bottoms *) + let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in + let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in + let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in + let fix lev v = + if v = lev + n_bot then `Bottom + else if v = lev + n_pac then `Pacman + else if v = lev + n_bomb then `Lam(true, `Bottom) + else `Var(v,1) in (* 1 by default when variable not applied *) + (* Fix arity *) + let open Num in + let exclude_bottom = function + | #nf_nob as t -> t + | `Bottom -> raise (ParsingError "Input term not in normal form") in + let rec aux_nob lev : nf_nob -> nf = function + | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> exclude_bottom (aux_nob lev t)) args) + | `Var(n,_) -> fix lev n + | `Lam(_,t) -> `Lam (true, aux (lev+1) t) + | `Match _ | `N _ -> assert false + | `Pacman -> assert false + and aux lev : Num.nf -> Num.nf = function + | #nf_nob as t -> aux_nob lev t + | `Bottom -> assert false in + List.map (aux 0) (tms :> Num.nf list), free +;; + +let problem_of_string s = +prerr_endline (s); + let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in + let lines = List.map String.trim lines in + let lines = List.filter ((<>) "") lines in + prerr_endline("number of lines" ^ string_of_int (List.length lines)); + let aux (last, name, div, conv, ps) line = + let chr = String.sub line 0 1 in + let line = String.sub line 1 (String.length line - 1) in + if line = "" then chr, name, div, conv, ps else + let rec aux' chr line = + if chr = "#" + then chr, line, div, conv, ps + else if chr = "D" + then chr, name, line, conv, ps + else if chr = "C" + then chr, name, div, line::conv, ps + else if chr = "N" + then chr, name, div, conv, line::ps + else if last = "" + then raise (ParsingError ("Unexpected at beginning of problem: " ^ chr)) + else aux' last (chr ^ line) in + aux' chr line in + let _, name, div, conv, ps = List.fold_left aux ("#", "?", "", [], []) lines in + let div_provided = div <> "" in + let div = if div_provided then div else "BOT" in + let strs = [div] @ ps @ conv in + + (* parse' *) + let (tms, free) = parse_many strs in + (* Replace pacmans and bottoms *) + let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in + let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in + let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in + let fix lev v = + if v = lev + n_bot then `Bottom + else if v = lev + n_pac then `Pacman + else if v = lev + n_bomb then `Lam(true, `Bottom) + else `Var(v,1) in (* 1 by default when variable not applied *) + (* Fix arity *) + let open Num in + let exclude_bottom = function + | #nf_nob as t -> t + | `Bottom -> raise (ParsingError "Input term not in normal form") in + let rec aux_nob lev : nf_nob -> nf = function + | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> exclude_bottom (aux_nob lev t)) args) + | `Var(n,_) -> fix lev n + | `Lam(_,t) -> `Lam (true, aux (lev+1) t) + | `Match _ | `N _ -> assert false + | `Pacman -> `Pacman + and aux lev : Num.nf -> Num.nf = function + | #nf_nob as t -> aux_nob lev t + | `Bottom -> assert false in +let all_tms = List.map (aux 0) (tms :> Num.nf list) in + +(* problem_of *) +let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in + +let div = if not div_provided || div = `Bottom + then None + else match div with + | `I _ as t -> Some t + | _ -> raise (ParsingError "div is not an inert or BOT in the initial problem") in +let conv = Util.filter_map ( + function + | #i_n_var as t -> Some t + | `Lam _ -> None + | _ -> raise (ParsingError "A term in conv is not i_n_var") + ) conv in +let ps = List.map ( + function + | #i_n_var as y -> y + | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y)) + ) ps in + name, div, conv, ps, free +;; + + +let from_file path = + let lines = ref [] in + let chan = open_in path in + let _ = try + while true; do + lines := input_line chan :: !lines + done + with End_of_file -> + close_in chan in + let txt = String.concat "\n" (List.rev !lines) in + let problems = Str.split (Str.regexp "\r?\n\r?\n\\#") txt in + List.map problem_of_string problems +;;