]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_textual_parser/cicTextualParser.mly
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / ocaml / cic_textual_parser / cicTextualParser.mly
1 /* Copyright (C) 2000, 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://cs.unibo.it/helm/.
24  */
25
26 %{
27  open Cic;;
28  module U = UriManager;;
29
30  exception InvalidSuffix of string;;
31  exception InductiveTypeURIExpected;;
32  exception UnknownIdentifier of string;;
33  exception ExplicitNamedSubstitutionAppliedToRel;;
34  exception TheLeftHandSideOfAnExplicitNamedSubstitutionMustBeAVariable;;
35  
36  (* merge removing duplicates of two lists free of duplicates *)
37  let union dom1 dom2 =
38   let rec filter =
39    function
40       [] -> []
41     | he::tl ->
42        if List.mem he dom1 then filter tl else he::(filter tl)
43   in
44    dom1 @ (filter dom2)
45  ;;
46
47  let get_index_in_list e =
48   let rec aux i =
49    function
50       [] -> raise Not_found
51     | (Some he)::_ when he = e -> i
52     | _::tl -> aux (i+1) tl
53   in
54    aux 1
55  ;;
56
57  (* Returns the first meta whose number is above the *)
58  (* number of the higher meta.                       *)
59  (*CSC: cut&pasted from proofEngine.ml *)
60  let new_meta () =
61   let rec aux =
62    function
63       None,[] -> 1
64     | Some n,[] -> n
65     | None,(n,_,_)::tl -> aux (Some n,tl)
66     | Some m,(n,_,_)::tl -> if n > m then aux (Some n,tl) else aux (Some m,tl)
67   in
68    1 + aux (None,!CicTextualParser0.metasenv)
69  ;;
70
71  (* identity_relocation_list_for_metavariable i canonical_context         *)
72  (* returns the identity relocation list, which is the list [1 ; ... ; n] *)
73  (* where n = List.length [canonical_context]                             *)
74  (*CSC: ma mi basta la lunghezza del contesto canonico!!!*)
75  (*CSC: cut&pasted from proofEngine.ml *)
76  let identity_relocation_list_for_metavariable canonical_context =
77   let canonical_context_length = List.length canonical_context in
78    let rec aux =
79     function
80        (_,[]) -> []
81      | (n,None::tl) -> None::(aux ((n+1),tl))
82      | (n,_::tl) -> (Some (Cic.Rel n))::(aux ((n+1),tl))
83    in
84     aux (1,canonical_context)
85  ;;
86
87  let deoptionize_exp_named_subst =
88   function
89      None -> [], (function _ -> [])
90    | Some (dom,mk_exp_named_subst) -> dom,mk_exp_named_subst
91  ;;
92
93  let term_of_con_uri uri exp_named_subst =
94   Const (uri,exp_named_subst)
95  ;;
96
97  let term_of_var_uri uri exp_named_subst =
98   Var (uri,exp_named_subst)
99  ;;
100
101  let term_of_indty_uri (uri,tyno) exp_named_subst =
102   MutInd (uri, tyno, exp_named_subst)
103  ;;
104
105  let term_of_indcon_uri (uri,tyno,consno) exp_named_subst =
106   MutConstruct (uri, tyno, consno, exp_named_subst)
107  ;;
108
109  let term_of_uri uri =
110   match uri with
111      CicTextualParser0.ConUri uri ->
112       term_of_con_uri uri
113    | CicTextualParser0.VarUri uri ->
114       term_of_var_uri uri
115    | CicTextualParser0.IndTyUri (uri,tyno) ->
116       term_of_indty_uri (uri,tyno) 
117    | CicTextualParser0.IndConUri (uri,tyno,consno) ->
118       term_of_indcon_uri (uri,tyno,consno)
119  ;;
120
121  let var_uri_of_id id interp =
122   let module CTP0 = CicTextualParser0 in
123    match interp (CicTextualParser0.Id id) with
124       None -> raise (UnknownIdentifier id)
125     | Some (CTP0.Uri (CTP0.VarUri uri)) -> uri
126     | Some _ -> raise TheLeftHandSideOfAnExplicitNamedSubstitutionMustBeAVariable
127  ;;
128
129  let indty_uri_of_id id interp =
130   let module CTP0 = CicTextualParser0 in
131    match interp (CicTextualParser0.Id id) with
132       None -> raise (UnknownIdentifier id)
133     | Some (CTP0.Uri (CTP0.IndTyUri (uri,tyno))) -> (uri,tyno)
134     | Some _ -> raise InductiveTypeURIExpected
135  ;;
136
137  let mk_implicit () =
138   let newmeta = new_meta () in
139    let new_canonical_context = [] in
140     let irl =
141      identity_relocation_list_for_metavariable new_canonical_context
142     in
143      CicTextualParser0.metasenv :=
144       [newmeta, new_canonical_context, Sort Type ;
145        newmeta+1, new_canonical_context, Meta (newmeta,irl);
146        newmeta+2, new_canonical_context, Meta (newmeta+1,irl)
147       ] @ !CicTextualParser0.metasenv ;
148      [], function _ -> Meta (newmeta+2,irl)
149  ;;
150 %}
151 %token <string> ID
152 %token <int> META
153 %token <int> NUM
154 %token <UriManager.uri> CONURI
155 %token <UriManager.uri> VARURI
156 %token <UriManager.uri * int> INDTYURI
157 %token <UriManager.uri * int * int> INDCONURI
158 %token LPAREN RPAREN PROD LAMBDA COLON DOT SET PROP TYPE CAST IMPLICIT NONE
159 %token LETIN FIX COFIX SEMICOLON LCURLY RCURLY CASE ARROW LBRACKET RBRACKET EOF
160 %right ARROW
161 %start main
162 %type <CicTextualParser0.interpretation_domain_item list * (CicTextualParser0.interpretation -> Cic.term)> main
163 %%
164 main:
165  | EOF { raise CicTextualParser0.Eof } /* FG: was never raised */
166  | expr EOF { $1 }
167  | expr SEMICOLON { $1 } /*  FG: to read several terms in a row
168                           *  Do we need to clear some static variables? 
169                           */
170 ;
171 expr2:
172    CONURI exp_named_subst
173    { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
174       dom, function interp -> term_of_con_uri $1 (mk_exp_named_subst interp)
175    }
176  | VARURI exp_named_subst
177    { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
178       dom, function interp -> term_of_var_uri $1 (mk_exp_named_subst interp)
179    }
180  | INDTYURI exp_named_subst
181    { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
182       dom, function interp -> term_of_indty_uri $1 (mk_exp_named_subst interp)
183    }
184  | INDCONURI exp_named_subst
185    { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
186       dom, function interp -> term_of_indcon_uri $1 (mk_exp_named_subst interp)
187    }
188  | ID exp_named_subst
189    { try
190       let res =
191        Rel (get_index_in_list (Name $1) !CicTextualParser0.binders)
192       in
193        (match $2 with
194            None -> ([], function _ -> res)
195          | Some _ -> raise (ExplicitNamedSubstitutionAppliedToRel)
196        )
197      with
198       Not_found ->
199        let dom1,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
200         let dom = union dom1 [CicTextualParser0.Id $1] in
201          dom,
202           function interp ->
203            match interp (CicTextualParser0.Id $1) with
204               None  -> raise (UnknownIdentifier $1)
205             | Some (CicTextualParser0.Uri uri) ->
206                term_of_uri uri (mk_exp_named_subst interp)
207             | Some CicTextualParser0.Implicit ->
208                (*CSC: not very clean; to maximize code reusage *)
209                snd (mk_implicit ()) ""
210             | Some (CicTextualParser0.Term mk_term) ->
211                (mk_term interp)
212    }
213  | CASE LPAREN expr COLON INDTYURI SEMICOLON expr RPAREN LCURLY branches RCURLY
214     { let dom1,mk_expr1 = $3 in
215       let dom2,mk_expr2 = $7 in
216       let dom3,mk_expr3 = $10 in
217        let dom = (union  dom1 (union dom2 dom3)) in
218         dom,
219         function interp ->
220          MutCase
221           (fst $5,snd $5,(mk_expr2 interp),(mk_expr1 interp),(mk_expr3 interp))
222     }
223  | CASE LPAREN expr COLON ID SEMICOLON expr RPAREN LCURLY branches RCURLY
224     { let dom1,mk_expr1 = $3 in
225       let dom2,mk_expr2 = $7 in
226       let dom3,mk_expr3 = $10 in
227        let dom = union [CicTextualParser0.Id $5] (union dom1 (union dom2 dom3)) in
228         dom,
229         function interp ->
230          let uri,typeno = indty_uri_of_id $5 interp in
231           MutCase
232            (uri,typeno,(mk_expr2 interp),(mk_expr1 interp),
233              (mk_expr3 interp))
234     }
235  | fixheader LCURLY exprseplist RCURLY
236     { let dom1,foo,ids_and_indexes,mk_types = $1 in
237       let dom2,mk_exprseplist = $3 in
238        let dom = union dom1 dom2 in
239         for i = 1 to List.length ids_and_indexes do
240          CicTextualParser0.binders := List.tl !CicTextualParser0.binders
241         done ;
242         dom,
243          function interp ->
244           let types = mk_types interp in
245           let fixfunsbodies = (mk_exprseplist interp) in
246            let idx =
247             let rec find idx =
248              function
249                 [] -> raise Not_found
250               | (name,_)::_  when name = foo -> idx
251               | _::tl -> find (idx+1) tl
252             in
253              find 0 ids_and_indexes
254            in
255             let fixfuns =
256              List.map2 (fun ((name,recindex),ty) bo -> (name,recindex,ty,bo))
257               (List.combine ids_and_indexes types) fixfunsbodies
258             in
259              Fix (idx,fixfuns)
260     }
261  | cofixheader LCURLY exprseplist RCURLY
262     { let dom1,foo,ids,mk_types = $1 in
263       let dom2,mk_exprseplist = $3 in
264        let dom = union dom1 dom2 in
265         dom,
266          function interp ->
267           let types = mk_types interp in
268           let fixfunsbodies = (mk_exprseplist interp) in
269            let idx =
270             let rec find idx =
271              function
272                 [] -> raise Not_found
273               | name::_  when name = foo -> idx
274               | _::tl -> find (idx+1) tl
275             in
276              find 0 ids
277            in
278             let fixfuns =
279              List.map2 (fun (name,ty) bo -> (name,ty,bo))
280               (List.combine ids types) fixfunsbodies
281             in
282              for i = 1 to List.length fixfuns do
283               CicTextualParser0.binders := List.tl !CicTextualParser0.binders
284              done ;
285              CoFix (idx,fixfuns)
286     }
287  | IMPLICIT
288     { mk_implicit () }
289  | SET  { [], function _ -> Sort Set }
290  | PROP { [], function _ -> Sort Prop }
291  | TYPE { [], function _ -> Sort Type }
292  | LPAREN expr CAST expr RPAREN
293     { let dom1,mk_expr1 = $2 in
294       let dom2,mk_expr2 = $4 in
295        let dom = union dom1 dom2 in
296         dom, function interp -> Cast ((mk_expr1 interp),(mk_expr2 interp))
297     }
298  | META LBRACKET substitutionlist RBRACKET
299     { let dom,mk_substitutionlist = $3 in
300        dom, function interp -> Meta ($1, mk_substitutionlist interp)
301     } 
302  | LPAREN expr exprlist RPAREN
303     { let length,dom2,mk_exprlist = $3 in
304        match length with
305           0 -> $2
306         | _ ->
307           let dom1,mk_expr1 = $2 in
308            let dom = union dom1 dom2 in
309             dom,
310              function interp ->
311               Appl ((mk_expr1 interp)::(mk_exprlist interp))
312     }
313 ;
314 exp_named_subst :
315     { None }
316  | LCURLY named_substs RCURLY
317     { Some $2 }
318 ;
319 named_substs :
320    VARURI LETIN expr2
321     { let dom,mk_expr = $3 in
322        dom, function interp -> [$1, mk_expr interp] }
323  | ID LETIN expr2
324     { let dom1,mk_expr = $3 in
325        let dom = union [CicTextualParser0.Id $1] dom1 in
326         dom, function interp -> [var_uri_of_id $1 interp, mk_expr interp] }
327  | VARURI LETIN expr2 SEMICOLON named_substs
328     { let dom1,mk_expr = $3 in
329       let dom2,mk_named_substs = $5 in
330        let dom = union dom1 dom2 in
331         dom, function interp -> ($1, mk_expr interp)::(mk_named_substs interp)
332     }
333  | ID LETIN expr2 SEMICOLON named_substs
334     { let dom1,mk_expr = $3 in
335       let dom2,mk_named_substs = $5 in
336        let dom = union [CicTextualParser0.Id $1] (union dom1 dom2) in
337         dom,
338          function interp ->
339           (var_uri_of_id $1 interp, mk_expr interp)::(mk_named_substs interp)
340     }
341 ;
342 expr :
343    pihead expr
344     { CicTextualParser0.binders := List.tl !CicTextualParser0.binders ;
345       let dom1,mk_expr1 = snd $1 in
346       let dom2,mk_expr2 = $2 in
347        let dom = union dom1 dom2 in
348         dom, function interp -> Prod (fst $1, mk_expr1 interp, mk_expr2 interp)
349     }
350  | lambdahead expr
351     { CicTextualParser0.binders := List.tl !CicTextualParser0.binders ;
352       let dom1,mk_expr1 = snd $1 in
353       let dom2,mk_expr2 = $2 in
354        let dom = union dom1 dom2 in
355         dom,function interp -> Lambda (fst $1, mk_expr1 interp, mk_expr2 interp)
356     }
357  | letinhead expr
358     { CicTextualParser0.binders := List.tl !CicTextualParser0.binders ;
359       let dom1,mk_expr1 = snd $1 in
360       let dom2,mk_expr2 = $2 in
361        let dom = union dom1 dom2 in
362         dom, function interp -> LetIn (fst $1, mk_expr1 interp, mk_expr2 interp)
363     }
364  | expr2
365     { $1 }
366 ;
367 fixheader:
368    FIX ID LCURLY fixfunsdecl RCURLY
369     { let dom,ids_and_indexes,mk_types = $4 in
370        let bs =
371         List.rev_map (function (name,_) -> Some (Name name)) ids_and_indexes
372        in
373         CicTextualParser0.binders := bs@(!CicTextualParser0.binders) ;
374         dom, $2, ids_and_indexes, mk_types
375     }
376 ;
377 fixfunsdecl:
378    ID LPAREN NUM RPAREN COLON expr
379     { let dom,mk_expr = $6 in
380        dom, [$1,$3], function interp -> [mk_expr interp]
381     }
382  | ID LPAREN NUM RPAREN COLON expr SEMICOLON fixfunsdecl
383     { let dom1,mk_expr = $6 in
384       let dom2,ids_and_indexes,mk_types = $8 in
385        let dom = union dom1 dom2 in
386         dom, ($1,$3)::ids_and_indexes,
387          function interp -> (mk_expr interp)::(mk_types interp)
388     }
389 ;
390 cofixheader:
391    COFIX ID LCURLY cofixfunsdecl RCURLY
392     { let dom,ids,mk_types = $4 in
393        let bs =
394         List.rev_map (function name -> Some (Name name)) ids
395        in
396         CicTextualParser0.binders := bs@(!CicTextualParser0.binders) ;
397         dom, $2, ids, mk_types
398     }
399 ;
400 cofixfunsdecl:
401    ID COLON expr
402     { let dom,mk_expr = $3 in
403        dom, [$1], function interp -> [mk_expr interp]
404     }
405  | ID COLON expr SEMICOLON cofixfunsdecl
406     { let dom1,mk_expr = $3 in
407       let dom2,ids,mk_types = $5 in
408        let dom = union dom1 dom2 in
409         dom, $1::ids,
410          function interp -> (mk_expr interp)::(mk_types interp)
411     }
412 ;
413 pihead:
414    PROD ID COLON expr DOT
415     { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders;
416       let dom,mk_expr = $4 in
417        Cic.Name $2, (dom, function interp -> mk_expr interp)
418     }
419  | expr2 ARROW
420    { CicTextualParser0.binders := (Some Anonymous)::!CicTextualParser0.binders ;
421      let dom,mk_expr = $1 in
422       Anonymous, (dom, function interp -> mk_expr interp)
423    }
424  | PROD ID DOT
425     { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders;
426       let newmeta = new_meta () in
427        let new_canonical_context = [] in
428         let irl =
429          identity_relocation_list_for_metavariable new_canonical_context
430         in
431          CicTextualParser0.metasenv :=
432           [newmeta, new_canonical_context, Sort Type ;
433            newmeta+1, new_canonical_context, Meta (newmeta,irl)
434           ] @ !CicTextualParser0.metasenv ;
435          Cic.Name $2, ([], function _ -> Meta (newmeta+1,irl))
436     }
437 ;
438 lambdahead:
439    LAMBDA ID COLON expr DOT
440     { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders;
441       let dom,mk_expr = $4 in
442        Cic.Name $2, (dom, function interp -> mk_expr interp)
443     }
444  | LAMBDA ID DOT
445     { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders;
446       let newmeta = new_meta () in
447        let new_canonical_context = [] in
448         let irl =
449          identity_relocation_list_for_metavariable new_canonical_context
450         in
451          CicTextualParser0.metasenv :=
452           [newmeta, new_canonical_context, Sort Type ;
453            newmeta+1, new_canonical_context, Meta (newmeta,irl)
454           ] @ !CicTextualParser0.metasenv ;
455          Cic.Name $2, ([], function _ -> Meta (newmeta+1,irl))
456     }
457 ;
458 letinhead:
459   LAMBDA ID LETIN expr DOT
460    { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders ;
461      let dom,mk_expr = $4 in
462       Cic.Name $2, (dom, function interp -> mk_expr interp)
463    }
464 ;
465 branches:
466     { [], function _ -> [] }
467  | expr SEMICOLON branches
468     { let dom1,mk_expr = $1 in
469       let dom2,mk_branches = $3 in
470        let dom = union dom1 dom2 in
471         dom, function interp -> (mk_expr interp)::(mk_branches interp)
472     }
473  | expr
474     { let dom,mk_expr = $1 in
475        dom, function interp -> [mk_expr interp]
476     }
477 ;
478 exprlist:
479     
480     { 0, [], function _ -> [] }
481  | expr exprlist
482     { let dom1,mk_expr = $1 in
483       let length,dom2,mk_exprlist = $2 in
484        let dom = union dom1 dom2 in
485         length+1, dom, function interp -> (mk_expr interp)::(mk_exprlist interp)
486     }
487 ;
488 exprseplist:
489    expr
490     { let dom,mk_expr = $1 in
491        dom, function interp -> [mk_expr interp]
492     }
493  | expr SEMICOLON exprseplist
494     { let dom1,mk_expr = $1 in
495       let dom2,mk_exprseplist = $3 in
496        let dom = union dom1 dom2 in
497         dom, function interp -> (mk_expr interp)::(mk_exprseplist interp)
498     }
499 ;
500 substitutionlist:
501     { [], function _ -> [] }
502  | expr SEMICOLON substitutionlist
503     { let dom1,mk_expr = $1 in
504       let dom2,mk_substitutionlist = $3 in
505        let dom = union dom1 dom2 in
506         dom,
507          function interp ->(Some (mk_expr interp))::(mk_substitutionlist interp)
508     }
509  | NONE SEMICOLON substitutionlist
510     { let dom,mk_exprsubstitutionlist = $3 in
511        dom, function interp -> None::(mk_exprsubstitutionlist interp)
512     }