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/
26 module Ast = CicNotationPt
29 | TermValue of Ast.term
30 | StringValue of string
32 | OptValue of value option
33 | ListValue of value list
39 | OptType of value_type
40 | ListType of value_type
42 exception Value_not_found of string
43 exception Type_mismatch of string * value_type
45 type declaration = string * value_type
46 type binding = string * (value_type * value)
52 with Not_found -> raise (Value_not_found name)
54 let lookup_value env name =
56 snd (List.assoc name env)
57 with Not_found -> raise (Value_not_found name)
59 let remove_name env name = List.remove_assoc name env
61 let remove_names env names =
62 List.filter (fun name, _ -> not (List.mem name names)) env
64 let lookup_term env name =
65 match lookup env name with
67 | ty, _ -> raise (Type_mismatch (name, ty))
69 let lookup_num env name =
70 match lookup env name with
72 | ty, _ -> raise (Type_mismatch (name, ty))
74 let lookup_string env name =
75 match lookup env name with
76 | _, StringValue x -> x
77 | ty, _ -> raise (Type_mismatch (name, ty))
79 let lookup_opt env name =
80 match lookup env name with
82 | ty, _ -> raise (Type_mismatch (name, ty))
84 let lookup_list env name =
85 match lookup env name with
87 | ty, _ -> raise (Type_mismatch (name, ty))
89 let opt_binding_some (n, (ty, v)) = (n, (OptType ty, OptValue (Some v)))
90 let opt_binding_none (n, (ty, v)) = (n, (OptType ty, OptValue None))
91 let opt_binding_of_name (n, ty) = (n, (OptType ty, OptValue None))
92 let list_binding_of_name (n, ty) = (n, (ListType ty, ListValue []))
93 let opt_declaration (n, ty) = (n, OptType ty)
94 let list_declaration (n, ty) = (n, ListType ty)
96 let declaration_of_var = function
97 | Ast.NumVar s -> s, NumType
98 | Ast.IdentVar s -> s, StringType
99 | Ast.TermVar s -> s, TermType
102 let value_of_term = function
103 | Ast.Num (s, _) -> NumValue s
104 | Ast.Ident (s, None) -> StringValue s
107 let term_of_value = function
108 | NumValue s -> Ast.Num (s, 0)
109 | StringValue s -> Ast.Ident (s, None)
111 | _ -> assert false (* TO BE UNDERSTOOD *)
113 let rec well_typed ty value =
115 | TermType, TermValue _
116 | StringType, StringValue _
117 | OptType _, OptValue None
118 | NumType, NumValue _ -> true
119 | OptType ty', OptValue (Some value') -> well_typed ty' value'
120 | ListType ty', ListValue vl ->
121 List.for_all (fun value' -> well_typed ty' value') vl
124 let declarations_of_env = List.map (fun (name, (ty, _)) -> (name, ty))
125 let declarations_of_term p =
126 List.map declaration_of_var (CicNotationUtil.variables_of_term p)
128 let rec combine decls values =
129 match decls, values with
131 | (name, ty) :: decls, v :: values ->
132 (name, (ty, v)) :: (combine decls values)
135 let coalesce_env declarations env_list =
136 let env0 = List.map list_binding_of_name declarations in
137 let grow_env_entry env n v =
140 | (n', (ty, ListValue vl)) as entry ->
141 if n' = n then n', (ty, ListValue (v :: vl)) else entry
145 let grow_env env_i env =
147 (fun env (n, (_, v)) -> grow_env_entry env n v)
150 List.fold_right grow_env env_list env0