]> matita.cs.unibo.it Git - helm.git/blob - components/acic_content/cicNotationEnv.ml
tagged 0.5.0-rc1
[helm.git] / components / acic_content / cicNotationEnv.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 module Ast = CicNotationPt
29
30 type value =
31   | TermValue of Ast.term
32   | StringValue of string
33   | NumValue of string
34   | OptValue of value option
35   | ListValue of value list
36
37 type value_type =
38   | TermType
39   | StringType
40   | NumType
41   | OptType of value_type
42   | ListType of value_type
43
44 exception Value_not_found of string
45 exception Type_mismatch of string * value_type
46
47 type declaration = string * value_type
48 type binding = string * (value_type * value)
49 type t = binding list
50
51 let lookup env name =
52   try
53     List.assoc name env
54   with Not_found -> raise (Value_not_found name)
55
56 let lookup_value env name =
57   try
58     snd (List.assoc name env)
59   with Not_found -> raise (Value_not_found name)
60
61 let remove_name env name = List.remove_assoc name env
62
63 let remove_names env names =
64   List.filter (fun name, _ -> not (List.mem name names)) env
65
66 let lookup_term env name =
67   match lookup env name with
68   | _, TermValue x -> x
69   | ty, _ -> raise (Type_mismatch (name, ty))
70
71 let lookup_num env name =
72   match lookup env name with
73   | _, NumValue x -> x
74   | ty, _ -> raise (Type_mismatch (name, ty))
75
76 let lookup_string env name =
77   match lookup env name with
78   | _, StringValue x -> x
79   | ty, _ -> raise (Type_mismatch (name, ty))
80
81 let lookup_opt env name =
82   match lookup env name with
83   | _, OptValue x -> x
84   | ty, _ -> raise (Type_mismatch (name, ty))
85
86 let lookup_list env name =
87   match lookup env name with
88   | _, ListValue x -> x
89   | ty, _ -> raise (Type_mismatch (name, ty))
90
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)
97
98 let declaration_of_var = function
99   | Ast.NumVar s -> s, NumType
100   | Ast.IdentVar s -> s, StringType
101   | Ast.TermVar s -> s, TermType
102   | _ -> assert false
103
104 let value_of_term = function
105   | Ast.Num (s, _) -> NumValue s
106   | Ast.Ident (s, None) -> StringValue s
107   | t -> TermValue t
108
109 let term_of_value = function
110   | NumValue s -> Ast.Num (s, 0)
111   | StringValue s -> Ast.Ident (s, None)
112   | TermValue t -> t
113   | _ -> assert false (* TO BE UNDERSTOOD *)
114
115 let rec well_typed ty value =
116   match ty, value with
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
124   | _ -> false
125
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)
129
130 let rec combine decls values =
131   match decls, values with
132   | [], [] -> []
133   | (name, ty) :: decls, v :: values ->
134       (name, (ty, v)) :: (combine decls values)
135   | _ -> assert false
136
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 =
140     List.map
141       (function
142         | (n', (ty, ListValue vl)) as entry ->
143             if n' = n then n', (ty, ListValue (v :: vl)) else entry
144         | _ -> assert false)
145       env
146   in
147   let grow_env env_i env =
148     List.fold_left
149       (fun env (n, (_, v)) -> grow_env_entry env n v)
150       env env_i
151   in
152   List.fold_right grow_env env_list env0
153