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
14 then free, mk_var (Util.index_of x bound)
\r
15 else if 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 | [] -> raise (ParsingError "bug")
\r
22 | t :: ts -> List.fold_left mk_app t ts
\r
27 if i < 0 then l else aux (i - 1) (s.[i] :: l)
\r
28 in aux (String.length s - 1) []
\r
32 let res = String.create (List.length l) in
\r
33 let rec aux i = function
\r
35 | c :: l -> res.[i] <- c; aux (i + 1) l in
\r
39 let rec strip_spaces = function
\r
40 | c::cs when isSpace c -> strip_spaces cs
\r
45 let rec aux = function
\r
47 | c::cs as x -> if isAlphaNum c
\r
48 then match aux cs with
\r
49 | (Some x), cs' -> Some (c :: x), cs'
\r
50 | None, cs' -> (Some [c]), cs'
\r
53 | None, y -> None, y
\r
54 | Some x, y -> Some (implode x), y
\r
57 let read_var' (bound, free as vars) s =
\r
58 match read_var s with
\r
59 | Some varname, cs ->
\r
60 let free, v = mk_var' vars varname in
\r
61 Some [v], cs, (bound, free)
\r
62 | _, _ -> raise (ParsingError ("Can't read variable"))
\r
65 let rec read_smt vars =
\r
66 let check_if_lambda cs = match read_var cs with
\r
68 | Some x, cs -> match strip_spaces cs with
\r
71 in let read_lambda (bound, free) cs = (
\r
72 match read_var (strip_spaces cs) with
\r
73 | Some varname, cs ->
\r
74 let vars' = (varname::bound, free) in
\r
75 (match strip_spaces cs with
\r
76 | [] -> raise (ParsingError "manca dopo variabile lambda")
\r
77 | c::cs -> (if c = '.' then (match read_smt vars' cs with
\r
78 | None, _, _ -> raise (ParsingError "manca corpo lambda")
\r
79 | Some [x], y, (_, free) -> Some [mk_lam x], y, (bound, free)
\r
80 | Some _, _, _ -> raise (ParsingError "???")
\r
81 ) else raise (ParsingError "manca . nel lambda")
\r
83 | _, _ -> assert false
\r
84 ) in let rec aux vars cs =
\r
85 match strip_spaces cs with
\r
86 | [] -> None, [], vars
\r
88 let tms, cs, vars = (
\r
89 if c = '(' then read_pars vars x
\r
90 else if c = ')' then (None, x, vars)
\r
91 else if check_if_lambda x then read_lambda vars x
\r
92 else read_var' vars x) in
\r
95 match aux vars cs with
\r
96 | None, cs, vars -> Some [tm], cs, vars
\r
97 | Some ts, cs, vars -> Some (tm :: ts), cs, vars
\r
99 | Some _ -> raise (ParsingError "bug")
\r
100 | None -> None, x, vars
\r
101 in fun cs -> match aux vars cs with
\r
102 | None, cs, vars -> None, cs, vars
\r
103 | Some ts, cs, vars -> Some [mk_app' ts], cs, vars
\r
104 and read_pars vars = function
\r
105 | [] -> None, [], vars
\r
106 | c::cs -> if c = '(' then (
\r
107 let tm, cs, vars = read_smt vars cs in
\r
108 let cs = strip_spaces cs in
\r
110 | [] -> None, [], vars
\r
111 | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError ") mancante")
\r
112 ) else raise (ParsingError "???")
\r
116 match read_smt ([],[]) (explode x) with
\r
117 | Some [y], [], _ -> y
\r
118 | _, _, _ -> raise (ParsingError "???")
\r
121 let parse_many strs =
\r
122 let f (x, y) z = match read_smt y (explode z) with
\r
123 | Some[tm], [], vars -> (tm :: x, vars)
\r
124 | _, _, _ -> raise (ParsingError "???")
\r
125 in let aux = List.fold_left f ([], ([], [])) (* index zero is reserved *)
\r
126 in let (tms, (_, free)) = aux strs
\r
127 in (List.rev tms, free)
\r
130 let problem_of_string s =
\r
131 let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in
\r
132 let head, lines = List.hd lines, List.tl lines in
\r
133 let name = String.trim (String.sub head 1 (String.length head - 1)) in
\r
134 let lines = List.filter ((<>) "") lines in
\r
135 let aux (last, div, conv, ps) line =
\r
136 let chr = String.sub line 0 1 in
\r
137 let line = String.trim (String.sub line 1 (String.length line - 1)) in
\r
138 if line = "" then chr, div, conv, ps else
\r
139 let rec aux' chr line =
\r
141 then chr, div, conv, ps
\r
143 then chr, line, conv, ps
\r
145 then chr, div, line::conv, ps
\r
147 then chr, div, conv, line::ps
\r
149 then aux' last line
\r
150 else raise (ParsingError
\r
151 ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in
\r
153 let _, div, conv, ps = List.fold_left aux ("#", "", [], []) lines in
\r
154 let div_provided = div <> "" in
\r
155 let div = if div_provided then div else "xxxxxx" in
\r
156 let strs = [div] @ ps @ conv in
\r
158 if List.length ps = 0 && List.length conv = 0
\r
159 then raise (ParsingError "empty problem");
\r
162 let (tms, free) = parse_many strs in
\r
163 let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in
\r
164 let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in
\r
165 let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in
\r
167 if v = lev + n_bot then failwith "Example with `Bottom"
\r
168 else if v = lev + n_pac then failwith "Example with `Pacman"
\r
169 else if v = lev + n_bomb then failwith "Example with `Bomb"
\r
170 else if v = lev then `Var(v, min_int) (* zero *)
\r
171 else `Var(v,1) in (* 1 by default when variable not applied *)
\r
174 let rec aux lev : nf -> nf = function
\r
175 | `I((n,_), args) -> `I((n,1 + Listx.length args), Listx.map (fun t -> aux lev t) args)
\r
176 | `Var(n,_) -> fix lev n
\r
177 | `Lam(_,t) -> `Lam (true, aux (lev+1) t)
\r
178 | `Match _ | `N _ -> assert false in
\r
179 let all_tms = List.map (aux 0) (tms :> Num.nf list) in
\r
182 let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in
\r
184 let div = if not div_provided
\r
186 else match div with
\r
187 | `I _ as t -> Some t
\r
188 | _ -> raise (ParsingError "div is not an inert or BOT in the initial problem") in
\r
189 let conv = Util.filter_map (
\r
191 | #i_n_var as t -> Some t
\r
193 | _ -> raise (ParsingError "A term in conv is not i_n_var")
\r
195 let ps = List.map (
\r
197 | #i_n_var as y -> y
\r
198 | _ as y -> raise (ParsingError ("A term in num is not i_n_var" ^ Num.string_of_nf y))
\r
200 name, div, conv, ps, free
\r
204 let from_file path =
\r
205 let lines = ref ["#"] in
\r
206 let chan = open_in path in
\r
209 lines := input_line chan :: !lines
\r
211 with End_of_file ->
\r
213 let txt = String.concat "\n" (List.rev !lines) in
\r
214 let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in
\r
215 List.map problem_of_string (List.tl (List.map ((^) "$") problems))
\r