--- /dev/null
+(* Copyright (C) 2004-2005, HELM Team.
+ *
+ * This file is part of HELM, an Hypertextual, Electronic
+ * Library of Mathematics, developed at the Computer Science
+ * Department, University of Bologna, Italy.
+ *
+ * HELM is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * HELM is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HELM; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ *
+ * For details, see the HELM World-Wide-Web page,
+ * http://helm.cs.unibo.it/
+ *)
+
+open CicNotationPt
+
+exception Value_not_found of string
+
+type value =
+ | TermValue of term
+ | StringValue of string
+ | NumValue of string
+ | OptValue of value option
+ | ListValue of value list
+
+type value_type =
+ | TermType
+ | StringType
+ | NumType
+ | OptType of value_type
+ | ListType of value_type
+
+type declaration = string * value_type
+type binding = string * (value_type * value)
+type env_type = (string * (value_type * value)) list
+
+let lookup_value ~env name =
+ try
+ snd (List.assoc name env)
+ with Not_found -> raise (Value_not_found name)
+
+let lookup_term ~env name =
+ match lookup_value ~env name with
+ | TermValue x -> x
+ | _ -> raise (Value_not_found name)
+
+let lookup_num ~env name =
+ match lookup_value ~env name with
+ | NumValue x -> x
+ | _ -> raise (Value_not_found name)
+
+let lookup_string ~env name =
+ match lookup_value ~env name with
+ | StringValue x -> x
+ | _ -> raise (Value_not_found name)
+
+let unopt_names names env =
+ let rec aux acc = function
+ | (name, (ty, v)) :: tl when List.mem name names ->
+ (match ty, v with
+ | OptType ty, OptValue (Some v) -> aux ((name, (ty, v)) :: acc) tl
+ | _ -> assert false)
+ | _ :: tl -> aux acc tl
+ | [] -> acc
+ in
+ aux [] env
+
+let opt_binding_some (n, (ty, v)) = (n, (OptType ty, OptValue (Some v)))
+let opt_binding_none (n, (ty, v)) = (n, (OptType ty, OptValue None))
+let opt_binding_of_name (n, ty) = (n, (OptType ty, OptValue None))
+let list_binding_of_name (n, ty) = (n, (ListType ty, ListValue []))
+let opt_declaration (n, ty) = (n, OptType ty)
+let list_declaration (n, ty) = (n, ListType ty)
+
+ (* TODO ensure that names generated by fresh_var do not clash with user's *)
+let fresh_var =
+ let index = ref ~-1 in
+ fun () ->
+ incr index;
+ "fresh" ^ string_of_int !index
+
+let meta_names_of term =
+ let rec names = ref [] in
+ let add_name n =
+ if List.mem n !names then ()
+ else names := n :: !names
+ in
+ let rec aux = function
+ | AttributedTerm (_, term) -> aux term
+ | Appl terms -> List.iter aux terms
+ | Binder (_, _, body) -> aux body
+ | Case (term, indty, outty_opt, patterns) ->
+ aux term ;
+ aux_opt outty_opt ;
+ List.iter aux_branch patterns
+ | LetIn (_, t1, t2) ->
+ aux t1 ;
+ aux t2
+ | LetRec (_, definitions, body) ->
+ List.iter aux_definition definitions ;
+ aux body
+ | Uri (_, Some substs) -> aux_substs substs
+ | Ident (_, Some substs) -> aux_substs substs
+ | Meta (_, substs) -> aux_meta_substs substs
+
+ | Implicit
+ | Ident _
+ | Num _
+ | Sort _
+ | Symbol _
+ | Uri _
+ | UserInput -> ()
+
+ | Magic magic -> aux_magic magic
+ | Variable var -> aux_variable var
+
+ | _ -> assert false
+ and aux_opt = function
+ | Some term -> aux term
+ | None -> ()
+ and aux_capture_var (_, ty_opt) = aux_opt ty_opt
+ and aux_branch (pattern, term) =
+ aux_pattern pattern ;
+ aux term
+ and aux_pattern (head, vars) =
+ List.iter aux_capture_var vars
+ and aux_definition (var, term, i) =
+ aux_capture_var var ;
+ aux term
+ and aux_substs substs = List.iter (fun (_, term) -> aux term) substs
+ and aux_meta_substs meta_substs = List.iter aux_opt meta_substs
+ and aux_variable = function
+ | NumVar name -> add_name name
+ | IdentVar name -> add_name name
+ | TermVar name -> add_name name
+ | FreshVar _ -> ()
+ | Ascription _ -> assert false
+ and aux_magic = function
+ | Fold (_, t1, _, t2) ->
+ aux t1 ;
+ aux t2
+ | Default (t1, t2) ->
+ aux t1 ;
+ aux t2
+ | _ -> assert false
+ in
+ aux term ;
+ !names
+
+let instantiate ~env term =
+ let fresh_env = ref [] in
+ let lookup_fresh_name n =
+ try
+ List.assoc n !fresh_env
+ with Not_found ->
+ let new_name = fresh_var () in
+ fresh_env := (n, new_name) :: !fresh_env;
+ new_name
+ in
+ let rec aux env term =
+ match term with
+ | AttributedTerm (_, term) -> aux env term
+ | Appl terms -> Appl (List.map (aux env) terms)
+ | Binder (binder, var, body) ->
+ Binder (binder, aux_capture_var env var, aux env body)
+ | Case (term, indty, outty_opt, patterns) ->
+ Case (aux env term, indty, aux_opt env outty_opt,
+ List.map (aux_branch env) patterns)
+ | LetIn (var, t1, t2) ->
+ LetIn (aux_capture_var env var, aux env t1, aux env t2)
+ | LetRec (kind, definitions, body) ->
+ LetRec (kind, List.map (aux_definition env) definitions, aux env body)
+ | Uri (name, None) -> Uri (name, None)
+ | Uri (name, Some substs) -> Uri (name, Some (aux_substs env substs))
+ | Ident (name, Some substs) -> Ident (name, Some (aux_substs env substs))
+ | Meta (index, substs) -> Meta (index, aux_meta_substs env substs)
+
+ | Implicit
+ | Ident _
+ | Num _
+ | Sort _
+ | Symbol _
+ | UserInput -> term
+
+ | Magic magic -> aux_magic env magic
+ | Variable var -> aux_variable env var
+
+ | _ -> assert false
+ and aux_opt env = function
+ | Some term -> Some (aux env term)
+ | None -> None
+ and aux_capture_var env (name, ty_opt) = (aux env name, aux_opt env ty_opt)
+ and aux_branch env (pattern, term) =
+ (aux_pattern env pattern, aux env term)
+ and aux_pattern env (head, vars) =
+ (head, List.map (aux_capture_var env) vars)
+ and aux_definition env (var, term, i) =
+ (aux_capture_var env var, aux env term, i)
+ and aux_substs env substs =
+ List.map (fun (name, term) -> (name, aux env term)) substs
+ and aux_meta_substs env meta_substs = List.map (aux_opt env) meta_substs
+ and aux_variable env = function
+ | NumVar name -> Num (lookup_num ~env name, 0)
+ | IdentVar name -> Ident (lookup_string ~env name, None)
+ | TermVar name -> lookup_term ~env name
+ | FreshVar name -> Ident (lookup_fresh_name name, None)
+ | Ascription (term, name) -> assert false
+ and aux_magic env = function
+ | Default (some_pattern, none_pattern) ->
+ (match meta_names_of some_pattern with
+ | [] -> assert false (* some pattern must contain at least 1 name *)
+ | (name :: _) as names ->
+ (match lookup_value ~env name with
+ | OptValue (Some _) ->
+ (* assumption: if "name" above is bound to Some _, then all
+ * names returned by "meta_names_of" are bound to Some _ as well
+ *)
+ aux (unopt_names names env) some_pattern
+ | OptValue None -> aux env none_pattern
+ | _ -> assert false))
+ | Fold _ -> failwith "not yet implemented" (* TODO Fold *)
+ | _ -> assert false
+ in
+ aux env term
+