]> matita.cs.unibo.it Git - fireball-separation.git/blob - ocaml/parser.ml
Fixed computation of arity for top-level inerts
[fireball-separation.git] / ocaml / parser.ml
1 exception ParsingError of string;;\r
2 \r
3 let mk_app x y = Num.mk_app x y;;\r
4 let mk_lam x = `Lam(true, x);;\r
5 let mk_var x = `Var(x, -666);;\r
6 \r
7 let isAlphaNum c = let n = Char.code c in\r
8  (48 <= n && n <= 90) || (95 <= n && n <= 122) ;;\r
9 let isSpace c = c = ' ' || c = '\n' || c = '\t' ;;\r
10 \r
11 (* FIXME *)\r
12 let mk_var' (bound, free) x =\r
13   if List.mem x bound\r
14   then free, mk_var (Util.index_of x bound)\r
15   else if List.mem x free\r
16        then free, mk_var (List.length bound + Util.index_of x free)\r
17        else (free @ [x]), mk_var (List.length bound + List.length free)\r
18 ;;\r
19 \r
20 let mk_app' = function\r
21   | [] ->  raise (ParsingError "bug")\r
22   | t :: ts -> List.fold_left mk_app t ts\r
23 ;;\r
24 \r
25 let explode s =\r
26   let rec aux i l =\r
27     if i < 0 then l else aux (i - 1) (s.[i] :: l)\r
28   in aux (String.length s - 1) []\r
29 ;;\r
30 \r
31 let implode l =\r
32   let res = Bytes.create (List.length l) in\r
33   let rec aux i = function\r
34     | [] -> res\r
35     | c :: l -> Bytes.set res i c; aux (i + 1) l in\r
36   aux 0 l\r
37 ;;\r
38 \r
39 let rec strip_spaces = function\r
40   | c::cs when isSpace c -> strip_spaces cs\r
41   | cs -> cs\r
42 ;;\r
43 \r
44 let read_var s =\r
45   let rec aux = function\r
46   | [] -> None, []\r
47   | c::cs as x -> if isAlphaNum c\r
48     then match aux cs with\r
49          | (Some x), cs' -> Some (c :: x), cs'\r
50          | None, cs' -> (Some [c]), cs'\r
51     else None, x\r
52   in match aux s with\r
53     | None, y -> None, y\r
54     | Some x, y -> Some (implode x), y\r
55 ;;\r
56 \r
57 let read_var' (bound, free as vars) s =\r
58   match read_var s with\r
59   | Some varname, cs ->\r
60     let free, v = mk_var' vars varname in\r
61     Some [v], cs, (bound, free)\r
62   | _, _ -> raise (ParsingError ("Can't read variable"))\r
63 ;;\r
64 \r
65 let rec read_smt vars =\r
66   let check_if_lambda cs = match read_var cs with\r
67     | None, _ -> false\r
68     | Some x, cs -> match strip_spaces cs with\r
69       | [] -> false\r
70       | c::_ -> c = '.'\r
71   in let read_lambda (bound, free) cs = (\r
72     match read_var (strip_spaces cs) with\r
73       | Some varname, cs ->\r
74       let vars' = (varname::bound, free) in\r
75       (match strip_spaces cs with\r
76         | [] -> raise (ParsingError "manca dopo variabile lambda")\r
77         | c::cs -> (if c = '.' then (match read_smt vars' cs with\r
78           | None, _, _ -> raise (ParsingError "manca corpo lambda")\r
79           | Some [x], y, (_, free) -> Some [mk_lam x], y, (bound, free)\r
80           | Some _, _, _ -> raise (ParsingError "???")\r
81           ) else raise (ParsingError "manca . nel lambda")\r
82         ))\r
83       | _, _ -> assert false\r
84  ) in let rec aux vars cs =\r
85   match strip_spaces cs with\r
86   | [] -> None, [], vars\r
87   | c::_ as x ->\r
88       let tms, cs, vars = (\r
89            if c = '(' then read_pars vars x\r
90       else if c = ')' then (None, x, vars)\r
91       else if check_if_lambda x then read_lambda vars x\r
92       else read_var' vars x) in\r
93       match tms with\r
94       | Some [tm] -> (\r
95         match aux vars cs with\r
96           | None, cs, vars -> Some [tm], cs, vars\r
97           | Some ts, cs, vars -> Some (tm :: ts), cs, vars\r
98         )\r
99       | Some _ -> raise (ParsingError "bug")\r
100       | None -> None, x, vars\r
101   in fun cs -> match aux vars cs with\r
102     | None, cs, vars -> None, cs, vars\r
103     | Some ts, cs, vars -> Some [mk_app' ts], cs, vars\r
104 and read_pars vars = function\r
105   | [] -> None, [], vars\r
106   | c::cs -> if c = '(' then (\r
107     let tm, cs, vars = read_smt vars cs in\r
108     let cs = strip_spaces cs in\r
109     match cs with\r
110       | [] -> None, [], vars\r
111       | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError ") mancante")\r
112     ) else raise (ParsingError "???")\r
113 ;;\r
114 \r
115 let parse x =\r
116   match read_smt ([],[]) (explode x) with\r
117   | Some [y], [], _ -> y\r
118   | _, _, _ -> raise (ParsingError "???")\r
119 ;;\r
120 \r
121 let parse_many strs =\r
122   let f (x, y) z = match read_smt y (explode z) with\r
123   | Some[tm], [], vars -> (tm :: x, vars)\r
124   | _, _, _ -> raise (ParsingError "???")\r
125   in let aux = List.fold_left f ([], ([], [])) (* index zero is reserved *)\r
126   in let (tms, (_, free)) = aux strs\r
127   in (List.rev tms, free)\r
128 ;;\r
129 \r
130 (**********************************************************************\r
131 \r
132 let rec string_of_term = function\r
133   | Tvar n -> string_of_int n\r
134   | Tapp(t1, t2) -> "(" ^ string_of_term t1 ^ " " ^ string_of_term t2 ^ ")"\r
135   | Tlam(t1) -> "(\\" ^ string_of_term t1 ^ ")"\r
136 ;;\r
137 \r
138 let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j"));;\r
139 \r
140 \r
141 *******************************************************************************)\r
142 \r
143 let problem_of_string s =\r
144  let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in\r
145  let head, lines = List.hd lines, List.tl lines in\r
146  let name = String.trim (String.sub head 1 (String.length head - 1)) in\r
147  let lines = List.filter ((<>) "") lines in\r
148  let aux (last, div, conv, ps) line =\r
149   let chr = String.sub line 0 1 in\r
150   let line = String.trim (String.sub line 1 (String.length line - 1)) in\r
151   if line = "" then chr, div, conv, ps else\r
152   let rec aux' chr line =\r
153    if chr = "#"\r
154     then chr, div, conv, ps\r
155    else if chr = "D"\r
156     then chr, line, conv, ps\r
157    else if chr = "C"\r
158     then chr, div, line::conv, ps\r
159    else if chr = "N"\r
160     then chr, div, conv, line::ps\r
161    else if chr = " "\r
162     then aux' last line\r
163    else raise (ParsingError\r
164      ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in\r
165   aux' chr line in\r
166  let _, div, conv, ps = List.fold_left aux ("#", "", [], []) lines in\r
167  let div_provided = div <> "" in\r
168  let div = if div_provided then div else "BOT" in\r
169  let strs = [div] @ ps @ conv in\r
170 \r
171  if List.length ps = 0 && List.length conv = 0\r
172   then raise (ParsingError "empty problem");\r
173 \r
174  (* parse' *)\r
175  let (tms, free) = parse_many strs in\r
176  (* Replace pacmans and bottoms *)\r
177  let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in\r
178  let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in\r
179  let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in\r
180  let fix lev v =\r
181   if v = lev + n_bot then `Bottom\r
182    else if v = lev + n_pac then `Pacman\r
183     else if v = lev + n_bomb then `Lam(true, `Bottom)\r
184     else if v = lev then `Var(v, min_int) (* zero *)\r
185      else `Var(v,1) in (* 1 by default when variable not applied *)\r
186  (* Fix arity *)\r
187  let open Num in\r
188  let exclude_bottom = function\r
189  | #nf_nob as t -> t\r
190  | `Bottom -> raise (ParsingError "Input term not in normal form") in\r
191  let rec aux_nob lev : nf_nob -> nf = function\r
192  | `I((n,_), args) -> `I((n,(if lev = 0 then 0 else 1) + Listx.length args), Listx.map (fun t -> exclude_bottom (aux_nob lev t)) args)\r
193  | `Var(n,_) -> fix lev n\r
194  | `Lam(_,t) -> `Lam (true, aux (lev+1) t)\r
195  | `Match _ | `N _ -> assert false\r
196  | `Pacman -> `Pacman\r
197  and aux lev : Num.nf -> Num.nf = function\r
198   | #nf_nob as t -> aux_nob lev t\r
199   | `Bottom -> assert false in\r
200 let all_tms = List.map (aux 0) (tms :> Num.nf list) in\r
201 \r
202 (* problem_of *)\r
203 let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in\r
204 \r
205 let div = if not div_provided || div = `Bottom\r
206  then None\r
207  else match div with\r
208   | `I _ as t -> Some t\r
209   | _ -> raise (ParsingError "div is not an inert or BOT in the initial problem") in\r
210 let conv = Util.filter_map (\r
211  function\r
212  | #i_n_var as t -> Some t\r
213  | `Lam _ -> None\r
214  | _ -> raise (ParsingError "A term in conv is not i_n_var")\r
215  ) conv in\r
216 let ps = List.map (\r
217  function\r
218   | #i_n_var as y -> y\r
219   | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y))\r
220  ) ps in\r
221  name, div, conv, ps, free\r
222 ;;\r
223 \r
224 \r
225 let from_file path =\r
226  let lines = ref ["#"] in\r
227  let chan = open_in path in\r
228  let _ = try\r
229   while true; do\r
230     lines := input_line chan :: !lines\r
231   done\r
232  with End_of_file ->\r
233   close_in chan in\r
234  let txt = String.concat "\n" (List.rev !lines) in\r
235  let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in\r
236  List.map problem_of_string (List.tl (List.map ((^) "$") problems))\r
237 ;;\r