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 = NotationPt
35 | TermValue of NotationPt.term
36 | StringValue of ident_or_var
38 | OptValue of value option
39 | ListValue of value list
40 | DisambiguationValue of (Stdpp.location * string option * string option)
43 | TermType of int (* the level of the expected term *)
46 | OptType of value_type
47 | ListType of value_type
50 exception Value_not_found of string
51 exception Type_mismatch of string * value_type
53 type declaration = string * value_type
54 type binding = string * (value_type * value)
60 with Not_found -> raise (Value_not_found name)
62 let lookup_value env name =
64 snd (List.assoc name env)
65 with Not_found -> raise (Value_not_found name)
67 let remove_name env name = List.remove_assoc name env
69 let remove_names env names =
70 List.filter (fun name, _ -> not (List.mem name names)) env
72 let lookup_term env name =
73 match lookup env name with
75 | ty, _ -> raise (Type_mismatch (name, ty))
77 let lookup_num env name =
78 match lookup env name with
80 | ty, _ -> raise (Type_mismatch (name, ty))
82 let lookup_string env name =
83 match lookup env name with
84 | _, StringValue x -> x
85 | ty, _ -> raise (Type_mismatch (name, ty))
87 let lookup_opt env name =
88 match lookup env name with
90 | ty, _ -> raise (Type_mismatch (name, ty))
92 let lookup_list env name =
93 match lookup env name with
95 | ty, _ -> raise (Type_mismatch (name, ty))
97 let opt_binding_some (n, (ty, v)) = (n, (OptType ty, OptValue (Some v)))
98 let opt_binding_none (n, (ty, v)) = (n, (OptType ty, OptValue None))
99 let opt_binding_of_name (n, ty) = (n, (OptType ty, OptValue None))
100 let list_binding_of_name (n, ty) = (n, (ListType ty, ListValue []))
101 let opt_declaration (n, ty) = (n, OptType ty)
102 let list_declaration (n, ty) = (n, ListType ty)
104 let declaration_of_var = function
105 | Ast.NumVar s -> s, NumType
106 | Ast.IdentVar s -> s, StringType
107 | Ast.TermVar (s,(Ast.Self l|Ast.Level l)) -> s, TermType l
110 let value_of_term = function
111 | Ast.Num (s, _) -> NumValue s
112 | Ast.Ident (s, (* `Ambiguous? *) _) -> StringValue (Var s)
115 let term_of_value = function
116 | NumValue s -> Ast.Num (s, None)
117 | StringValue (Ident s) -> Ast.Ident (s, `Ambiguous)
119 | _ -> assert false (* TO BE UNDERSTOOD *)
121 let rec well_typed ty value =
123 | TermType _, TermValue _
124 | StringType, StringValue _
125 | OptType _, OptValue None
126 | NumType, NumValue _ -> true
127 | OptType ty', OptValue (Some value') -> well_typed ty' value'
128 | ListType ty', ListValue vl ->
129 List.for_all (fun value' -> well_typed ty' value') vl
132 let declarations_of_env = List.map (fun (name, (ty, _)) -> (name, ty))
133 let declarations_of_term p =
134 List.map declaration_of_var (NotationUtil.variables_of_term p)
136 let rec combine decls values =
137 match decls, values with
139 | (name, ty) :: decls, v :: values ->
140 (name, (ty, v)) :: (combine decls values)
143 let coalesce_env declarations env_list =
144 let env0 = List.map list_binding_of_name declarations in
145 let grow_env_entry env n v =
148 | (n', (ty, ListValue vl)) as entry ->
149 if n' = n then n', (ty, ListValue (v :: vl)) else entry
153 let grow_env env_i env =
155 (fun env (n, (_, v)) -> grow_env_entry env n v)
158 List.fold_right grow_env env_list env0