X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=ocaml%2Fparser_andrea.ml;fp=ocaml%2Fparser_andrea.ml;h=0000000000000000000000000000000000000000;hb=f1873841a3271d332cb8429b46e7bc7e0bca2402;hp=fbce730c78915d11457b701ee46691fef2cae988;hpb=3ba5ecfc8372d4c9f380bad5cdf7387f2bbcea6a;p=fireball-separation.git diff --git a/ocaml/parser_andrea.ml b/ocaml/parser_andrea.ml deleted file mode 100644 index fbce730..0000000 --- a/ocaml/parser_andrea.ml +++ /dev/null @@ -1,266 +0,0 @@ -type term = - | Var of int - | App of term * term - | Lam of term -;; - -let mk_app x y = App(x, y);; -let mk_lam x = Lam x;; -let mk_var x = Var x;; - -exception ParsingError of string;; - -let isAlphaNum c = let n = Char.code c in - (48 <= n && n <= 90) || (95 <= n && n <= 122) ;; -let isSpace c = c = ' ' || c = '\n' || c = '\t' ;; - -(* FIXME *) -let mk_var' (bound, free) x = - if x <> "@" && List.mem x bound - then free, mk_var (Util.index_of x bound) - else if x <> "@" && List.mem x free - then free, mk_var (List.length bound + Util.index_of x free) - else (free @ [x]), mk_var (List.length bound + List.length free) -;; - -let mk_app' = function - | [] -> assert false - | t :: ts -> List.fold_left mk_app t ts -;; - -let explode s = - let len = String.length s in - let rec aux i l = - if i >= len || s.[i] = '#' then l else aux (i+1) (s.[i] :: l) - in List.rev (aux 0 []) -;; - -let implode l = - let res = String.create (List.length l) in - let rec aux i = function - | [] -> res - | c :: l -> String.set res i c; aux (i + 1) l in - aux 0 l -;; - -let rec strip_spaces = function - | c::cs when isSpace c -> strip_spaces cs - | cs -> cs -;; - -let read_var s = - let rec aux = function - | [] -> None, [] - | c::cs as x -> - if c = '@' then - (if cs <> [] && (let hd = List.hd cs in hd = '@' || isAlphaNum hd) - then raise (ParsingError ("Unexpected `"^String.make 1 (List.hd cs)^"` after `@`.")) - else Some['@'], cs) - else if isAlphaNum c - then match aux cs with - | (Some x), cs' -> Some (c :: x), cs' - | None, cs' -> (Some [c]), cs' - else None, x - in match aux s with - | None, y -> None, y - | Some x, y -> Some (implode x), y -;; - -let read_var' (bound, free as vars) s = - match read_var s with - | Some varname, cs -> - let free, v = mk_var' vars varname in - Some [v], cs, (bound, free) - | None, _ -> raise (ParsingError ("Can't read variable")) -;; - -let rec read_smt vars = - let check_if_lambda cs = match read_var cs with - | None, _ -> false - | Some x, cs -> match strip_spaces cs with - | [] -> false - | c::_ -> c = '.' - in let read_lambda (bound, free) cs = ( - match read_var (strip_spaces cs) with - | Some varname, cs -> - let vars' = (varname::bound, free) in - (match strip_spaces cs with - | [] -> raise (ParsingError "Lambda expression incomplete") - | c::cs -> (if c = '.' then (match read_smt vars' cs with - | None, _, _ -> raise (ParsingError "Lambda body expected") - | Some [x], y, (_, free) -> Some [mk_lam x], y, (bound, free) - | Some _, _, _ -> assert false - ) else raise (ParsingError "Expected `.` after variable in lambda") - )) - | _, _ -> assert false - ) in let rec aux vars cs = - match strip_spaces cs with - | [] -> None, [], vars - | c::_ as x -> - let tms, cs, vars = ( - if c = '(' then read_pars vars x - else if c = ')' then (None, x, vars) - else if check_if_lambda x then read_lambda vars x - else read_var' vars x) in - match tms with - | Some [tm] -> ( - match aux vars cs with - | None, cs, vars -> Some [tm], cs, vars - | Some ts, cs, vars -> Some (tm :: ts), cs, vars - ) - | Some _ -> assert false - | None -> None, x, vars - in fun cs -> match aux vars cs with - | None, cs, vars -> None, cs, vars - | Some ts, cs, vars -> Some [mk_app' ts], cs, vars -and read_pars vars = function - | [] -> None, [], vars - | c::cs -> if c = '(' then ( - let tm, cs, vars = read_smt vars cs in - let cs = strip_spaces cs in - match cs with - | [] -> None, [], vars - | c::cs -> if c = ')' then tm, cs, vars else raise (ParsingError "Missing `)`") - ) else assert false -;; - -let parse x = - match read_smt ([],[]) (explode x) with - | Some [y], [], _ -> y - | _, _, _ -> assert false -;; - -let parse_many strs = - let f (x, y) z = match read_smt y (explode z) with - | Some[tm], [], vars -> (tm :: x, vars) - | _, _, _ -> assert false - in let aux = List.fold_left f ([], ([], [])) - in let (tms, (_, free)) = aux strs - in (List.rev tms, free) -;; - -(********************************************************************** - -let rec string_of_term = function - | Tvar n -> string_of_int n - | Tapp(t1, t2) -> "(" ^ string_of_term t1 ^ " " ^ string_of_term t2 ^ ")" - | Tlam(t1) -> "(\\" ^ string_of_term t1 ^ ")" -;; - -let _ = prerr_endline (">>>" ^ string_of_term (parse "(\\x. x y z z1 k) z1 z j"));; - - -*******************************************************************************) - -(* let problem_of_string s = - let lines = Str.split (Str.regexp "[\n\r\x0c\t;]+") s in - let head, lines = List.hd lines, List.tl lines in - let name = String.trim (String.sub head 1 (String.length head - 1)) in - let lines = List.filter ((<>) "") lines in - let aux (last, div, conv, ps) line = - let chr = String.sub line 0 1 in - let line = String.trim (String.sub line 1 (String.length line - 1)) in - if line = "" then chr, div, conv, ps else - let rec aux' chr line = - if chr = "#" - then chr, div, conv, ps - else if chr = "D" - then chr, line, conv, ps - else if chr = "C" - then chr, div, line::conv, ps - else if chr = "N" - then chr, div, conv, line::ps - else if chr = " " - then aux' last line - else raise (ParsingError - ("Unexpected at beginning of problem: `" ^ chr ^ "` " ^ line)) in - aux' chr line in - let _, div, conv, ps = List.fold_left aux ("#", "", [], []) lines in - let div_provided = div <> "" in - let div = if div_provided then div else "BOT" in - let strs = [div] @ ps @ conv in - - if List.length ps = 0 && List.length conv = 0 - then raise (ParsingError "Parsed empty problem"); - - (* parse' *) - let (tms, free) = parse_many strs in - (* Replace pacmans and bottoms *) - let n_bot = try Util.index_of "BOT" free with Not_found -> min_int in - let n_pac = try Util.index_of "PAC" free with Not_found -> min_int in - let n_bomb = try Util.index_of "BOMB" free with Not_found -> min_int in - let fix lev v = - if v = lev + n_bot then `Bottom - else if v = lev + n_pac then `Pacman - else if v = lev + n_bomb then `Lam(true, `Bottom) - else if v = lev then `Var(v, min_int) (* zero *) - else `Var(v,1) in (* 1 by default when variable not applied *) - (* Fix arity *) - let open Num in - let exclude_bottom = function - | #nf_nob as t -> t - (* actually, the following may be assert false *) - | `Bottom -> raise (ParsingError "Input term not in normal form") in - let rec aux_nob lev : nf_nob -> nf = function - | `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) - | `Var(n,_) -> fix lev n - | `Lam(_,t) -> `Lam (true, aux (lev+1) t) - | `Match _ | `N _ -> assert false - | `Pacman -> `Pacman - and aux lev : Num.nf -> Num.nf = function - | #nf_nob as t -> aux_nob lev t - | `Bottom -> assert false in -let all_tms = List.map (aux 0) (tms :> Num.nf list) in - -(* problem_of *) -let div, (ps, conv) = List.hd all_tms, Util.list_cut (List.length ps, List.tl all_tms) in - -let div = if not div_provided || div = `Bottom - then None - else match div with - | `I _ as t -> Some t - | _ -> raise (ParsingError "D is not an inert in the initial problem") in -let conv = Util.filter_map ( - function - | #i_n_var as t -> Some t - | `Lam _ -> None - | _ -> raise (ParsingError "A C-term is not i_n_var") - ) conv in -let ps = List.map ( - function - | #i_n_var as y -> y - | _ -> raise (ParsingError "An N-term is not i_n_var") - ) ps in - name, div, conv, ps, free -;; - - -let from_file path = - let lines = ref ["#"] in - let chan = open_in path in - let _ = try - while true; do - lines := input_line chan :: !lines - done - with End_of_file -> - close_in chan in - let txt = String.concat "\n" (List.rev !lines) in - let problems = Str.split (Str.regexp "[\n\r]+\\$") txt in - List.map problem_of_string (List.tl (List.map ((^) "$") problems)) -;; *) - -let parse x = - match read_smt ([],[]) (explode x) with - | Some [y], [], _ -> y - | _, _, _ -> assert false -;; - - -let parse_many strs = - let f (x, y) z = match read_smt y (explode z) with - | Some[tm], [], vars -> (tm :: x, vars) - | _, _, _ -> assert false - in let aux = List.fold_left f ([], ([], [])) - in let (tms, (_, free)) = aux strs - in (List.rev tms, free) -;;