]> matita.cs.unibo.it Git - helm.git/blob - matitaB/components/content/notationPt.ml
Added NotationPt.name_of_obj.
[helm.git] / matitaB / components / content / notationPt.ml
1 (* Copyright (C) 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 (** CIC Notation Parse Tree *)
29
30 type binder_kind = [ `Lambda | `Pi | `Exists | `Forall ]
31 type induction_kind = [ `Inductive | `CoInductive ]
32 type sort_kind = [ `Prop | `Set | `NType of string |`NCProp of string]
33 type fold_kind = [ `Left | `Right ]
34
35 type location = Stdpp.location
36 let fail floc msg =
37   let (x, y) = HExtlib.loc_of_floc floc in
38   failwith (Printf.sprintf "Error at characters %d - %d: %s" x y msg)
39
40 type href = NReference.reference
41
42 type child_pos = [ `Left | `Right | `Inner ]
43
44 type term_attribute =
45   [ `Loc of location                  (* source file location *)
46   | `IdRef of string                  (* ACic pointer *)
47   | `Level of int
48   | `XmlAttrs of (string option * string * string) list
49       (* list of XML attributes: namespace, name, value *)
50   | `Raw of string                    (* unparsed version *)
51   ]
52
53 type literal =
54   [ `Symbol of string * (string option * string option)
55   | `Keyword of string * (string option * string option)
56   | `Number of string * (string option * string option)
57   ]
58
59 type case_indtype = string * href option
60
61 type 'term capture_variable = 'term * 'term option
62
63 (** To be increased each time the term type below changes, used for "safe"
64  * marshalling *)
65 let magic = 8
66
67 type term =
68   (* CIC AST *)
69
70   | AttributedTerm of term_attribute * term
71
72   | Appl of term list
73   | Binder of binder_kind * term capture_variable * term (* kind, name, body *)
74   | Case of term * case_indtype option * term option *
75       (case_pattern * term) list
76       (* what to match, inductive type, out type, <pattern,action> list *)
77   | Cast of term * term
78   | LetIn of term capture_variable * term * term  (* name, body, where *)
79   | LetRec of induction_kind * (term capture_variable list * term capture_variable * term * int) list * term
80       (* (params, name, body, decreasing arg) list, where *)
81   | Ident of string * [ `Ambiguous | `Uri of string | `Rel ]
82       (* literal, disambiguation info (can be ambiguous, a disambiguated uri, 
83        * or an identifier to be converted in a Rel)
84        * case `Uri is also used for literal uris
85        * the parser fills the name of the identifier using the final part of the
86        * uri
87        *)
88   | Implicit of [`Vector | `JustOne | `Tagged of string]
89   | Meta of int * meta_subst list
90   | Num of string * (string option * string) option 
91       (* literal, (uri, interpretation name) *)
92   | Sort of sort_kind
93   (* XXX: symbols used to be packed with their instance number, probably for
94    * disambiguation purposes; hopefully we won't need this info any more *)
95   | Symbol of string * (string option * string) option 
96       (* canonical name, (uri, interpretation name) *)
97   | UserInput (* place holder for user input, used by MatitaConsole, not to be
98               used elsewhere *)
99   | NRef of NReference.reference
100
101   | NCic of NCic.term
102
103   (* Syntax pattern extensions *)
104   (* string option = optional name of an Ast.Symbol occurring in the level 2
105    * term, which is associated to this literal *)
106   | Literal of (string option * literal)
107   | Layout of layout_pattern
108
109   | Magic of magic_term
110   | Variable of pattern_variable
111
112   (* name, type. First component must be Ident or Variable (FreshVar _) *)
113
114 and meta_subst = term option
115 and subst = string * term
116 and case_pattern =
117    Pattern of string * href option * term capture_variable list
118  | Wildcard
119
120 and box_kind = H | V | HV | HOV
121 and box_spec = box_kind * bool * bool (* kind, spacing, indent *)
122
123 and layout_pattern =
124   | Sub of term * term
125   | Sup of term * term
126   | Below of term * term
127   | Above of term * term
128   | Frac of term * term
129   | Over of term * term
130   | Atop of term * term
131   | InfRule of term * term * term
132 (*   | array of term * literal option * literal option
133       |+ column separator, row separator +| *)
134   | Maction of term list
135   | Sqrt of term
136   | Root of term * term (* argument, index *)
137   | Break
138   | Box of box_spec * term list
139   | Group of term list
140   | Mstyle of (string * string) list * term list
141   | Mpadded of (string * string) list * term list
142
143 and magic_term =
144   (* level 1 magics *)
145   | List0 of term * literal option (* pattern, separator *)
146   | List1 of term * literal option (* pattern, separator *)
147   | Opt of term
148
149   (* level 2 magics *)
150   | Fold of fold_kind * term * string list * term
151     (* base case pattern, recursive case bound names, recursive case pattern *)
152   | Default of term * term  (* "some" case pattern, "none" case pattern *)
153   | Fail
154   | If of term * term * term (* test, pattern if true, pattern if false *)
155
156 and term_level = Self of int | Level of int
157
158 and pattern_variable =
159   (* level 1 and 2 variables *)
160   | NumVar of string
161   | IdentVar of string
162   | TermVar of string * term_level
163
164   (* level 1 variables *)
165   | Ascription of term * string
166
167   (* level 2 variables *)
168   | FreshVar of string
169
170 type argument_pattern =
171   | IdentArg of int * string (* eta-depth, name *)
172
173 type cic_appl_pattern =
174   | NRefPattern of NReference.reference
175   | VarPattern of string
176   | ImplicitPattern
177   | ApplPattern of cic_appl_pattern list
178
179   (** <name, inductive/coinductive, type, constructor list>
180   * true means inductive, false coinductive *)
181 type 'term inductive_type = string * bool * 'term * (string * 'term) list
182
183 type 'term obj =
184   | Inductive of 'term capture_variable list * 'term inductive_type list
185       (** parameters, list of loc * mutual inductive types *)
186   | Theorem of NCic.def_flavour * string * 'term * 'term option * NCic.def_pragma
187       (** flavour, name, type, body
188        * - name is absent when an unnamed theorem is being proved, tipically in
189        *   interactive usage
190        * - body is present when its given along with the command, otherwise it
191        *   will be given in proof editing mode using the tactical language,
192        *   unless the flavour is an Axiom
193        *)
194   | Record of 'term capture_variable list * string * 'term * (string * 'term * bool * int) list
195       (** left parameters, name, type, fields *)
196
197 let name_of_obj = function
198   | Theorem (_,n,_,_,_) | Record (_,n,_,_)
199   | Inductive (_,(n,_,_,_)::_) -> n
200   | _ -> (* empty inductive block *) assert false
201 ;;
202  
203 let map_variable f (t,u) = f t, HExtlib.map_option f u ;;
204
205 let map_pattern f = function
206   | Pattern (s,h,vl) -> Pattern (s,h,List.map (map_variable f) vl)
207   | p -> p
208 ;;
209
210 let map f = function
211   | AttributedTerm (a,u) -> AttributedTerm (a,f u)
212   | Appl tl -> Appl (List.map f tl)
213   | Binder (k,n,body) -> Binder (k,map_variable f n,f body)
214   | Case (t,ity,oty,pl) -> 
215       let pl' = List.map (fun (p,u) -> map_pattern f p, f u) pl in 
216       Case (f t,ity,HExtlib.map_option f oty,pl')
217   | Cast (u,v) -> Cast (f u,f v)
218   | LetIn (n,u,v) -> LetIn (n,f u,f v)
219   | LetRec (ik,l,t) ->
220       let l' = List.map (fun (vl,v,u,n) -> 
221                 (List.map (map_variable f) vl,
222                  map_variable f v,
223                  f u,
224                  n)) l
225       in LetRec (ik,l',f t)
226   | t -> t
227 ;;
228
229 (** {2 Standard precedences} *)
230
231 let let_in_prec = 10
232 let binder_prec = 20
233 let apply_prec = 70
234 let simple_prec = 90
235
236 (* sequents *)
237
238 type context_entry = 
239     Decl of term
240   | Def of term * term 
241
242 type sequent = int * context * term
243 and context = (string * context_entry) list