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