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