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 <= 122 ;; let isSpace c = c = ' ' || c = '\n' || c = '\t' ;; let rec index_of x = function | [] -> raise (Failure "index_of: Not Found") | h::t -> if x = h then 0 else 1 + index_of x t ;; (* FIXME *) let mk_var' (bound, free) x = if List.mem x bound then free, mk_var (index_of x bound) else if List.mem x free then free, mk_var (List.length bound + index_of x free) else (free @ [x]), mk_var (List.length bound + List.length free) ;; let mk_app' = function | [] -> raise (ParsingError "bug") | t :: ts -> List.fold_left mk_app t ts ;; let explode s = let rec aux i l = if i < 0 then l else aux (i - 1) (s.[i] :: l) in aux (String.length s - 1) [] ;; let implode l = let res = String.create (List.length l) in let rec aux i = function | [] -> res | c :: l -> 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 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) | _, _ -> 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 "manca dopo variabile lambda") | c::cs -> (if c = '.' then (match read_smt vars' cs with | None, _, _ -> raise (ParsingError "manca corpo lambda") | Some [x], y, (_, free) -> Some [mk_lam x], y, (bound, free) | Some _, _, _ -> raise (ParsingError "???") ) else raise (ParsingError "manca . nel 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 _ -> raise (ParsingError "bug") | 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 ") mancante") ) else raise (ParsingError "???") ;; let parse x = match read_smt ([],[]) (explode x) with | Some [y], [], _ -> y | _, _, _ -> raise (ParsingError "???") ;; let var_zero = "ω";; (* w is an invalid variable name *) let parse_many strs = let f (x, y) z = match read_smt y (explode z) with | Some[tm], [], vars -> (tm :: x, vars) | _, _, _ -> raise (ParsingError "???") in let aux = List.fold_left f ([], ([], [var_zero])) (* index zero is reserved *) 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"));; **********************************************************************)