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 new_canonical_context = [] in
141 identity_relocation_list_for_metavariable new_canonical_context
143 TexCicTextualParser0.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 ] @ !TexCicTextualParser0.metasenv ;
148 [], function _ -> Meta (newmeta+2,irl)
155 %token <UriManager.uri> CONURI
156 %token <UriManager.uri> VARURI
157 %token <UriManager.uri * int> INDTYURI
158 %token <UriManager.uri * int * int> INDCONURI
159 %token LPAREN RPAREN PROD LAMBDA COLON DOT SET PROP TYPE CPROP CAST IMPLICIT NONE
160 %token LETIN FIX COFIX SEMICOLON LCURLY RCURLY CASE ARROW LBRACKET RBRACKET EOF
162 %token RPLUS RMINUS RTIMES RDIV
163 %token PLUS MINUS TIMES EQT EQ NEQT
165 %nonassoc EQ EQT NEQT
166 %left PLUS MINUS RPLUS RMINUS
167 %left TIMES RTIMES RDIV
169 %type <CicTextualParser0.interpretation_domain_item list * (CicTextualParser0.interpretation -> Cic.term)> main
172 | EOF { raise CicTextualParser0.Eof } /* FG: was never raised */
173 | DOLLAR DOLLAR EOF {raise CicTextualParser0.Eof }
174 | DOLLAR DOLLAR DOLLAR DOLLAR EOF {raise CicTextualParser0.Eof }
176 | DOLLAR expr DOLLAR EOF { $2 }
177 | DOLLAR DOLLAR expr DOLLAR DOLLAR EOF { $3 }
178 | expr SEMICOLON { $1 } /* FG: to read several terms in a row
179 * Do we need to clear some static variables?
184 { [], function interp ->
185 let rec cic_real_of_real =
187 0 -> Cic.Const (HelmLibraryObjects.Reals.r0_URI, [])
188 | 1 -> Cic.Const (HelmLibraryObjects.Reals.r1_URI,[])
192 (HelmLibraryObjects.Reals.rplus_URI,[]) ;
193 Cic.Const (HelmLibraryObjects.Reals.r1_URI,[]);
194 cic_real_of_real (n - 1)
200 { [], function interp ->
201 let rec cic_int_of_int =
204 Cic.MutConstruct (HelmLibraryObjects.Datatypes.nat_URI,0,1,[])
207 [ Cic.MutConstruct (HelmLibraryObjects.Datatypes.nat_URI,0,2,[]) ;
208 cic_int_of_int (n - 1)
214 { let dom1,mk_expr1 = $1 in
215 let dom2,mk_expr2 = $3 in
216 let dom = union dom1 dom2 in
217 dom, function interp ->
219 [Cic.Const (HelmLibraryObjects.Reals.rplus_URI,[]) ;
225 { let dom1,mk_expr1 = $1 in
226 let dom2,mk_expr2 = $3 in
227 let dom = union dom1 dom2 in
228 dom, function interp ->
230 [Cic.Const (HelmLibraryObjects.Reals.rminus_URI,[]);
236 { let dom1,mk_expr1 = $1 in
237 let dom2,mk_expr2 = $3 in
238 let dom = union dom1 dom2 in
239 dom, function interp ->
241 [Cic.Const (HelmLibraryObjects.Reals.rmult_URI,[]) ;
247 { let dom1,mk_expr1 = $1 in
248 let dom2,mk_expr2 = $3 in
249 let dom = union dom1 dom2 in
250 dom, function interp ->
252 [Cic.Const (HelmLibraryObjects.Reals.rdiv_URI,[]) ;
258 { let dom1,mk_expr1 = $1 in
259 let dom2,mk_expr2 = $3 in
260 let dom = union dom1 dom2 in
261 dom, function interp ->
263 [Cic.Const (HelmLibraryObjects.Reals.rplus_URI,[]) ;
269 { let dom1,mk_expr1 = $1 in
270 let dom2,mk_expr2 = $3 in
271 let dom = union dom1 dom2 in
272 dom, function interp ->
274 [Cic.Const (HelmLibraryObjects.Peano.minus_URI,[]) ;
280 { let dom1,mk_expr1 = $1 in
281 let dom2,mk_expr2 = $3 in
282 let dom = union dom1 dom2 in
283 dom, function interp ->
285 [Cic.Const (HelmLibraryObjects.Peano.mult_URI,[]) ;
291 { let dom1,mk_expr1 = $1 in
292 let dom2,mk_expr2 = $3 in
293 let dom3,mk_expr3 = mk_implicit () in
294 let dom = union dom1 (union dom2 dom3) in
295 dom, function interp ->
297 [Cic.MutInd (HelmLibraryObjects.Logic.eq_URI,0,[]) ;
303 | CONURI exp_named_subst
304 { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
305 dom, function interp -> term_of_con_uri $1 (mk_exp_named_subst interp)
307 | VARURI exp_named_subst
308 { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
309 dom, function interp -> term_of_var_uri $1 (mk_exp_named_subst interp)
311 | INDTYURI exp_named_subst
312 { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
313 dom, function interp -> term_of_indty_uri $1 (mk_exp_named_subst interp)
315 | INDCONURI exp_named_subst
316 { let dom,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
317 dom, function interp -> term_of_indcon_uri $1 (mk_exp_named_subst interp)
322 Rel (get_index_in_list (Name $1) !TexCicTextualParser0.binders)
325 None -> ([], function _ -> res)
326 | Some _ -> raise (ExplicitNamedSubstitutionAppliedToRel)
330 let dom1,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
331 let dom = union dom1 [CicTextualParser0.Id $1] in
334 match interp (CicTextualParser0.Id $1) with
335 None -> raise (UnknownIdentifier $1)
336 | Some (CicTextualParser0.Uri uri) ->
337 term_of_uri uri (mk_exp_named_subst interp)
338 | Some CicTextualParser0.Implicit ->
339 (*CSC: not very clean; to maximize code reusage *)
340 snd (mk_implicit ()) ""
341 | Some (CicTextualParser0.Term mk_term) ->
344 | CASE LPAREN expr COLON INDTYURI SEMICOLON expr RPAREN LCURLY branches RCURLY
345 { let dom1,mk_expr1 = $3 in
346 let dom2,mk_expr2 = $7 in
347 let dom3,mk_expr3 = $10 in
348 let dom = union dom1 (union dom2 dom3) in
352 (fst $5,snd $5,(mk_expr2 interp),(mk_expr1 interp),(mk_expr3 interp))
354 | CASE LPAREN expr COLON ID SEMICOLON expr RPAREN LCURLY branches RCURLY
355 { let dom1,mk_expr1 = $3 in
356 let dom2,mk_expr2 = $7 in
357 let dom3,mk_expr3 = $10 in
359 union [CicTextualParser0.Id $5] (union dom1 (union dom2 dom3))
363 let uri,typeno = indty_uri_of_id $5 interp in
365 (uri,typeno,(mk_expr2 interp),(mk_expr1 interp),
368 | fixheader LCURLY exprseplist RCURLY
369 { let dom1,foo,ids_and_indexes,mk_types = $1 in
370 let dom2,mk_exprseplist = $3 in
371 let dom = union dom1 dom2 in
372 for i = 1 to List.length ids_and_indexes do
373 TexCicTextualParser0.binders := List.tl !TexCicTextualParser0.binders
377 let types = mk_types interp in
378 let fixfunsbodies = (mk_exprseplist interp) in
382 [] -> raise Not_found
383 | (name,_)::_ when name = foo -> idx
384 | _::tl -> find (idx+1) tl
386 find 0 ids_and_indexes
389 List.map2 (fun ((name,recindex),ty) bo -> (name,recindex,ty,bo))
390 (List.combine ids_and_indexes types) fixfunsbodies
394 | cofixheader LCURLY exprseplist RCURLY
395 { let dom1,foo,ids,mk_types = $1 in
396 let dom2,mk_exprseplist = $3 in
397 let dom = union dom1 dom2 in
400 let types = mk_types interp in
401 let fixfunsbodies = (mk_exprseplist interp) in
405 [] -> raise Not_found
406 | name::_ when name = foo -> idx
407 | _::tl -> find (idx+1) tl
412 List.map2 (fun (name,ty) bo -> (name,ty,bo))
413 (List.combine ids types) fixfunsbodies
415 for i = 1 to List.length fixfuns do
416 TexCicTextualParser0.binders :=
417 List.tl !TexCicTextualParser0.binders
423 | SET { [], function _ -> Sort Set }
424 | PROP { [], function _ -> Sort Prop }
425 | TYPE { [], function _ -> Sort Type }
426 | CPROP { [], function _ -> Sort CProp }
427 | LPAREN expr CAST expr RPAREN
428 { let dom1,mk_expr1 = $2 in
429 let dom2,mk_expr2 = $4 in
430 let dom = union dom1 dom2 in
431 dom, function interp -> Cast ((mk_expr1 interp),(mk_expr2 interp))
433 | META LBRACKET substitutionlist RBRACKET
434 { let dom,mk_substitutionlist = $3 in
435 dom, function interp -> Meta ($1, mk_substitutionlist interp)
437 | LPAREN expr exprlist RPAREN
438 { let length,dom2,mk_exprlist = $3 in
442 let dom1,mk_expr1 = $2 in
443 let dom = union dom1 dom2 in
446 Appl ((mk_expr1 interp)::(mk_exprlist interp))
451 | LCURLY named_substs RCURLY
456 { let dom,mk_expr = $3 in
457 dom, function interp -> [$1, mk_expr interp] }
459 { let dom1,mk_expr = $3 in
460 let dom = union [CicTextualParser0.Id $1] dom1 in
461 dom, function interp -> [var_uri_of_id $1 interp, mk_expr interp] }
462 | VARURI LETIN expr2 SEMICOLON named_substs
463 { let dom1,mk_expr = $3 in
464 let dom2,mk_named_substs = $5 in
465 let dom = union dom1 dom2 in
466 dom, function interp -> ($1, mk_expr interp)::(mk_named_substs interp)
468 | ID LETIN expr2 SEMICOLON named_substs
469 { let dom1,mk_expr = $3 in
470 let dom2,mk_named_substs = $5 in
471 let dom = union [CicTextualParser0.Id $1] (union dom1 dom2) in
474 (var_uri_of_id $1 interp, mk_expr interp)::(mk_named_substs interp)
479 { TexCicTextualParser0.binders := List.tl !TexCicTextualParser0.binders ;
480 let dom1,mk_expr1 = snd $1 in
481 let dom2,mk_expr2 = $2 in
482 let dom = union dom1 dom2 in
483 dom, function interp -> Prod (fst $1, mk_expr1 interp, mk_expr2 interp)
486 { TexCicTextualParser0.binders := List.tl !TexCicTextualParser0.binders ;
487 let dom1,mk_expr1 = snd $1 in
488 let dom2,mk_expr2 = $2 in
489 let dom = union dom1 dom2 in
490 dom,function interp -> Lambda (fst $1, mk_expr1 interp, mk_expr2 interp)
493 { TexCicTextualParser0.binders := List.tl !TexCicTextualParser0.binders ;
494 let dom1,mk_expr1 = snd $1 in
495 let dom2,mk_expr2 = $2 in
496 let dom = union dom1 dom2 in
497 dom, function interp -> LetIn (fst $1, mk_expr1 interp, mk_expr2 interp)
503 FIX ID LCURLY fixfunsdecl RCURLY
504 { let dom,ids_and_indexes,mk_types = $4 in
506 List.rev_map (function (name,_) -> Some (Name name)) ids_and_indexes
508 TexCicTextualParser0.binders := bs@(!TexCicTextualParser0.binders) ;
509 dom, $2, ids_and_indexes, mk_types
513 ID LPAREN NUM RPAREN COLON expr
514 { let dom,mk_expr = $6 in
515 dom, [$1,$3], function interp -> [mk_expr interp]
517 | ID LPAREN NUM RPAREN COLON expr SEMICOLON fixfunsdecl
518 { let dom1,mk_expr = $6 in
519 let dom2,ids_and_indexes,mk_types = $8 in
520 let dom = union dom1 dom2 in
521 dom, ($1,$3)::ids_and_indexes,
522 function interp -> (mk_expr interp)::(mk_types interp)
526 COFIX ID LCURLY cofixfunsdecl RCURLY
527 { let dom,ids,mk_types = $4 in
529 List.rev_map (function name -> Some (Name name)) ids
531 TexCicTextualParser0.binders := bs@(!TexCicTextualParser0.binders) ;
532 dom, $2, ids, mk_types
537 { let dom,mk_expr = $3 in
538 dom, [$1], function interp -> [mk_expr interp]
540 | ID COLON expr SEMICOLON cofixfunsdecl
541 { let dom1,mk_expr = $3 in
542 let dom2,ids,mk_types = $5 in
543 let dom = union dom1 dom2 in
545 function interp -> (mk_expr interp)::(mk_types interp)
549 PROD ID COLON expr DOT
550 { TexCicTextualParser0.binders :=
551 (Some (Name $2))::!TexCicTextualParser0.binders;
552 let dom,mk_expr = $4 in
553 Cic.Name $2, (dom, function interp -> mk_expr interp)
556 { TexCicTextualParser0.binders :=
557 (Some Anonymous)::!TexCicTextualParser0.binders ;
558 let dom,mk_expr = $1 in
559 Anonymous, (dom, function interp -> mk_expr interp)
562 { TexCicTextualParser0.binders :=
563 (Some (Name $2))::!TexCicTextualParser0.binders;
564 let newmeta = new_meta () in
565 let new_canonical_context = [] in
567 identity_relocation_list_for_metavariable new_canonical_context
569 TexCicTextualParser0.metasenv :=
570 [newmeta, new_canonical_context, Sort Type ;
571 newmeta+1, new_canonical_context, Meta (newmeta,irl)
572 ] @ !TexCicTextualParser0.metasenv ;
573 Cic.Name $2, ([], function _ -> Meta (newmeta+1,irl))
577 LAMBDA ID COLON expr DOT
578 { TexCicTextualParser0.binders :=
579 (Some (Name $2))::!TexCicTextualParser0.binders;
580 let dom,mk_expr = $4 in
581 Cic.Name $2, (dom, function interp -> mk_expr interp)
584 { TexCicTextualParser0.binders :=
585 (Some (Name $2))::!TexCicTextualParser0.binders;
586 let newmeta = new_meta () in
587 let new_canonical_context = [] in
589 identity_relocation_list_for_metavariable new_canonical_context
591 TexCicTextualParser0.metasenv :=
592 [newmeta, new_canonical_context, Sort Type ;
593 newmeta+1, new_canonical_context, Meta (newmeta,irl)
594 ] @ !TexCicTextualParser0.metasenv ;
595 Cic.Name $2, ([], function _ -> Meta (newmeta+1,irl))
599 LAMBDA ID LETIN expr DOT
600 { TexCicTextualParser0.binders :=
601 (Some (Name $2))::!TexCicTextualParser0.binders ;
602 let dom,mk_expr = $4 in
603 Cic.Name $2, (dom, function interp -> mk_expr interp)
607 { [], function _ -> [] }
608 | expr SEMICOLON branches
609 { let dom1,mk_expr = $1 in
610 let dom2,mk_branches = $3 in
611 let dom = union dom1 dom2 in
612 dom, function interp -> (mk_expr interp)::(mk_branches interp)
615 { let dom,mk_expr = $1 in
616 dom, function interp -> [mk_expr interp]
621 { 0, [], function _ -> [] }
623 { let dom1,mk_expr = $1 in
624 let length,dom2,mk_exprlist = $2 in
625 let dom = union dom1 dom2 in
626 length+1, dom, function interp -> (mk_expr interp)::(mk_exprlist interp)
631 { let dom,mk_expr = $1 in
632 dom, function interp -> [mk_expr interp]
634 | expr SEMICOLON exprseplist
635 { let dom1,mk_expr = $1 in
636 let dom2,mk_exprseplist = $3 in
637 let dom = union dom1 dom2 in
638 dom, function interp -> (mk_expr interp)::(mk_exprseplist interp)
642 { [], function _ -> [] }
643 | expr SEMICOLON substitutionlist
644 { let dom1,mk_expr = $1 in
645 let dom2,mk_substitutionlist = $3 in
646 let dom = union dom1 dom2 in
648 function interp ->(Some (mk_expr interp))::(mk_substitutionlist interp)
650 | NONE SEMICOLON substitutionlist
651 { let dom,mk_exprsubstitutionlist = $3 in
652 dom, function interp -> None::(mk_exprsubstitutionlist interp)