]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationEnv.ml
ocaml 3.09 transition
[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 module Ast = CicNotationPt
27
28 type value =
29   | TermValue of Ast.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 = binding 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_name env name = List.remove_assoc name env
60
61 let remove_names env names =
62   List.filter (fun name, _ -> not (List.mem name names)) env
63
64 let lookup_term env name =
65   match lookup env name with
66   | _, TermValue x -> x
67   | ty, _ -> raise (Type_mismatch (name, ty))
68
69 let lookup_num env name =
70   match lookup env name with
71   | _, NumValue x -> x
72   | ty, _ -> raise (Type_mismatch (name, ty))
73
74 let lookup_string env name =
75   match lookup env name with
76   | _, StringValue x -> x
77   | ty, _ -> raise (Type_mismatch (name, ty))
78
79 let lookup_opt env name =
80   match lookup env name with
81   | _, OptValue x -> x
82   | ty, _ -> raise (Type_mismatch (name, ty))
83
84 let lookup_list env name =
85   match lookup env name with
86   | _, ListValue x -> x
87   | ty, _ -> raise (Type_mismatch (name, ty))
88
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)
95
96 let declaration_of_var = function
97   | Ast.NumVar s -> s, NumType
98   | Ast.IdentVar s -> s, StringType
99   | Ast.TermVar s -> s, TermType
100   | _ -> assert false
101
102 let value_of_term = function
103   | Ast.Num (s, _) -> NumValue s
104   | Ast.Ident (s, None) -> StringValue s
105   | t -> TermValue t
106
107 let term_of_value = function
108   | NumValue s -> Ast.Num (s, 0)
109   | StringValue s -> Ast.Ident (s, None)
110   | TermValue t -> t
111   | _ -> assert false (* TO BE UNDERSTOOD *)
112
113 let rec well_typed ty value =
114   match ty, value with
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
122   | _ -> false
123
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)
127
128 let rec combine decls values =
129   match decls, values with
130   | [], [] -> []
131   | (name, ty) :: decls, v :: values ->
132       (name, (ty, v)) :: (combine decls values)
133   | _ -> assert false
134
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 =
138     List.map
139       (function
140         | (n', (ty, ListValue vl)) as entry ->
141             if n' = n then n', (ty, ListValue (v :: vl)) else entry
142         | _ -> assert false)
143       env
144   in
145   let grow_env env_i env =
146     List.fold_left
147       (fun env (n, (_, v)) -> grow_env_entry env n v)
148       env env_i
149   in
150   List.fold_right grow_env env_list env0
151