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