]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationEnv.ml
snapshort
[helm.git] / helm / ocaml / cic_notation / 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 open CicNotationPt
27
28 type value =
29   | TermValue of term
30   | StringValue of string
31   | NumValue of string
32   | OptValue of value option
33   | ListValue of value list
34
35 type value_type =
36   | TermType
37   | StringType
38   | NumType
39   | OptType of value_type
40   | ListType of value_type
41
42 exception Value_not_found of string
43 exception Type_mismatch of string * value_type
44
45 type declaration = string * value_type
46 type binding = string * (value_type * value)
47 type t = (string * (value_type * value)) list
48
49 let lookup env name =
50   try
51     List.assoc name env
52   with Not_found -> raise (Value_not_found name)
53
54 let lookup_value env name =
55   try
56     snd (List.assoc name env)
57   with Not_found -> raise (Value_not_found name)
58
59 let remove env name = List.remove_assoc name env
60
61 let lookup_term env name =
62   match lookup env name with
63   | _, TermValue x -> x
64   | ty, _ -> raise (Type_mismatch (name, ty))
65
66 let lookup_num env name =
67   match lookup env name with
68   | _, NumValue x -> x
69   | ty, _ -> raise (Type_mismatch (name, ty))
70
71 let lookup_string env name =
72   match lookup env name with
73   | _, StringValue x -> x
74   | ty, _ -> raise (Type_mismatch (name, ty))
75
76 let lookup_opt env name =
77   match lookup env name with
78   | _, OptValue x -> x
79   | ty, _ -> raise (Type_mismatch (name, ty))
80
81 let lookup_list env name =
82   match lookup env name with
83   | _, ListValue x -> x
84   | ty, _ -> raise (Type_mismatch (name, ty))
85
86 let opt_binding_some (n, (ty, v)) = (n, (OptType ty, OptValue (Some v)))
87 let opt_binding_none (n, (ty, v)) = (n, (OptType ty, OptValue None))
88 let opt_binding_of_name (n, ty) = (n, (OptType ty, OptValue None))
89 let list_binding_of_name (n, ty) = (n, (ListType ty, ListValue []))
90 let opt_declaration (n, ty) = (n, OptType ty)
91 let list_declaration (n, ty) = (n, ListType ty)
92
93 let declaration_of_var = function
94   | NumVar s -> s, NumType
95   | IdentVar s -> s, StringType
96   | TermVar s -> s, TermType
97   | _ -> assert false
98
99 let value_of_term = function
100   | Num (s, _) -> NumValue s
101   | Ident (s, None) -> StringValue s
102   | t -> TermValue t
103
104 let term_of_value = function
105   | NumValue s -> Num (s, 0)
106   | StringValue s -> Ident (s, None)
107   | TermValue t -> t
108   | _ -> assert false (* TO BE UNDERSTOOD *)
109
110 let rec well_typed ty value =
111   match ty, value with
112   | TermType, TermValue _
113   | StringType, StringValue _
114   | OptType _, OptValue None
115   | NumType, NumValue _ -> true
116   | OptType ty', OptValue (Some value') -> well_typed ty' value'
117   | ListType ty', ListValue vl ->
118       List.for_all (fun value' -> well_typed ty' value') vl
119   | _ -> false
120
121 let declarations_of_env = List.map (fun (name, (ty, _)) -> (name, ty))
122 let declarations_of_term p =
123   List.map declaration_of_var (CicNotationUtil.variables_of_term p)
124
125 let rec combine decls values =
126   match decls, values with
127   | [], [] -> []
128   | (name, ty) :: decls, v :: values ->
129       (name, (ty, v)) :: (combine decls values)
130   | _ -> assert false
131
132 let coalesce_env declarations env_list =
133   let env0 = List.map list_binding_of_name declarations in
134   let grow_env_entry env n v =
135     List.map
136       (function
137         | (n', (ty, ListValue vl)) as entry ->
138             if n' = n then n', (ty, ListValue (v :: vl)) else entry
139         | _ -> assert false)
140       env
141   in
142   let grow_env env_i env =
143     List.fold_left
144       (fun env (n, (_, v)) -> grow_env_entry env n v)
145       env env_i
146   in
147   List.fold_right grow_env env_list env0
148