]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_notation/cicNotationEnv.ml
snapshort
[helm.git] / helm / ocaml / cic_notation / cicNotationEnv.ml
index 749e9148b979e7d7a137fef76f4968f556767afb..f7f11c46db4a2951f355acc9d169bce233a586bd 100644 (file)
@@ -25,8 +25,6 @@
 
 open CicNotationPt
 
-exception Value_not_found of string
-
 type value =
   | TermValue of term
   | StringValue of string
@@ -41,10 +39,18 @@ type value_type =
   | OptType of value_type
   | ListType of value_type
 
+exception Value_not_found of string
+exception Type_mismatch of string * value_type
+
 type declaration = string * value_type
 type binding = string * (value_type * value)
 type t = (string * (value_type * value)) list
 
+let lookup env name =
+  try
+    List.assoc name env
+  with Not_found -> raise (Value_not_found name)
+
 let lookup_value env name =
   try
     snd (List.assoc name env)
@@ -53,19 +59,29 @@ let lookup_value env name =
 let remove env name = List.remove_assoc name env
 
 let lookup_term env name =
-  match lookup_value env name with
-  | TermValue x -> x
-  | _ -> raise (Value_not_found name)
+  match lookup env name with
+  | _, TermValue x -> x
+  | ty, _ -> raise (Type_mismatch (name, ty))
 
 let lookup_num env name =
-  match lookup_value env name with
-  | NumValue x -> x
-  | _ -> raise (Value_not_found name)
+  match lookup env name with
+  | _, NumValue x -> x
+  | ty, _ -> raise (Type_mismatch (name, ty))
 
 let lookup_string env name =
-  match lookup_value env name with
-  | StringValue x -> x
-  | _ -> raise (Value_not_found name)
+  match lookup env name with
+  | _, StringValue x -> x
+  | ty, _ -> raise (Type_mismatch (name, ty))
+
+let lookup_opt env name =
+  match lookup env name with
+  | _, OptValue x -> x
+  | ty, _ -> raise (Type_mismatch (name, ty))
+
+let lookup_list env name =
+  match lookup env name with
+  | _, ListValue x -> x
+  | ty, _ -> raise (Type_mismatch (name, ty))
 
 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))
@@ -103,6 +119,15 @@ let rec well_typed ty value =
   | _ -> false
 
 let declarations_of_env = List.map (fun (name, (ty, _)) -> (name, ty))
+let declarations_of_term p =
+  List.map declaration_of_var (CicNotationUtil.variables_of_term p)
+
+let rec combine decls values =
+  match decls, values with
+  | [], [] -> []
+  | (name, ty) :: decls, v :: values ->
+      (name, (ty, v)) :: (combine decls values)
+  | _ -> assert false
 
 let coalesce_env declarations env_list =
   let env0 = List.map list_binding_of_name declarations in