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
11 exception ParsingError of string;;
\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
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
26 let mk_app' = function
\r
27 | [] -> assert false
\r
28 | t :: ts -> List.fold_left mk_app t ts
\r
32 let len = String.length s in
\r
34 if i >= len || s.[i] = '#' then l else aux (i+1) (s.[i] :: l)
\r
35 in List.rev (aux 0 [])
\r
39 let res = String.create (List.length l) in
\r
40 let rec aux i = function
\r
42 | c :: l -> String.set res i c; aux (i + 1) l in
\r
46 let rec strip_spaces = function
\r
47 | c::cs when isSpace c -> strip_spaces cs
\r
52 let rec aux = function
\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
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
65 | None, y -> None, y
\r
66 | Some x, y -> Some (implode x), y
\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
77 let rec read_smt vars =
\r
78 let check_if_lambda cs = match read_var cs with
\r
80 | Some x, cs -> match strip_spaces cs with
\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
95 | _, _ -> assert false
\r
96 ) in let rec aux vars cs =
\r
97 match strip_spaces cs with
\r
98 | [] -> None, [], vars
\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
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
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
122 | [] -> None, [], vars
\r
123 | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError "Missing `)`")
\r
124 ) else assert false
\r
128 match read_smt ([],[]) (explode x) with
\r
129 | Some [y], [], _ -> y
\r
130 | _, _, _ -> assert false
\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
142 (**********************************************************************
\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
150 let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j"));;
\r
153 *******************************************************************************)
\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
166 then chr, div, conv, ps
\r
168 then chr, line, conv, ps
\r
170 then chr, div, line::conv, ps
\r
172 then chr, div, conv, line::ps
\r
174 then aux' last line
\r
175 else raise (ParsingError
\r
176 ("Unexpected at beginning of problem: `" ^ 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
183 if List.length ps = 0 && List.length conv = 0
\r
184 then raise (ParsingError "Parsed empty problem");
\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
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
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
216 let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in
\r
218 let div = if not div_provided || div = `Bottom
\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
225 | #i_n_var as t -> Some t
\r
227 | _ -> raise (ParsingError "A C-term is not i_n_var")
\r
229 let ps = List.map (
\r
231 | #i_n_var as y -> y
\r
232 | _ -> raise (ParsingError "An N-term is not i_n_var")
\r
234 name, div, conv, ps, free
\r
238 let from_file path =
\r
239 let lines = ref ["#"] in
\r
240 let chan = open_in path in
\r
243 lines := input_line chan :: !lines
\r
245 with End_of_file ->
\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
253 match read_smt ([],[]) (explode x) with
\r
254 | Some [y], [], _ -> y
\r
255 | _, _, _ -> assert false
\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