]> matita.cs.unibo.it Git - helm.git/blob - matitaB/components/content/notationEnv.ml
Matitaweb:
[helm.git] / matitaB / 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 NotationPt.term
36   | StringValue of ident_or_var
37   | NumValue of string
38   | OptValue of value option
39   | ListValue of value list
40    (* optional name of Ast.Symbols that we want to contain the interpretation of this literal
41     * location of the literal we are parsing
42     * optional uri, optional desc *)
43   | DisambiguationValue of (string option * Stdpp.location * string option * string option)
44
45 type value_type =
46   | TermType of int (* the level of the expected term *)
47   | StringType
48   | NumType
49   | OptType of value_type
50   | ListType of value_type
51   | NoType
52
53 exception Value_not_found of string
54 exception Type_mismatch of string * value_type
55
56 type declaration = string * value_type
57 type binding = string * (value_type * value)
58 type t = binding list
59
60 (* let rec pp_value = function
61  | TermValue t -> "T#" ^ NotationPp.pp_term (new NCicPp.status) t
62  | StringValue (Ident i) -> "I#" ^ i
63  | StringValue (Var v) -> "V#" ^ v
64  | NumValue n -> "N#" ^ n
65  | OptValue None -> "O#None"
66  | OptValue (Some v) -> "O#" ^ pp_value v
67  | ListValue vl -> "L#[" ^ (String.concat ";" (List.map pp_value vl)) ^ "]"
68  | DisambiguationValue _ -> "D#"
69
70 let pp_binding = function
71  | s, (ty,v) -> Printf.sprintf "{ %s := %s : %s }" s (pp_value v) "..."
72
73 let pp_env e = String.concat " " (List.map pp_binding e) *)
74
75 let lookup env name =
76   try
77     List.assoc name env
78   with Not_found -> raise (Value_not_found name)
79
80 let lookup_value env name =
81   try
82     snd (List.assoc name env)
83   with Not_found -> raise (Value_not_found name)
84
85 let remove_name env name = List.remove_assoc name env
86
87 let remove_names env names =
88   List.filter (fun name, _ -> not (List.mem name names)) env
89
90 let lookup_term env name =
91   match lookup env name with
92   | _, TermValue x -> x
93   | ty, _ -> raise (Type_mismatch (name, ty))
94
95 let lookup_num env name =
96   match lookup env name with
97   | _, NumValue x -> x
98   | ty, _ -> raise (Type_mismatch (name, ty))
99
100 let lookup_string env name =
101   match lookup env name with
102   | _, StringValue x -> x
103   | ty, _ -> raise (Type_mismatch (name, ty))
104
105 let lookup_opt env name =
106   match lookup env name with
107   | _, OptValue x -> x
108   | ty, _ -> raise (Type_mismatch (name, ty))
109
110 let lookup_list env name =
111   match lookup env name with
112   | _, ListValue x -> x
113   | ty, _ -> raise (Type_mismatch (name, ty))
114
115 let opt_binding_some (n, (ty, v)) = (n, (OptType ty, OptValue (Some v)))
116 let opt_binding_none (n, (ty, v)) = (n, (OptType ty, OptValue None))
117 let opt_binding_of_name (n, ty) = (n, (OptType ty, OptValue None))
118 let list_binding_of_name (n, ty) = (n, (ListType ty, ListValue []))
119 let opt_declaration (n, ty) = (n, OptType ty)
120 let list_declaration (n, ty) = (n, ListType ty)
121
122 let declaration_of_var = function
123   | Ast.NumVar s -> s, NumType
124   | Ast.IdentVar s -> s, StringType
125   | Ast.TermVar (s,(Ast.Self l|Ast.Level l)) -> s, TermType l
126   | _ -> assert false
127
128 let value_of_term = function
129   | Ast.Num (s, _) -> NumValue s
130   | Ast.Ident (s, (* `Ambiguous? *) _) -> StringValue (Var s)
131   | t -> TermValue t
132
133 let term_of_value = function
134   | NumValue s -> Ast.Num (s, None)
135   | StringValue (Ident s) -> Ast.Ident (s, `Ambiguous)
136   | TermValue t -> t
137   | _ -> assert false (* TO BE UNDERSTOOD *)
138
139 let rec well_typed ty value =
140   match ty, value with
141   | TermType _, TermValue _
142   | StringType, StringValue _
143   | OptType _, OptValue None
144   | NumType, NumValue _ -> true
145   | OptType ty', OptValue (Some value') -> well_typed ty' value'
146   | ListType ty', ListValue vl ->
147       List.for_all (fun value' -> well_typed ty' value') vl
148   | _ -> false
149
150 let declarations_of_env = List.map (fun (name, (ty, _)) -> (name, ty))
151 let declarations_of_term p =
152   List.map declaration_of_var (NotationUtil.variables_of_term p)
153
154 let rec combine decls values =
155   match decls, values with
156   | [], [] -> []
157   | (name, ty) :: decls, v :: values ->
158       (name, (ty, v)) :: (combine decls values)
159   | _ -> assert false
160
161 let coalesce_env declarations env_list =
162   let env0 = List.map list_binding_of_name declarations in
163   let grow_env_entry env n v =
164     List.map
165       (function
166         | (n', (ty, ListValue vl)) as entry ->
167             if n' = n then n', (ty, ListValue (v :: vl)) else entry
168         | _ -> assert false)
169       env
170   in
171   let grow_env env_i env =
172     List.fold_left
173       (fun env (n, (_, v)) -> grow_env_entry env n v)
174       env env_i
175   in
176   List.fold_right grow_env env_list env0
177