1 (* Copyright (C) 2004-2005, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 module Ast = CicNotationPt
31 | TermValue of Ast.term
32 | StringValue of string
34 | OptValue of value option
35 | ListValue of value list
41 | OptType of value_type
42 | ListType of value_type
44 exception Value_not_found of string
45 exception Type_mismatch of string * value_type
47 type declaration = string * value_type
48 type binding = string * (value_type * value)
54 with Not_found -> raise (Value_not_found name)
56 let lookup_value env name =
58 snd (List.assoc name env)
59 with Not_found -> raise (Value_not_found name)
61 let remove_name env name = List.remove_assoc name env
63 let remove_names env names =
64 List.filter (fun name, _ -> not (List.mem name names)) env
66 let lookup_term env name =
67 match lookup env name with
69 | ty, _ -> raise (Type_mismatch (name, ty))
71 let lookup_num env name =
72 match lookup env name with
74 | ty, _ -> raise (Type_mismatch (name, ty))
76 let lookup_string env name =
77 match lookup env name with
78 | _, StringValue x -> x
79 | ty, _ -> raise (Type_mismatch (name, ty))
81 let lookup_opt env name =
82 match lookup env name with
84 | ty, _ -> raise (Type_mismatch (name, ty))
86 let lookup_list env name =
87 match lookup env name with
89 | ty, _ -> raise (Type_mismatch (name, ty))
91 let opt_binding_some (n, (ty, v)) = (n, (OptType ty, OptValue (Some v)))
92 let opt_binding_none (n, (ty, v)) = (n, (OptType ty, OptValue None))
93 let opt_binding_of_name (n, ty) = (n, (OptType ty, OptValue None))
94 let list_binding_of_name (n, ty) = (n, (ListType ty, ListValue []))
95 let opt_declaration (n, ty) = (n, OptType ty)
96 let list_declaration (n, ty) = (n, ListType ty)
98 let declaration_of_var = function
99 | Ast.NumVar s -> s, NumType
100 | Ast.IdentVar s -> s, StringType
101 | Ast.TermVar (s,(Ast.Self l|Ast.Level l)) -> s, TermType l
104 let value_of_term = function
105 | Ast.Num (s, _) -> NumValue s
106 | Ast.Ident (s, None) -> StringValue s
109 let term_of_value = function
110 | NumValue s -> Ast.Num (s, 0)
111 | StringValue s -> Ast.Ident (s, None)
113 | _ -> assert false (* TO BE UNDERSTOOD *)
115 let rec well_typed ty value =
117 | TermType _, TermValue _
118 | StringType, StringValue _
119 | OptType _, OptValue None
120 | NumType, NumValue _ -> true
121 | OptType ty', OptValue (Some value') -> well_typed ty' value'
122 | ListType ty', ListValue vl ->
123 List.for_all (fun value' -> well_typed ty' value') vl
126 let declarations_of_env = List.map (fun (name, (ty, _)) -> (name, ty))
127 let declarations_of_term p =
128 List.map declaration_of_var (CicNotationUtil.variables_of_term p)
130 let rec combine decls values =
131 match decls, values with
133 | (name, ty) :: decls, v :: values ->
134 (name, (ty, v)) :: (combine decls values)
137 let coalesce_env declarations env_list =
138 let env0 = List.map list_binding_of_name declarations in
139 let grow_env_entry env n v =
142 | (n', (ty, ListValue vl)) as entry ->
143 if n' = n then n', (ty, ListValue (v :: vl)) else entry
147 let grow_env env_i env =
149 (fun env (n, (_, v)) -> grow_env_entry env n v)
152 List.fold_right grow_env env_list env0