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,!CicTextualParser0.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 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)
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 CPROP CAST IMPLICIT NONE
159 %token LETIN FIX COFIX SEMICOLON LCURLY RCURLY CASE ARROW LBRACKET RBRACKET EOF
162 %type <CicTextualParser0.interpretation_domain_item list * (CicTextualParser0.interpretation -> Cic.term)> main
165 | EOF { raise CicTextualParser0.Eof } /* FG: was never raised */
167 | expr SEMICOLON { $1 } /* FG: to read several terms in a row
168 * Do we need to clear some static variables?
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)
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)
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)
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)
191 Rel (get_index_in_list (Name $1) !CicTextualParser0.binders)
194 None -> ([], function _ -> res)
195 | Some _ -> raise (ExplicitNamedSubstitutionAppliedToRel)
199 let dom1,mk_exp_named_subst = deoptionize_exp_named_subst $2 in
200 let dom = union dom1 [CicTextualParser0.Id $1] in
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) ->
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
221 (fst $5,snd $5,(mk_expr2 interp),(mk_expr1 interp),(mk_expr3 interp))
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
230 let uri,typeno = indty_uri_of_id $5 interp in
232 (uri,typeno,(mk_expr2 interp),(mk_expr1 interp),
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
244 let types = mk_types interp in
245 let fixfunsbodies = (mk_exprseplist interp) in
249 [] -> raise Not_found
250 | (name,_)::_ when name = foo -> idx
251 | _::tl -> find (idx+1) tl
253 find 0 ids_and_indexes
256 List.map2 (fun ((name,recindex),ty) bo -> (name,recindex,ty,bo))
257 (List.combine ids_and_indexes types) fixfunsbodies
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
267 let types = mk_types interp in
268 let fixfunsbodies = (mk_exprseplist interp) in
272 [] -> raise Not_found
273 | name::_ when name = foo -> idx
274 | _::tl -> find (idx+1) tl
279 List.map2 (fun (name,ty) bo -> (name,ty,bo))
280 (List.combine ids types) fixfunsbodies
282 for i = 1 to List.length fixfuns do
283 CicTextualParser0.binders := List.tl !CicTextualParser0.binders
289 | SET { [], function _ -> Sort Set }
290 | PROP { [], function _ -> Sort Prop }
291 | TYPE { [], function _ -> Sort Type }
292 | CPROP { [], function _ -> Sort CProp }
293 | LPAREN expr CAST expr RPAREN
294 { let dom1,mk_expr1 = $2 in
295 let dom2,mk_expr2 = $4 in
296 let dom = union dom1 dom2 in
297 dom, function interp -> Cast ((mk_expr1 interp),(mk_expr2 interp))
299 | META LBRACKET substitutionlist RBRACKET
300 { let dom,mk_substitutionlist = $3 in
301 dom, function interp -> Meta ($1, mk_substitutionlist interp)
303 | LPAREN expr exprlist RPAREN
304 { let length,dom2,mk_exprlist = $3 in
308 let dom1,mk_expr1 = $2 in
309 let dom = union dom1 dom2 in
312 Appl ((mk_expr1 interp)::(mk_exprlist interp))
317 | LCURLY named_substs RCURLY
322 { let dom,mk_expr = $3 in
323 dom, function interp -> [$1, mk_expr interp] }
325 { let dom1,mk_expr = $3 in
326 let dom = union [CicTextualParser0.Id $1] dom1 in
327 dom, function interp -> [var_uri_of_id $1 interp, mk_expr interp] }
328 | VARURI LETIN expr2 SEMICOLON named_substs
329 { let dom1,mk_expr = $3 in
330 let dom2,mk_named_substs = $5 in
331 let dom = union dom1 dom2 in
332 dom, function interp -> ($1, mk_expr interp)::(mk_named_substs interp)
334 | ID LETIN expr2 SEMICOLON named_substs
335 { let dom1,mk_expr = $3 in
336 let dom2,mk_named_substs = $5 in
337 let dom = union [CicTextualParser0.Id $1] (union dom1 dom2) in
340 (var_uri_of_id $1 interp, mk_expr interp)::(mk_named_substs interp)
345 { CicTextualParser0.binders := List.tl !CicTextualParser0.binders ;
346 let dom1,mk_expr1 = snd $1 in
347 let dom2,mk_expr2 = $2 in
348 let dom = union dom1 dom2 in
349 dom, function interp -> Prod (fst $1, mk_expr1 interp, mk_expr2 interp)
352 { CicTextualParser0.binders := List.tl !CicTextualParser0.binders ;
353 let dom1,mk_expr1 = snd $1 in
354 let dom2,mk_expr2 = $2 in
355 let dom = union dom1 dom2 in
356 dom,function interp -> Lambda (fst $1, mk_expr1 interp, mk_expr2 interp)
359 { CicTextualParser0.binders := List.tl !CicTextualParser0.binders ;
360 let dom1,mk_expr1 = snd $1 in
361 let dom2,mk_expr2 = $2 in
362 let dom = union dom1 dom2 in
363 dom, function interp -> LetIn (fst $1, mk_expr1 interp, mk_expr2 interp)
369 FIX ID LCURLY fixfunsdecl RCURLY
370 { let dom,ids_and_indexes,mk_types = $4 in
372 List.rev_map (function (name,_) -> Some (Name name)) ids_and_indexes
374 CicTextualParser0.binders := bs@(!CicTextualParser0.binders) ;
375 dom, $2, ids_and_indexes, mk_types
379 ID LPAREN NUM RPAREN COLON expr
380 { let dom,mk_expr = $6 in
381 dom, [$1,$3], function interp -> [mk_expr interp]
383 | ID LPAREN NUM RPAREN COLON expr SEMICOLON fixfunsdecl
384 { let dom1,mk_expr = $6 in
385 let dom2,ids_and_indexes,mk_types = $8 in
386 let dom = union dom1 dom2 in
387 dom, ($1,$3)::ids_and_indexes,
388 function interp -> (mk_expr interp)::(mk_types interp)
392 COFIX ID LCURLY cofixfunsdecl RCURLY
393 { let dom,ids,mk_types = $4 in
395 List.rev_map (function name -> Some (Name name)) ids
397 CicTextualParser0.binders := bs@(!CicTextualParser0.binders) ;
398 dom, $2, ids, mk_types
403 { let dom,mk_expr = $3 in
404 dom, [$1], function interp -> [mk_expr interp]
406 | ID COLON expr SEMICOLON cofixfunsdecl
407 { let dom1,mk_expr = $3 in
408 let dom2,ids,mk_types = $5 in
409 let dom = union dom1 dom2 in
411 function interp -> (mk_expr interp)::(mk_types interp)
415 PROD ID COLON expr DOT
416 { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders;
417 let dom,mk_expr = $4 in
418 Cic.Name $2, (dom, function interp -> mk_expr interp)
421 { CicTextualParser0.binders := (Some Anonymous)::!CicTextualParser0.binders ;
422 let dom,mk_expr = $1 in
423 Anonymous, (dom, function interp -> mk_expr interp)
426 { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders;
427 let newmeta = new_meta () in
428 let new_canonical_context = [] in
430 identity_relocation_list_for_metavariable new_canonical_context
432 CicTextualParser0.metasenv :=
433 [newmeta, new_canonical_context, Sort Type ;
434 newmeta+1, new_canonical_context, Meta (newmeta,irl)
435 ] @ !CicTextualParser0.metasenv ;
436 Cic.Name $2, ([], function _ -> Meta (newmeta+1,irl))
440 LAMBDA ID COLON expr DOT
441 { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders;
442 let dom,mk_expr = $4 in
443 Cic.Name $2, (dom, function interp -> mk_expr interp)
446 { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders;
447 let newmeta = new_meta () in
448 let new_canonical_context = [] in
450 identity_relocation_list_for_metavariable new_canonical_context
452 CicTextualParser0.metasenv :=
453 [newmeta, new_canonical_context, Sort Type ;
454 newmeta+1, new_canonical_context, Meta (newmeta,irl)
455 ] @ !CicTextualParser0.metasenv ;
456 Cic.Name $2, ([], function _ -> Meta (newmeta+1,irl))
460 LAMBDA ID LETIN expr DOT
461 { CicTextualParser0.binders := (Some (Name $2))::!CicTextualParser0.binders ;
462 let dom,mk_expr = $4 in
463 Cic.Name $2, (dom, function interp -> mk_expr interp)
467 { [], function _ -> [] }
468 | expr SEMICOLON branches
469 { let dom1,mk_expr = $1 in
470 let dom2,mk_branches = $3 in
471 let dom = union dom1 dom2 in
472 dom, function interp -> (mk_expr interp)::(mk_branches interp)
475 { let dom,mk_expr = $1 in
476 dom, function interp -> [mk_expr interp]
481 { 0, [], function _ -> [] }
483 { let dom1,mk_expr = $1 in
484 let length,dom2,mk_exprlist = $2 in
485 let dom = union dom1 dom2 in
486 length+1, dom, function interp -> (mk_expr interp)::(mk_exprlist interp)
491 { let dom,mk_expr = $1 in
492 dom, function interp -> [mk_expr interp]
494 | expr SEMICOLON exprseplist
495 { let dom1,mk_expr = $1 in
496 let dom2,mk_exprseplist = $3 in
497 let dom = union dom1 dom2 in
498 dom, function interp -> (mk_expr interp)::(mk_exprseplist interp)
502 { [], function _ -> [] }
503 | expr SEMICOLON substitutionlist
504 { let dom1,mk_expr = $1 in
505 let dom2,mk_substitutionlist = $3 in
506 let dom = union dom1 dom2 in
508 function interp ->(Some (mk_expr interp))::(mk_substitutionlist interp)
510 | NONE SEMICOLON substitutionlist
511 { let dom,mk_exprsubstitutionlist = $3 in
512 dom, function interp -> None::(mk_exprsubstitutionlist interp)