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