1 /* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
28 module U = UriManager;;
30 exception InvalidSuffix of string;;
31 exception InductiveTypeURIExpected;;
32 exception UnknownIdentifier of string;;
33 exception ExplicitNamedSubstitutionAppliedToRel;;
34 exception TheLeftHandSideOfAnExplicitNamedSubstitutionMustBeAVariable;;
36 (* merge removing duplicates of two lists free of duplicates *)
42 if List.mem he dom1 then filter tl else he::(filter tl)
47 let get_index_in_list e =
51 | (Some he)::_ when he = e -> i
52 | _::tl -> aux (i+1) tl
57 (* Returns the first meta whose number is above the *)
58 (* number of the higher meta. *)
59 (*CSC: cut&pasted from proofEngine.ml *)
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)
68 1 + aux (None,!TexCicTextualParser0.metasenv)
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
81 | (n,None::tl) -> None::(aux ((n+1),tl))
82 | (n,_::tl) -> (Some (Cic.Rel n))::(aux ((n+1),tl))
84 aux (1,canonical_context)
87 let deoptionize_exp_named_subst =
89 None -> [], (function _ -> [])
90 | Some (dom,mk_exp_named_subst) -> dom,mk_exp_named_subst
93 let term_of_con_uri uri exp_named_subst =
94 Const (uri,exp_named_subst)
97 let term_of_var_uri uri exp_named_subst =
98 Var (uri,exp_named_subst)
101 let term_of_indty_uri (uri,tyno) exp_named_subst =
102 MutInd (uri, tyno, exp_named_subst)
105 let term_of_indcon_uri (uri,tyno,consno) exp_named_subst =
106 MutConstruct (uri, tyno, consno, exp_named_subst)
109 let term_of_uri uri =
111 CicTextualParser0.ConUri uri ->
113 | CicTextualParser0.VarUri 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)
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
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
138 let newmeta = new_meta () in
139 let newuniv = CicUniv.fresh () in
140 let new_canonical_context = [] in
142 identity_relocation_list_for_metavariable new_canonical_context
144 TexCicTextualParser0.metasenv :=
145 [newmeta, new_canonical_context, Sort (Type newuniv);
147 newmeta+1, new_canonical_context, Meta (newmeta,irl);
148 newmeta+2, new_canonical_context, Meta (newmeta+1,irl)
149 ] @ !TexCicTextualParser0.metasenv ;
150 [], function _ -> Meta (newmeta+2,irl)
157 %token <UriManager.uri> CONURI
158 %token <UriManager.uri> VARURI
159 %token <UriManager.uri * int> INDTYURI
160 %token <UriManager.uri * int * int> INDCONURI
161 %token LPAREN RPAREN PROD LAMBDA COLON DOT SET PROP TYPE CPROP CAST IMPLICIT NONE
162 %token LETIN FIX COFIX SEMICOLON LCURLY RCURLY CASE ARROW LBRACKET RBRACKET EOF
164 %token RPLUS RMINUS RTIMES RDIV
165 %token PLUS MINUS TIMES EQT EQ NEQT
167 %nonassoc EQ EQT NEQT
168 %left PLUS MINUS RPLUS RMINUS
169 %left TIMES RTIMES RDIV
171 %type <CicTextualParser0.interpretation_domain_item list * (CicTextualParser0.interpretation -> Cic.term)> main
174 | EOF { raise CicTextualParser0.Eof } /* FG: was never raised */
175 | DOLLAR DOLLAR EOF {raise CicTextualParser0.Eof }
176 | DOLLAR DOLLAR DOLLAR DOLLAR EOF {raise CicTextualParser0.Eof }
178 | DOLLAR expr DOLLAR EOF { $2 }
179 | DOLLAR DOLLAR expr DOLLAR DOLLAR EOF { $3 }
180 | expr SEMICOLON { $1 } /* FG: to read several terms in a row
181 * Do we need to clear some static variables?
186 { [], function interp ->
187 let rec cic_real_of_real =
189 0 -> Cic.Const (HelmLibraryObjects.Reals.r0_URI, [])
190 | 1 -> Cic.Const (HelmLibraryObjects.Reals.r1_URI,[])
194 (HelmLibraryObjects.Reals.rplus_URI,[]) ;
195 Cic.Const (HelmLibraryObjects.Reals.r1_URI,[]);
196 cic_real_of_real (n - 1)
202 { [], function interp ->
203 let rec cic_int_of_int =
206 Cic.MutConstruct (HelmLibraryObjects.Datatypes.nat_URI,0,1,[])
209 [ Cic.MutConstruct (HelmLibraryObjects.Datatypes.nat_URI,0,2,[]) ;
210 cic_int_of_int (n - 1)
216 { let dom1,mk_expr1 = $1 in
217 let dom2,mk_expr2 = $3 in
218 let dom = union dom1 dom2 in
219 dom, function interp ->
221 [Cic.Const (HelmLibraryObjects.Reals.rplus_URI,[]) ;
227 { let dom1,mk_expr1 = $1 in
228 let dom2,mk_expr2 = $3 in
229 let dom = union dom1 dom2 in
230 dom, function interp ->
232 [Cic.Const (HelmLibraryObjects.Reals.rminus_URI,[]);
238 { let dom1,mk_expr1 = $1 in
239 let dom2,mk_expr2 = $3 in
240 let dom = union dom1 dom2 in
241 dom, function interp ->
243 [Cic.Const (HelmLibraryObjects.Reals.rmult_URI,[]) ;
249 { let dom1,mk_expr1 = $1 in
250 let dom2,mk_expr2 = $3 in
251 let dom = union dom1 dom2 in
252 dom, function interp ->
254 [Cic.Const (HelmLibraryObjects.Reals.rdiv_URI,[]) ;
260 { let dom1,mk_expr1 = $1 in
261 let dom2,mk_expr2 = $3 in
262 let dom = union dom1 dom2 in
263 dom, function interp ->
265 [Cic.Const (HelmLibraryObjects.Reals.rplus_URI,[]) ;
271 { let dom1,mk_expr1 = $1 in
272 let dom2,mk_expr2 = $3 in
273 let dom = union dom1 dom2 in
274 dom, function interp ->
276 [Cic.Const (HelmLibraryObjects.Peano.minus_URI,[]) ;
282 { let dom1,mk_expr1 = $1 in
283 let dom2,mk_expr2 = $3 in
284 let dom = union dom1 dom2 in
285 dom, function interp ->
287 [Cic.Const (HelmLibraryObjects.Peano.mult_URI,[]) ;
293 { let dom1,mk_expr1 = $1 in
294 let dom2,mk_expr2 = $3 in
295 let dom3,mk_expr3 = mk_implicit () in
296 let dom = union dom1 (union dom2 dom3) in
297 dom, function interp ->
299 [Cic.MutInd (HelmLibraryObjects.Logic.eq_URI,0,[]) ;
305 | CONURI exp_named_subst
306 { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
307 dom, function interp -> term_of_con_uri $1 (mk_exp_named_subst interp)
309 | VARURI exp_named_subst
310 { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
311 dom, function interp -> term_of_var_uri $1 (mk_exp_named_subst interp)
313 | INDTYURI exp_named_subst
314 { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
315 dom, function interp -> term_of_indty_uri $1 (mk_exp_named_subst interp)
317 | INDCONURI exp_named_subst
318 { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
319 dom, function interp -> term_of_indcon_uri $1 (mk_exp_named_subst interp)
324 Rel (get_index_in_list (Name $1) !TexCicTextualParser0.binders)
327 None -> ([], function _ -> res)
328 | Some _ -> raise (ExplicitNamedSubstitutionAppliedToRel)
332 let dom1,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
333 let dom = union dom1 [CicTextualParser0.Id $1] in
336 match interp (CicTextualParser0.Id $1) with
337 None -> raise (UnknownIdentifier $1)
338 | Some (CicTextualParser0.Uri uri) ->
339 term_of_uri uri (mk_exp_named_subst interp)
340 | Some CicTextualParser0.Implicit ->
341 (*CSC: not very clean; to maximize code reusage *)
342 snd (mk_implicit ()) ""
343 | Some (CicTextualParser0.Term mk_term) ->
346 | CASE LPAREN expr COLON INDTYURI SEMICOLON expr RPAREN LCURLY branches RCURLY
347 { let dom1,mk_expr1 = $3 in
348 let dom2,mk_expr2 = $7 in
349 let dom3,mk_expr3 = $10 in
350 let dom = union dom1 (union dom2 dom3) in
354 (fst $5,snd $5,(mk_expr2 interp),(mk_expr1 interp),(mk_expr3 interp))
356 | CASE LPAREN expr COLON ID SEMICOLON expr RPAREN LCURLY branches RCURLY
357 { let dom1,mk_expr1 = $3 in
358 let dom2,mk_expr2 = $7 in
359 let dom3,mk_expr3 = $10 in
361 union [CicTextualParser0.Id $5] (union dom1 (union dom2 dom3))
365 let uri,typeno = indty_uri_of_id $5 interp in
367 (uri,typeno,(mk_expr2 interp),(mk_expr1 interp),
370 | fixheader LCURLY exprseplist RCURLY
371 { let dom1,foo,ids_and_indexes,mk_types = $1 in
372 let dom2,mk_exprseplist = $3 in
373 let dom = union dom1 dom2 in
374 for i = 1 to List.length ids_and_indexes do
375 TexCicTextualParser0.binders := List.tl !TexCicTextualParser0.binders
379 let types = mk_types interp in
380 let fixfunsbodies = (mk_exprseplist interp) in
384 [] -> raise Not_found
385 | (name,_)::_ when name = foo -> idx
386 | _::tl -> find (idx+1) tl
388 find 0 ids_and_indexes
391 List.map2 (fun ((name,recindex),ty) bo -> (name,recindex,ty,bo))
392 (List.combine ids_and_indexes types) fixfunsbodies
396 | cofixheader LCURLY exprseplist RCURLY
397 { let dom1,foo,ids,mk_types = $1 in
398 let dom2,mk_exprseplist = $3 in
399 let dom = union dom1 dom2 in
402 let types = mk_types interp in
403 let fixfunsbodies = (mk_exprseplist interp) in
407 [] -> raise Not_found
408 | name::_ when name = foo -> idx
409 | _::tl -> find (idx+1) tl
414 List.map2 (fun (name,ty) bo -> (name,ty,bo))
415 (List.combine ids types) fixfunsbodies
417 for i = 1 to List.length fixfuns do
418 TexCicTextualParser0.binders :=
419 List.tl !TexCicTextualParser0.binders
425 | SET { [], function _ -> Sort Set }
426 | PROP { [], function _ -> Sort Prop }
427 | TYPE { [], function _ -> Sort (Type (CicUniv.fresh ())) (* TASSI: ?? *)}
428 | CPROP { [], function _ -> Sort CProp }
429 | LPAREN expr CAST expr RPAREN
430 { let dom1,mk_expr1 = $2 in
431 let dom2,mk_expr2 = $4 in
432 let dom = union dom1 dom2 in
433 dom, function interp -> Cast ((mk_expr1 interp),(mk_expr2 interp))
435 | META LBRACKET substitutionlist RBRACKET
436 { let dom,mk_substitutionlist = $3 in
437 dom, function interp -> Meta ($1, mk_substitutionlist interp)
439 | LPAREN expr exprlist RPAREN
440 { let length,dom2,mk_exprlist = $3 in
444 let dom1,mk_expr1 = $2 in
445 let dom = union dom1 dom2 in
448 Appl ((mk_expr1 interp)::(mk_exprlist interp))
453 | LCURLY named_substs RCURLY
458 { let dom,mk_expr = $3 in
459 dom, function interp -> [$1, mk_expr interp] }
461 { let dom1,mk_expr = $3 in
462 let dom = union [CicTextualParser0.Id $1] dom1 in
463 dom, function interp -> [var_uri_of_id $1 interp, mk_expr interp] }
464 | VARURI LETIN expr2 SEMICOLON named_substs
465 { let dom1,mk_expr = $3 in
466 let dom2,mk_named_substs = $5 in
467 let dom = union dom1 dom2 in
468 dom, function interp -> ($1, mk_expr interp)::(mk_named_substs interp)
470 | ID LETIN expr2 SEMICOLON named_substs
471 { let dom1,mk_expr = $3 in
472 let dom2,mk_named_substs = $5 in
473 let dom = union [CicTextualParser0.Id $1] (union dom1 dom2) in
476 (var_uri_of_id $1 interp, mk_expr interp)::(mk_named_substs interp)
481 { TexCicTextualParser0.binders := List.tl !TexCicTextualParser0.binders ;
482 let dom1,mk_expr1 = snd $1 in
483 let dom2,mk_expr2 = $2 in
484 let dom = union dom1 dom2 in
485 dom, function interp -> Prod (fst $1, mk_expr1 interp, mk_expr2 interp)
488 { TexCicTextualParser0.binders := List.tl !TexCicTextualParser0.binders ;
489 let dom1,mk_expr1 = snd $1 in
490 let dom2,mk_expr2 = $2 in
491 let dom = union dom1 dom2 in
492 dom,function interp -> Lambda (fst $1, mk_expr1 interp, mk_expr2 interp)
495 { TexCicTextualParser0.binders := List.tl !TexCicTextualParser0.binders ;
496 let dom1,mk_expr1 = snd $1 in
497 let dom2,mk_expr2 = $2 in
498 let dom = union dom1 dom2 in
499 dom, function interp -> LetIn (fst $1, mk_expr1 interp, mk_expr2 interp)
505 FIX ID LCURLY fixfunsdecl RCURLY
506 { let dom,ids_and_indexes,mk_types = $4 in
508 List.rev_map (function (name,_) -> Some (Name name)) ids_and_indexes
510 TexCicTextualParser0.binders := bs@(!TexCicTextualParser0.binders) ;
511 dom, $2, ids_and_indexes, mk_types
515 ID LPAREN NUM RPAREN COLON expr
516 { let dom,mk_expr = $6 in
517 dom, [$1,$3], function interp -> [mk_expr interp]
519 | ID LPAREN NUM RPAREN COLON expr SEMICOLON fixfunsdecl
520 { let dom1,mk_expr = $6 in
521 let dom2,ids_and_indexes,mk_types = $8 in
522 let dom = union dom1 dom2 in
523 dom, ($1,$3)::ids_and_indexes,
524 function interp -> (mk_expr interp)::(mk_types interp)
528 COFIX ID LCURLY cofixfunsdecl RCURLY
529 { let dom,ids,mk_types = $4 in
531 List.rev_map (function name -> Some (Name name)) ids
533 TexCicTextualParser0.binders := bs@(!TexCicTextualParser0.binders) ;
534 dom, $2, ids, mk_types
539 { let dom,mk_expr = $3 in
540 dom, [$1], function interp -> [mk_expr interp]
542 | ID COLON expr SEMICOLON cofixfunsdecl
543 { let dom1,mk_expr = $3 in
544 let dom2,ids,mk_types = $5 in
545 let dom = union dom1 dom2 in
547 function interp -> (mk_expr interp)::(mk_types interp)
551 PROD ID COLON expr DOT
552 { TexCicTextualParser0.binders :=
553 (Some (Name $2))::!TexCicTextualParser0.binders;
554 let dom,mk_expr = $4 in
555 Cic.Name $2, (dom, function interp -> mk_expr interp)
558 { TexCicTextualParser0.binders :=
559 (Some Anonymous)::!TexCicTextualParser0.binders ;
560 let dom,mk_expr = $1 in
561 Anonymous, (dom, function interp -> mk_expr interp)
564 { TexCicTextualParser0.binders :=
565 (Some (Name $2))::!TexCicTextualParser0.binders;
566 let newmeta = new_meta () in
567 let newuniv = CicUniv.fresh () in
568 let new_canonical_context = [] in
570 identity_relocation_list_for_metavariable new_canonical_context
572 TexCicTextualParser0.metasenv :=
573 [newmeta, new_canonical_context, Sort (Type newuniv);
575 newmeta+1, new_canonical_context, Meta (newmeta,irl)
576 ] @ !TexCicTextualParser0.metasenv ;
577 Cic.Name $2, ([], function _ -> Meta (newmeta+1,irl))
581 LAMBDA ID COLON expr DOT
582 { TexCicTextualParser0.binders :=
583 (Some (Name $2))::!TexCicTextualParser0.binders;
584 let dom,mk_expr = $4 in
585 Cic.Name $2, (dom, function interp -> mk_expr interp)
588 { TexCicTextualParser0.binders :=
589 (Some (Name $2))::!TexCicTextualParser0.binders;
590 let newmeta = new_meta () in
591 let newuniv = CicUniv.fresh () in
592 let new_canonical_context = [] in
594 identity_relocation_list_for_metavariable new_canonical_context
596 TexCicTextualParser0.metasenv :=
597 [newmeta, new_canonical_context, Sort (Type newuniv) ;
599 newmeta+1, new_canonical_context, Meta (newmeta,irl)
600 ] @ !TexCicTextualParser0.metasenv ;
601 Cic.Name $2, ([], function _ -> Meta (newmeta+1,irl))
605 LAMBDA ID LETIN expr DOT
606 { TexCicTextualParser0.binders :=
607 (Some (Name $2))::!TexCicTextualParser0.binders ;
608 let dom,mk_expr = $4 in
609 Cic.Name $2, (dom, function interp -> mk_expr interp)
613 { [], function _ -> [] }
614 | expr SEMICOLON branches
615 { let dom1,mk_expr = $1 in
616 let dom2,mk_branches = $3 in
617 let dom = union dom1 dom2 in
618 dom, function interp -> (mk_expr interp)::(mk_branches interp)
621 { let dom,mk_expr = $1 in
622 dom, function interp -> [mk_expr interp]
627 { 0, [], function _ -> [] }
629 { let dom1,mk_expr = $1 in
630 let length,dom2,mk_exprlist = $2 in
631 let dom = union dom1 dom2 in
632 length+1, dom, function interp -> (mk_expr interp)::(mk_exprlist interp)
637 { let dom,mk_expr = $1 in
638 dom, function interp -> [mk_expr interp]
640 | expr SEMICOLON exprseplist
641 { let dom1,mk_expr = $1 in
642 let dom2,mk_exprseplist = $3 in
643 let dom = union dom1 dom2 in
644 dom, function interp -> (mk_expr interp)::(mk_exprseplist interp)
648 { [], function _ -> [] }
649 | expr SEMICOLON substitutionlist
650 { let dom1,mk_expr = $1 in
651 let dom2,mk_substitutionlist = $3 in
652 let dom = union dom1 dom2 in
654 function interp ->(Some (mk_expr interp))::(mk_substitutionlist interp)
656 | NONE SEMICOLON substitutionlist
657 { let dom,mk_exprsubstitutionlist = $3 in
658 dom, function interp -> None::(mk_exprsubstitutionlist interp)