1 (* Copyright (C) 2002, 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 exception Bad_pattern of string Lazy.t
30 let new_meta_of_proof ~proof:(_, metasenv, subst, _, _, _) =
31 CicMkImplicit.new_meta metasenv subst
33 let subst_meta_in_proof proof meta term newmetasenv =
34 let uri,metasenv,initial_subst,bo,ty, attrs = proof in
35 (* empty context is ok for term since it wont be used by apply_subst *)
36 (* hack: since we do not know the context and the type of term, we
37 create a substitution with cc =[] and type = Implicit; they will be
38 in any case dropped by apply_subst, but it would be better to rewrite
39 the code. Cannot we just use apply_subst_metasenv, etc. ?? *)
40 let subst_in = CicMetaSubst.apply_subst [meta,([], term,Cic.Implicit None)] in
42 newmetasenv @ (List.filter (function (m,_,_) -> m <> meta) metasenv)
46 (function i,canonical_context,ty ->
47 let canonical_context' =
50 Some (n,Cic.Decl s) -> Some (n,Cic.Decl (subst_in s))
52 | Some (n,Cic.Def (bo,ty)) ->
53 Some (n,Cic.Def (subst_in bo,subst_in ty))
56 i,canonical_context',(subst_in ty)
59 let bo' = lazy (subst_in (Lazy.force bo)) in
60 (* Metavariables can appear also in the *statement* of the theorem
61 * since the parser does not reject as statements terms with
62 * metavariable therein *)
63 let ty' = subst_in ty in
64 let newproof = uri,metasenv'',initial_subst,bo',ty', attrs in
65 (newproof, metasenv'')
67 (*CSC: commento vecchio *)
68 (* refine_meta_with_brand_new_metasenv meta term subst_in newmetasenv *)
69 (* This (heavy) function must be called when a tactic can instantiate old *)
70 (* metavariables (i.e. existential variables). It substitues the metasenv *)
71 (* of the proof with the result of removing [meta] from the domain of *)
72 (* [newmetasenv]. Then it replaces Cic.Meta [meta] with [term] everywhere *)
73 (* in the current proof. Finally it applies [apply_subst_replacing] to *)
75 (*CSC: A questo punto perche' passare un bo' gia' istantiato, se tanto poi *)
76 (*CSC: ci ripasso sopra apply_subst!!! *)
77 (*CSC: Attenzione! Ora questa funzione applica anche [subst_in] a *)
78 (*CSC: [newmetasenv]. *)
79 let subst_meta_and_metasenv_in_proof proof meta subst newmetasenv =
80 let (uri,_,initial_subst,bo,ty, attrs) = proof in
81 let subst_in = CicMetaSubst.apply_subst subst in
82 let bo' = lazy (subst_in (Lazy.force bo)) in
83 (* Metavariables can appear also in the *statement* of the theorem
84 * since the parser does not reject as statements terms with
85 * metavariable therein *)
86 let ty' = subst_in ty in
89 (fun metasenv_entry i ->
90 match metasenv_entry with
91 (m,canonical_context,ty) when m <> meta ->
92 let canonical_context' =
96 | Some (i,Cic.Decl t) -> Some (i,Cic.Decl (subst_in t))
97 | Some (i,Cic.Def (bo,ty)) ->
98 Some (i,Cic.Def (subst_in bo,subst_in ty))
101 (m,canonical_context',subst_in ty)::i
105 (* qui da capire se per la fase transitoria si fa initial_subst @ subst
107 let newproof = uri,metasenv',subst,bo',ty', attrs in
108 (newproof, metasenv')
110 let compare_metasenvs ~oldmetasenv ~newmetasenv =
111 List.map (function (i,_,_) -> i)
114 not (List.exists (fun (j,_,_) -> i=j) oldmetasenv)) newmetasenv)
117 (** finds the _pointers_ to subterms that are alpha-equivalent to wanted in t *)
118 let find_subterms ~subst ~metasenv ~ugraph ~wanted ~context t =
119 let rec find subst metasenv ugraph context w t =
121 let subst,metasenv,ugraph =
122 CicUnification.fo_unif_subst subst context metasenv w t ugraph
124 subst,metasenv,ugraph,[context,t]
126 CicUnification.UnificationFailure _
127 | CicUnification.Uncertain _ ->
130 | Cic.Rel _ -> subst,metasenv,ugraph,[]
131 | Cic.Meta (_, ctx) ->
133 fun (subst,metasenv,ugraph,acc) e ->
135 | None -> subst,metasenv,ugraph,acc
137 let subst,metasenv,ugraph,res =
138 find subst metasenv ugraph context w t
140 subst,metasenv,ugraph, res @ acc
141 ) (subst,metasenv,ugraph,[]) ctx
142 | Cic.Lambda (name, t1, t2)
143 | Cic.Prod (name, t1, t2) ->
144 let subst,metasenv,ugraph,rest1 =
145 find subst metasenv ugraph context w t1 in
146 let subst,metasenv,ugraph,rest2 =
147 find subst metasenv ugraph (Some (name, Cic.Decl t1)::context)
148 (CicSubstitution.lift 1 w) t2
150 subst,metasenv,ugraph,rest1 @ rest2
151 | Cic.LetIn (name, t1, t2, t3) ->
152 let subst,metasenv,ugraph,rest1 =
153 find subst metasenv ugraph context w t1 in
154 let subst,metasenv,ugraph,rest2 =
155 find subst metasenv ugraph context w t2 in
156 let subst,metasenv,ugraph,rest3 =
157 find subst metasenv ugraph (Some (name, Cic.Def (t1,t2))::context)
158 (CicSubstitution.lift 1 w) t3
160 subst,metasenv,ugraph,rest1 @ rest2 @ rest3
163 (fun (subst,metasenv,ugraph,acc) t ->
164 let subst,metasenv,ugraph,res =
165 find subst metasenv ugraph context w t
167 subst,metasenv,ugraph,res @ acc)
168 (subst,metasenv,ugraph,[]) l
169 | Cic.Cast (t, ty) ->
170 let subst,metasenv,ugraph,rest =
171 find subst metasenv ugraph context w t in
172 let subst,metasenv,ugraph,resty =
173 find subst metasenv ugraph context w ty
175 subst,metasenv,ugraph,rest @ resty
176 | Cic.Implicit _ -> assert false
177 | Cic.Const (_, esubst)
178 | Cic.Var (_, esubst)
179 | Cic.MutInd (_, _, esubst)
180 | Cic.MutConstruct (_, _, _, esubst) ->
182 (fun (subst,metasenv,ugraph,acc) (_, t) ->
183 let subst,metasenv,ugraph,res =
184 find subst metasenv ugraph context w t
186 subst,metasenv,ugraph,res @ acc)
187 (subst,metasenv,ugraph,[]) esubst
188 | Cic.MutCase (_, _, outty, indterm, patterns) ->
189 let subst,metasenv,ugraph,resoutty =
190 find subst metasenv ugraph context w outty in
191 let subst,metasenv,ugraph,resindterm =
192 find subst metasenv ugraph context w indterm in
193 let subst,metasenv,ugraph,respatterns =
195 (fun (subst,metasenv,ugraph,acc) p ->
196 let subst,metaseng,ugraph,res =
197 find subst metasenv ugraph context w p
199 subst,metasenv,ugraph,res @ acc
200 ) (subst,metasenv,ugraph,[]) patterns
202 subst,metasenv,ugraph,resoutty @ resindterm @ respatterns
203 | Cic.Fix (_, funl) ->
205 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funl
208 fun (subst,metasenv,ugraph,acc) (_, _, ty, bo) ->
209 let subst,metasenv,ugraph,resty =
210 find subst metasenv ugraph context w ty in
211 let subst,metasenv,ugraph,resbo =
212 find subst metasenv ugraph (tys @ context) w bo
214 subst,metasenv,ugraph, resty @ resbo @ acc
215 ) (subst,metasenv,ugraph,[]) funl
216 | Cic.CoFix (_, funl) ->
218 List.map (fun (n,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funl
221 fun (subst,metasenv,ugraph,acc) (_, ty, bo) ->
222 let subst,metasenv,ugraph,resty =
223 find subst metasenv ugraph context w ty in
224 let subst,metasenv,ugraph,resbo =
225 find subst metasenv ugraph (tys @ context) w bo
227 subst,metasenv,ugraph, resty @ resbo @ acc
228 ) (subst,metasenv,ugraph,[]) funl
230 find subst metasenv ugraph context wanted t
233 ~metasenv ~subst ~context ~ugraph ~term ~pattern:(wanted,where)
235 let add_ctx context name entry = (Some (name, entry)) :: context in
236 let map2 error_msg f l1 l2 =
240 | Invalid_argument _ -> raise (Bad_pattern (lazy error_msg))
242 let rec aux context where term =
243 match (where, term) with
244 | Cic.Implicit (Some `Hole), t -> [context,t]
245 | Cic.Implicit (Some `Type), t -> []
246 | Cic.Implicit None,_ -> []
247 | Cic.Meta (_, ctxt1), Cic.Meta (_, ctxt2) ->
249 (map2 "wrong number of argument in explicit substitution"
252 Some t1, Some t2 -> aux context t1 t2
255 | Cic.Cast (te1, ty1), Cic.Cast (te2, ty2) ->
256 aux context te1 te2 @ aux context ty1 ty2
257 | Cic.Prod (Cic.Anonymous, s1, t1), Cic.Prod (name, s2, t2)
258 | Cic.Lambda (Cic.Anonymous, s1, t1), Cic.Lambda (name, s2, t2) ->
259 aux context s1 s2 @ aux (add_ctx context name (Cic.Decl s2)) t1 t2
260 | Cic.Prod (Cic.Name n1, s1, t1),
261 Cic.Prod ((Cic.Name n2) as name , s2, t2)
262 | Cic.Lambda (Cic.Name n1, s1, t1),
263 Cic.Lambda ((Cic.Name n2) as name, s2, t2) when n1 = n2->
264 aux context s1 s2 @ aux (add_ctx context name (Cic.Decl s2)) t1 t2
265 | Cic.Prod (name1, s1, t1), Cic.Prod (name2, s2, t2)
266 | Cic.Lambda (name1, s1, t1), Cic.Lambda (name2, s2, t2) -> []
267 | Cic.LetIn (Cic.Anonymous, s1, ty1, t1), Cic.LetIn (name, s2, ty2, t2) ->
269 aux context ty1 ty2 @
270 aux (add_ctx context name (Cic.Def (s2,ty2))) t1 t2
271 | Cic.LetIn (Cic.Name n1, s1, ty1, t1),
272 Cic.LetIn ((Cic.Name n2) as name, s2, ty2, t2) when n1 = n2->
274 aux context ty1 ty2 @
275 aux (add_ctx context name (Cic.Def (s2,ty2))) t1 t2
276 | Cic.LetIn (name1, s1, ty1, t1), Cic.LetIn (name2, s2, ty2, t2) -> []
277 | Cic.Appl terms1, Cic.Appl terms2 -> auxs context terms1 terms2
278 | Cic.Var (_, subst1), Cic.Var (_, subst2)
279 | Cic.Const (_, subst1), Cic.Const (_, subst2)
280 | Cic.MutInd (_, _, subst1), Cic.MutInd (_, _, subst2)
281 | Cic.MutConstruct (_, _, _, subst1), Cic.MutConstruct (_, _, _, subst2) ->
282 auxs context (List.map snd subst1) (List.map snd subst2)
283 | Cic.MutCase (_, _, out1, t1, pat1), Cic.MutCase (_ , _, out2, t2, pat2) ->
284 aux context out1 out2 @ aux context t1 t2 @ auxs context pat1 pat2
285 | Cic.Fix (_, funs1), Cic.Fix (_, funs2) ->
287 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs2
290 (map2 "wrong number of mutually recursive functions"
291 (fun (_, _, ty1, bo1) (_, _, ty2, bo2) ->
292 aux context ty1 ty2 @ aux (tys @ context) bo1 bo2)
294 | Cic.CoFix (_, funs1), Cic.CoFix (_, funs2) ->
296 List.map (fun (n,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs2
299 (map2 "wrong number of mutually co-recursive functions"
300 (fun (_, ty1, bo1) (_, ty2, bo2) ->
301 aux context ty1 ty2 @ aux (tys @ context) bo1 bo2)
305 (lazy (Printf.sprintf "Pattern %s versus term %s"
308 and auxs context terms1 terms2 = (* as aux for list of terms *)
309 List.concat (map2 "wrong number of arguments in application"
310 (fun t1 t2 -> aux context t1 t2) terms1 terms2)
315 | Some where -> aux context where term
318 None -> subst,metasenv,ugraph,roots
320 let rec find_in_roots subst =
322 [] -> subst,metasenv,ugraph,[]
323 | (context',where)::tl ->
324 let subst,metasenv,ugraph,tl' = find_in_roots subst tl in
325 let subst,metasenv,ugraph,found =
326 let wanted, metasenv, ugraph = wanted context' metasenv ugraph in
327 find_subterms ~subst ~metasenv ~ugraph ~wanted ~context:context'
330 subst,metasenv,ugraph,found @ tl'
332 find_in_roots subst roots
335 (** create a pattern from a term and a list of subterms.
336 * the pattern is granted to have a ? for every subterm that has no selected
338 * @param equality equality function used while walking the term. Defaults to
339 * physical equality (==) *)
340 let pattern_of ?(equality=(==)) ~term terms =
341 let (===) x y = equality x y in
342 let not_found = false, Cic.Implicit None in
345 | t when List.exists (fun t' -> t === t') terms ->
346 true,Cic.Implicit (Some `Hole)
347 | Cic.Var (uri, subst) ->
348 let b,subst = aux_subst subst in
350 true,Cic.Var (uri, subst)
353 | Cic.Meta (i, ctxt) ->
359 | Some t -> let bt,t = aux t in b||bt ,Some t::ctxt
363 true,Cic.Meta (i, ctxt)
366 | Cic.Cast (te, ty) ->
367 let b1,te = aux te in
368 let b2,ty = aux ty in
369 if b1||b2 then true,Cic.Cast (te, ty)
372 | Cic.Prod (_, s, t) ->
376 true, Cic.Prod (Cic.Anonymous, s, t)
379 | Cic.Lambda (_, s, t) ->
383 true, Cic.Lambda (Cic.Anonymous, s, t)
386 | Cic.LetIn (_, s, ty, t) ->
388 let b2,ty = aux ty in
391 true, Cic.LetIn (Cic.Anonymous, s, ty, t)
406 | Cic.Const (uri, subst) ->
407 let b,subst = aux_subst subst in
409 true, Cic.Const (uri, subst)
412 | Cic.MutInd (uri, tyno, subst) ->
413 let b,subst = aux_subst subst in
415 true, Cic.MutInd (uri, tyno, subst)
418 | Cic.MutConstruct (uri, tyno, consno, subst) ->
419 let b,subst = aux_subst subst in
421 true, Cic.MutConstruct (uri, tyno, consno, subst)
424 | Cic.MutCase (uri, tyno, outty, t, pat) ->
425 let b1,outty = aux outty in
434 if b1 || b2 || b3 then
435 true, Cic.MutCase (uri, tyno, outty, t, pat)
438 | Cic.Fix (funno, funs) ->
441 (fun (name, i, ty, bo) (b,funs) ->
442 let b1,ty = aux ty in
443 let b2,bo = aux bo in
444 b||b1||b2, (name, i, ty, bo)::funs) funs (false,[])
447 true, Cic.Fix (funno, funs)
450 | Cic.CoFix (funno, funs) ->
453 (fun (name, ty, bo) (b,funs) ->
454 let b1,ty = aux ty in
455 let b2,bo = aux bo in
456 b||b1||b2, (name, ty, bo)::funs) funs (false,[])
459 true, Cic.CoFix (funno, funs)
464 | Cic.Implicit _ -> not_found
465 and aux_subst subst =
467 (fun (uri, t) (b,subst) ->
469 b||b1,(uri, t)::subst) subst (false,[])
473 exception Fail of string Lazy.t
475 (** select metasenv conjecture pattern
476 * select all subterms of [conjecture] matching [pattern].
477 * It returns the set of matched terms (that can be compared using physical
478 * equality to the subterms of [conjecture]) together with their contexts.
479 * The representation of the set mimics the ProofEngineTypes.pattern type:
480 * a list of hypothesis (names of) together with the list of its matched
481 * subterms (and their contexts) + the list of matched subterms of the
482 * with their context conclusion. Note: in the result the list of hypothesis
483 * has an entry for each entry in the context and in the same order.
484 * Of course the list of terms (with their context) associated to the
485 * hypothesis name may be empty.
489 let select ~metasenv ~subst ~ugraph ~conjecture:(_,context,ty)
490 ~(pattern: (Cic.term, Cic.lazy_term) ProofEngineTypes.pattern)
492 let what, hyp_patterns, goal_pattern = pattern in
493 let find_pattern_for name =
494 try Some (snd (List.find (fun (n, pat) -> Cic.Name n = name) hyp_patterns))
495 with Not_found -> None in
496 (* Multiple hypotheses with the same name can be in the context.
497 In this case we need to pick the last one, but we will perform
498 a fold_right on the context. Thus we pre-process hyp_patterns. *)
499 let full_hyp_pattern =
500 let rec aux blacklist =
503 | None::tl -> None::aux blacklist tl
504 | Some (name,_)::tl ->
505 if List.mem name blacklist then
506 None::aux blacklist tl
508 find_pattern_for name::aux (name::blacklist) tl
512 let subst,metasenv,ugraph,ty_terms =
513 select_in_term ~metasenv ~subst ~context ~ugraph ~term:ty
514 ~pattern:(what,goal_pattern)
516 let subst,metasenv,ugraph,context_terms =
517 let subst,metasenv,ugraph,res,_ =
519 (fun (pattern,entry) (subst,metasenv,ugraph,res,context) ->
521 None -> subst,metasenv,ugraph,None::res,None::context
522 | Some (name,Cic.Decl term) ->
525 subst,metasenv,ugraph,((Some (`Decl []))::res),(entry::context)
527 let subst,metasenv,ugraph,terms =
528 select_in_term ~subst ~metasenv ~context ~ugraph ~term
529 ~pattern:(what, Some pat)
531 subst,metasenv,ugraph,((Some (`Decl terms))::res),
533 | Some (name,Cic.Def (bo, ty)) ->
536 let selected_ty = [] in
537 subst,metasenv,ugraph,((Some (`Def ([],selected_ty)))::res),
540 let subst,metasenv,ugraph,terms_bo =
541 select_in_term ~subst ~metasenv ~context ~ugraph ~term:bo
542 ~pattern:(what, Some pat) in
543 let subst,metasenv,ugraph,terms_ty =
544 let subst,metasenv,ugraph,res =
545 select_in_term ~subst ~metasenv ~context ~ugraph ~term:ty
546 ~pattern:(what, Some pat)
548 subst,metasenv,ugraph,res
550 subst,metasenv,ugraph,((Some (`Def (terms_bo,terms_ty)))::res),
552 ) (List.combine full_hyp_pattern context) (subst,metasenv,ugraph,[],[]))
554 subst,metasenv,ugraph,res
556 subst,metasenv,ugraph,context_terms, ty_terms
559 (** locate_in_term equality what where context
560 * [what] must match a subterm of [where] according to [equality]
561 * It returns the matched terms together with their contexts in [where]
562 * [equality] defaults to physical equality
563 * [context] must be the context of [where]
565 let locate_in_term ?(equality=(fun _ -> (==))) what ~where context =
566 let add_ctx context name entry =
567 (Some (name, entry)) :: context in
568 let rec aux context where =
569 if equality context what where then [context,where]
579 | Cic.MutConstruct _ -> []
580 | Cic.Cast (te, ty) -> aux context te @ aux context ty
581 | Cic.Prod (name, s, t)
582 | Cic.Lambda (name, s, t) ->
583 aux context s @ aux (add_ctx context name (Cic.Decl s)) t
584 | Cic.LetIn (name, s, ty, t) ->
587 aux (add_ctx context name (Cic.Def (s,ty))) t
588 | Cic.Appl tl -> auxs context tl
589 | Cic.MutCase (_, _, out, t, pat) ->
590 aux context out @ aux context t @ auxs context pat
591 | Cic.Fix (_, funs) ->
593 List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs
597 (fun (_, _, ty, bo) ->
598 aux context ty @ aux (tys @ context) bo)
600 | Cic.CoFix (_, funs) ->
602 List.map (fun (n,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs
607 aux context ty @ aux (tys @ context) bo)
609 and auxs context tl = (* as aux for list of terms *)
610 List.concat (List.map (fun t -> aux context t) tl)
614 (** locate_in_conjecture equality what where context
615 * [what] must match a subterm of [where] according to [equality]
616 * It returns the matched terms together with their contexts in [where]
617 * [equality] defaults to physical equality
618 * [context] must be the context of [where]
620 let locate_in_conjecture ?(equality=fun _ -> (==)) what (_,context,ty) =
623 (fun entry (context,res) ->
625 None -> entry::context, res
626 | Some (_, Cic.Decl ty) ->
627 let res = res @ locate_in_term what ~where:ty context in
628 let context' = entry::context in
630 | Some (_, Cic.Def (bo,ty)) ->
631 let res = res @ locate_in_term what ~where:bo context in
632 let res = res @ locate_in_term what ~where:ty context in
633 let context' = entry::context in
637 res @ locate_in_term what ~where:ty context
639 let lookup_type metasenv context hyp =
640 let rec aux p = function
641 | Some (Cic.Name name, Cic.Decl t) :: _ when name = hyp -> p, t
642 | Some (Cic.Name name, Cic.Def (_,t)) :: _ when name = hyp -> p, t
643 | _ :: tail -> aux (succ p) tail
644 | [] -> raise (ProofEngineTypes.Fail (lazy "lookup_type: not premise in the current goal"))
652 | Some (Cic.Name s,Cic.Decl ty)::_ when name = s ->
653 Cic.Rel n, CicSubstitution.lift n ty
654 | Some (Cic.Name s,Cic.Def _)::_ when name = s -> assert false (*CSC: not implemented yet! But does this make any sense?*)
655 | _::tl -> find_hyp (n+1) tl
660 (* sort pattern hypotheses from the smallest to the highest Rel *)
661 let sort_pattern_hyps context (t,hpatterns,cpattern) =
664 (fun (id1,_) (id2,_) ->
665 let t1,_ = find_hyp id1 context in
666 let t2,_ = find_hyp id2 context in
668 Cic.Rel n1, Cic.Rel n2 -> compare n1 n2
669 | _,_ -> assert false) hpatterns
674 (* FG: **********************************************************************)
676 let get_name context index =
677 try match List.nth context (pred index) with
678 | Some (Cic.Name name, _) -> Some name
680 with Invalid_argument "List.nth" -> None
682 let get_rel context name =
683 let rec aux i = function
685 | Some (Cic.Name s, _) :: _ when s = name -> Some (Cic.Rel i)
686 | _ :: tl -> aux (succ i) tl
690 let split_with_whd (c, t) =
691 let add s v c = Some (s, Cic.Decl v) :: c in
692 let rec aux whd a n c = function
693 | Cic.Prod (s, v, t) -> aux false ((c, v) :: a) (succ n) (add s v c) t
694 | v when whd -> (c, v) :: a, n
695 | v -> aux true a n c (CicReduction.whd c v)
699 let split_with_normalize (c, t) =
700 let add s v c = Some (s, Cic.Decl v) :: c in
701 let rec aux a n c = function
702 | Cic.Prod (s, v, t) -> aux ((c, v) :: a) (succ n) (add s v c) t
703 | v -> (c, v) :: a, n
705 aux [] 0 c (CicReduction.normalize c t)
710 type t = Cic.conjecture
711 let compare (i,_,_) (j,_,_) = Pervasives.compare i j
713 module MS = HTopoSort.Make(OT)
714 let relations_of_menv m c =
715 let i, ctx, ty = c in
716 let m = List.filter (fun (j,_,_) -> j <> i) m in
717 let m_ty = List.map fst (CicUtil.metas_of_term ty) in
723 | Some (_,Cic.Decl t) ->
724 List.map fst (CicUtil.metas_of_term ty)
725 | Some (_,Cic.Def (t,ty)) ->
726 List.map fst (CicUtil.metas_of_term ty) @
727 List.map fst (CicUtil.metas_of_term t))
730 let metas = HExtlib.list_uniq (List.sort compare (m_ty @ m_ctx)) in
731 List.filter (fun (i,_,_) -> List.exists ((=) i) metas) m
733 let sort_metasenv (m : Cic.metasenv) =
734 (MS.topological_sort m (relations_of_menv m) : Cic.metasenv)