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