1 exception ParsingError of string;;
\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
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
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
20 let mk_app' = function
\r
21 | [] -> assert false
\r
22 | t :: ts -> List.fold_left mk_app t ts
\r
26 let len = String.length s in
\r
28 if i >= len || s.[i] = '#' then l else aux (i+1) (s.[i] :: l)
\r
29 in List.rev (aux 0 [])
\r
33 let res = Bytes.create (List.length l) in
\r
34 let rec aux i = function
\r
36 | c :: l -> Bytes.set res i c; aux (i + 1) l in
\r
40 let rec strip_spaces = function
\r
41 | c::cs when isSpace c -> strip_spaces cs
\r
46 let rec aux = function
\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
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
59 | None, y -> None, y
\r
60 | Some x, y -> Some (implode x), y
\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
71 let rec read_smt vars =
\r
72 let check_if_lambda cs = match read_var cs with
\r
74 | Some x, cs -> match strip_spaces cs with
\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
89 | _, _ -> assert false
\r
90 ) in let rec aux vars cs =
\r
91 match strip_spaces cs with
\r
92 | [] -> None, [], vars
\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
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
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
116 | [] -> None, [], vars
\r
117 | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError "Missing `)`")
\r
118 ) else assert false
\r
122 match read_smt ([],[]) (explode x) with
\r
123 | Some [y], [], _ -> y
\r
124 | _, _, _ -> assert false
\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
136 (**********************************************************************
\r
138 let rec string_of_term = function
\r
139 | Tvar n -> string_of_int n
\r
140 | Tapp(t1, t2) -> "(" ^ string_of_term t1 ^ " " ^ string_of_term t2 ^ ")"
\r
141 | Tlam(t1) -> "(\\" ^ string_of_term t1 ^ ")"
\r
144 let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j"));;
\r
147 *******************************************************************************)
\r
149 let problem_of_string s =
\r
150 let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in
\r
151 let head, lines = List.hd lines, List.tl lines in
\r
152 let name = String.trim (String.sub head 1 (String.length head - 1)) in
\r
153 let lines = List.filter ((<>) "") lines in
\r
154 let aux (last, div, conv, ps) line =
\r
155 let chr = String.sub line 0 1 in
\r
156 let line = String.trim (String.sub line 1 (String.length line - 1)) in
\r
157 if line = "" then chr, div, conv, ps else
\r
158 let rec aux' chr line =
\r
160 then chr, div, conv, ps
\r
162 then chr, line, conv, ps
\r
164 then chr, div, line::conv, ps
\r
166 then chr, div, conv, line::ps
\r
168 then aux' last line
\r
169 else raise (ParsingError
\r
170 ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in
\r
172 let _, div, conv, ps = List.fold_left aux ("#", "", [], []) lines in
\r
173 let div_provided = div <> "" in
\r
174 let div = if div_provided then div else "BOT" in
\r
175 let strs = [div] @ ps @ conv in
\r
177 if List.length ps = 0 && List.length conv = 0
\r
178 then raise (ParsingError "Parsed empty problem");
\r
181 let (tms, free) = parse_many strs in
\r
182 (* Replace pacmans and bottoms *)
\r
183 let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in
\r
184 let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in
\r
185 let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in
\r
187 if v = lev + n_bot then `Bottom
\r
188 else if v = lev + n_pac then `Pacman
\r
189 else if v = lev + n_bomb then `Lam(true, `Bottom)
\r
190 else if v = lev then `Var(v, min_int) (* zero *)
\r
191 else `Var(v,1) in (* 1 by default when variable not applied *)
\r
194 let exclude_bottom = function
\r
195 | #nf_nob as t -> t
\r
196 (* actually, the following may be assert false *)
\r
197 | `Bottom -> raise (ParsingError "Input term not in normal form") in
\r
198 let rec aux_nob lev : nf_nob -> nf = function
\r
199 | `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
200 | `Var(n,_) -> fix lev n
\r
201 | `Lam(_,t) -> `Lam (true, aux (lev+1) t)
\r
202 | `Match _ | `N _ -> assert false
\r
203 | `Pacman -> `Pacman
\r
204 and aux lev : Num.nf -> Num.nf = function
\r
205 | #nf_nob as t -> aux_nob lev t
\r
206 | `Bottom -> assert false in
\r
207 let all_tms = List.map (aux 0) (tms :> Num.nf list) in
\r
210 let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in
\r
212 let div = if not div_provided || div = `Bottom
\r
214 else match div with
\r
215 | `I _ as t -> Some t
\r
216 | _ -> raise (ParsingError "D is not an inert in the initial problem") in
\r
217 let conv = Util.filter_map (
\r
219 | #i_n_var as t -> Some t
\r
221 | _ -> raise (ParsingError "A C-term is not i_n_var")
\r
223 let ps = List.map (
\r
225 | #i_n_var as y -> y
\r
226 | _ -> raise (ParsingError "An N-term is not i_n_var")
\r
228 name, div, conv, ps, free
\r
232 let from_file path =
\r
233 let lines = ref ["#"] in
\r
234 let chan = open_in path in
\r
237 lines := input_line chan :: !lines
\r
239 with End_of_file ->
\r
241 let txt = String.concat "\n" (List.rev !lines) in
\r
242 let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in
\r
243 List.map problem_of_string (List.tl (List.map ((^) "$") problems))
\r