]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/tactics/proofEngineHelpers.ml
test branch
[helm.git] / helm / ocaml / tactics / proofEngineHelpers.ml
1 (* Copyright (C) 2002, 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 (* $Id$ *)
27
28 exception Bad_pattern of string Lazy.t
29
30 let new_meta_of_proof ~proof:(_, metasenv, _, _) =
31   CicMkImplicit.new_meta metasenv []
32
33 let subst_meta_in_proof proof meta term newmetasenv =
34  let uri,metasenv,bo,ty = 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
41    let metasenv' =
42     newmetasenv @ (List.filter (function (m,_,_) -> m <> meta) metasenv)
43    in
44     let metasenv'' =
45      List.map
46       (function i,canonical_context,ty ->
47         let canonical_context' =
48          List.map
49           (function
50               Some (n,Cic.Decl s) -> Some (n,Cic.Decl (subst_in s))
51             | Some (n,Cic.Def (s,None)) -> Some (n,Cic.Def (subst_in s,None))
52             | None -> None
53             | Some (n,Cic.Def (bo,Some ty)) ->
54                Some (n,Cic.Def (subst_in bo,Some (subst_in ty)))
55           ) canonical_context
56         in
57          i,canonical_context',(subst_in ty)
58       ) metasenv'
59     in
60      let bo' = subst_in bo in
61      (* Metavariables can appear also in the *statement* of the theorem
62       * since the parser does not reject as statements terms with
63       * metavariable therein *)
64      let ty' = subst_in ty in
65       let newproof = uri,metasenv'',bo',ty' in
66        (newproof, metasenv'')
67
68 (*CSC: commento vecchio *)
69 (* refine_meta_with_brand_new_metasenv meta term subst_in newmetasenv     *)
70 (* This (heavy) function must be called when a tactic can instantiate old *)
71 (* metavariables (i.e. existential variables). It substitues the metasenv *)
72 (* of the proof with the result of removing [meta] from the domain of     *)
73 (* [newmetasenv]. Then it replaces Cic.Meta [meta] with [term] everywhere *)
74 (* in the current proof. Finally it applies [apply_subst_replacing] to    *)
75 (*  current proof.                                                        *)
76 (*CSC: A questo punto perche' passare un bo' gia' istantiato, se tanto poi *)
77 (*CSC: ci ripasso sopra apply_subst!!!                                     *)
78 (*CSC: Attenzione! Ora questa funzione applica anche [subst_in] a *)
79 (*CSC: [newmetasenv].                                             *)
80 let subst_meta_and_metasenv_in_proof proof meta subst_in newmetasenv =
81  let (uri,_,bo,ty) = proof in
82   let bo' = subst_in 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
87   let metasenv' =
88    List.fold_right
89     (fun metasenv_entry i ->
90       match metasenv_entry with
91          (m,canonical_context,ty) when m <> meta ->
92            let canonical_context' =
93             List.map
94              (function
95                  None -> None
96                | Some (i,Cic.Decl t) -> Some (i,Cic.Decl (subst_in t))
97                | Some (i,Cic.Def (t,None))  ->
98                   Some (i,Cic.Def (subst_in t,None))
99                | Some (i,Cic.Def (bo,Some ty)) ->
100                   Some (i,Cic.Def (subst_in bo,Some (subst_in ty)))
101              ) canonical_context
102            in
103             (m,canonical_context',subst_in ty)::i
104        | _ -> i
105     ) newmetasenv []
106   in
107    let newproof = uri,metasenv',bo',ty' in
108     (newproof, metasenv')
109
110 let compare_metasenvs ~oldmetasenv ~newmetasenv =
111  List.map (function (i,_,_) -> i)
112   (List.filter
113    (function (i,_,_) ->
114      not (List.exists (fun (j,_,_) -> i=j) oldmetasenv)) newmetasenv)
115 ;;
116
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 =
120    try
121     let subst,metasenv,ugraph =
122      CicUnification.fo_unif_subst subst context metasenv w t ugraph
123     in
124       subst,metasenv,ugraph,[context,t]
125    with
126      CicUnification.UnificationFailure _
127    | CicUnification.Uncertain _ ->
128       match t with
129       | Cic.Sort _ 
130       | Cic.Rel _ -> subst,metasenv,ugraph,[]
131       | Cic.Meta (_, ctx) -> 
132           List.fold_left (
133             fun (subst,metasenv,ugraph,acc) e -> 
134               match e with 
135               | None -> subst,metasenv,ugraph,acc 
136               | Some t ->
137                  let subst,metasenv,ugraph,res =
138                   find subst metasenv ugraph context w t
139                  in
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
149          in
150           subst,metasenv,ugraph,rest1 @ rest2
151       | Cic.LetIn (name, t1, t2) -> 
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 (Some (name, Cic.Def (t1,None))::context)
156            (CicSubstitution.lift 1 w) t2
157          in
158           subst,metasenv,ugraph,rest1 @ rest2
159       | Cic.Appl l -> 
160           List.fold_left
161            (fun (subst,metasenv,ugraph,acc) t ->
162              let subst,metasenv,ugraph,res =
163               find subst metasenv ugraph context w t
164              in
165               subst,metasenv,ugraph,res @ acc)
166            (subst,metasenv,ugraph,[]) l
167       | Cic.Cast (t, ty) ->
168          let subst,metasenv,ugraph,rest =
169           find subst metasenv ugraph context w t in
170          let subst,metasenv,ugraph,resty =
171           find subst metasenv ugraph context w ty
172          in
173           subst,metasenv,ugraph,rest @ resty
174       | Cic.Implicit _ -> assert false
175       | Cic.Const (_, esubst)
176       | Cic.Var (_, esubst) 
177       | Cic.MutInd (_, _, esubst) 
178       | Cic.MutConstruct (_, _, _, esubst) -> 
179           List.fold_left
180            (fun (subst,metasenv,ugraph,acc) (_, t) ->
181              let subst,metasenv,ugraph,res =
182               find subst metasenv ugraph context w t
183              in
184               subst,metasenv,ugraph,res @ acc)
185            (subst,metasenv,ugraph,[]) esubst
186       | Cic.MutCase (_, _, outty, indterm, patterns) -> 
187          let subst,metasenv,ugraph,resoutty =
188           find subst metasenv ugraph context w outty in
189          let subst,metasenv,ugraph,resindterm =
190           find subst metasenv ugraph context w indterm in
191          let subst,metasenv,ugraph,respatterns =
192           List.fold_left
193            (fun (subst,metasenv,ugraph,acc) p ->
194              let subst,metaseng,ugraph,res =
195               find subst metasenv ugraph context w p
196              in
197               subst,metasenv,ugraph,res @ acc
198            ) (subst,metasenv,ugraph,[]) patterns
199          in
200           subst,metasenv,ugraph,resoutty @ resindterm @ respatterns
201       | Cic.Fix (_, funl) -> 
202          let tys =
203           List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funl
204          in
205           List.fold_left (
206             fun (subst,metasenv,ugraph,acc) (_, _, ty, bo) ->
207              let subst,metasenv,ugraph,resty =
208               find subst metasenv ugraph context w ty in
209              let subst,metasenv,ugraph,resbo =
210               find subst metasenv ugraph (tys @ context) w bo
211              in
212               subst,metasenv,ugraph, resty @ resbo @ acc
213           ) (subst,metasenv,ugraph,[]) funl
214       | Cic.CoFix (_, funl) ->
215          let tys =
216           List.map (fun (n,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funl
217          in
218           List.fold_left (
219             fun (subst,metasenv,ugraph,acc) (_, ty, bo) ->
220              let subst,metasenv,ugraph,resty =
221               find subst metasenv ugraph context w ty in
222              let subst,metasenv,ugraph,resbo =
223               find subst metasenv ugraph (tys @ context) w bo
224              in
225               subst,metasenv,ugraph, resty @ resbo @ acc
226           ) (subst,metasenv,ugraph,[]) funl
227   in
228   find subst metasenv ugraph context wanted t
229   
230 let select_in_term ~metasenv ~context ~ugraph ~term ~pattern:(wanted,where) =
231   let add_ctx context name entry = (Some (name, entry)) :: context in
232   let map2 error_msg f l1 l2 = 
233     try 
234       List.map2 f l1 l2 
235     with
236     | Invalid_argument _ -> raise (Bad_pattern (lazy error_msg))
237   in
238   let rec aux context where term =
239     match (where, term) with
240     | Cic.Implicit (Some `Hole), t -> [context,t]
241     | Cic.Implicit (Some `Type), t -> []
242     | Cic.Implicit None,_ -> []
243     | Cic.Meta (_, ctxt1), Cic.Meta (_, ctxt2) ->
244         List.concat
245           (map2 "wrong number of argument in explicit substitution"
246             (fun t1 t2 ->
247               (match (t1, t2) with
248                   Some t1, Some t2 -> aux context t1 t2
249                 | _ -> []))
250             ctxt1 ctxt2)
251     | Cic.Cast (te1, ty1), Cic.Cast (te2, ty2) ->
252        aux context te1 te2 @ aux context ty1 ty2
253     | Cic.Prod (Cic.Anonymous, s1, t1), Cic.Prod (name, s2, t2)
254     | Cic.Lambda (Cic.Anonymous, s1, t1), Cic.Lambda (name, s2, t2) ->
255         aux context s1 s2 @ aux (add_ctx context name (Cic.Decl s2)) t1 t2
256     | Cic.Prod (Cic.Name n1, s1, t1), 
257       Cic.Prod ((Cic.Name n2) as name , s2, t2)
258     | Cic.Lambda (Cic.Name n1, s1, t1), 
259       Cic.Lambda ((Cic.Name n2) as name, s2, t2) when n1 = n2->
260         aux context s1 s2 @ aux (add_ctx context name (Cic.Decl s2)) t1 t2
261     | Cic.Prod (name1, s1, t1), Cic.Prod (name2, s2, t2)
262     | Cic.Lambda (name1, s1, t1), Cic.Lambda (name2, s2, t2) -> []
263     | Cic.LetIn (Cic.Anonymous, s1, t1), Cic.LetIn (name, s2, t2) -> 
264         aux context s1 s2 @ aux (add_ctx context name (Cic.Def (s2,None))) t1 t2
265     | Cic.LetIn (Cic.Name n1, s1, t1), 
266       Cic.LetIn ((Cic.Name n2) as name, s2, t2) when n1 = n2-> 
267         aux context s1 s2 @ aux (add_ctx context name (Cic.Def (s2,None))) t1 t2
268     | Cic.LetIn (name1, s1, t1), Cic.LetIn (name2, s2, t2) -> []
269     | Cic.Appl terms1, Cic.Appl terms2 -> auxs context terms1 terms2
270     | Cic.Var (_, subst1), Cic.Var (_, subst2)
271     | Cic.Const (_, subst1), Cic.Const (_, subst2)
272     | Cic.MutInd (_, _, subst1), Cic.MutInd (_, _, subst2)
273     | Cic.MutConstruct (_, _, _, subst1), Cic.MutConstruct (_, _, _, subst2) ->
274         auxs context (List.map snd subst1) (List.map snd subst2)
275     | Cic.MutCase (_, _, out1, t1, pat1), Cic.MutCase (_ , _, out2, t2, pat2) ->
276         aux context out1 out2 @ aux context t1 t2 @ auxs context pat1 pat2
277     | Cic.Fix (_, funs1), Cic.Fix (_, funs2) ->
278        let tys =
279         List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs2
280        in
281         List.concat
282           (map2 "wrong number of mutually recursive functions"
283             (fun (_, _, ty1, bo1) (_, _, ty2, bo2) -> 
284               aux context ty1 ty2 @ aux (tys @ context) bo1 bo2)
285             funs1 funs2)
286     | Cic.CoFix (_, funs1), Cic.CoFix (_, funs2) ->
287        let tys =
288         List.map (fun (n,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs2
289        in
290         List.concat
291           (map2 "wrong number of mutually co-recursive functions"
292             (fun (_, ty1, bo1) (_, ty2, bo2) ->
293               aux context ty1 ty2 @ aux (tys @ context) bo1 bo2)
294             funs1 funs2)
295     | x,y -> 
296         raise (Bad_pattern 
297                 (lazy (Printf.sprintf "Pattern %s versus term %s" 
298                   (CicPp.ppterm x)
299                   (CicPp.ppterm y))))
300   and auxs context terms1 terms2 =  (* as aux for list of terms *)
301     List.concat (map2 "wrong number of arguments in application"
302       (fun t1 t2 -> aux context t1 t2) terms1 terms2)
303   in
304    let roots =
305      match where with
306      | None -> []
307      | Some where -> aux context where term
308    in
309     match wanted with
310        None -> [],metasenv,ugraph,roots
311      | Some wanted ->
312         let rec find_in_roots =
313          function
314             [] -> [],metasenv,ugraph,[]
315           | (context',where)::tl ->
316              let subst,metasenv,ugraph,tl' = find_in_roots tl in
317              let subst,metasenv,ugraph,found =
318               let wanted, metasenv, ugraph = wanted context' metasenv ugraph in
319                find_subterms ~subst ~metasenv ~ugraph ~wanted ~context:context'
320                 where
321              in
322               subst,metasenv,ugraph,found @ tl'
323         in
324          find_in_roots roots
325
326 (** create a pattern from a term and a list of subterms.
327 * the pattern is granted to have a ? for every subterm that has no selected
328 * subterms
329 * @param equality equality function used while walking the term. Defaults to
330 * physical equality (==) *)
331 let pattern_of ?(equality=(==)) ~term terms =
332   let (===) x y = equality x y in
333   let not_found = false, Cic.Implicit None in
334   let rec aux t =
335     match t with
336     | t when List.exists (fun t' -> t === t') terms ->
337        true,Cic.Implicit (Some `Hole)
338     | Cic.Var (uri, subst) ->
339        let b,subst = aux_subst subst in
340         if b then
341          true,Cic.Var (uri, subst)
342         else
343          not_found
344     | Cic.Meta (i, ctxt) ->
345         let b,ctxt =
346           List.fold_right
347            (fun e (b,ctxt) ->
348              match e with
349                 None -> b,None::ctxt
350               | Some t -> let bt,t = aux t in b||bt ,Some t::ctxt
351            ) ctxt (false,[])
352         in
353          if b then
354           true,Cic.Meta (i, ctxt)
355          else
356           not_found
357     | Cic.Cast (te, ty) ->
358        let b1,te = aux te in
359        let b2,ty = aux ty in
360         if b1||b2 then true,Cic.Cast (te, ty)
361         else
362          not_found
363     | Cic.Prod (name, s, t) ->
364        let b1,s = aux s in
365        let b2,t = aux t in
366         if b1||b2 then
367          true, Cic.Prod (name, s, t)
368         else
369          not_found
370     | Cic.Lambda (name, s, t) ->
371        let b1,s = aux s in
372        let b2,t = aux t in
373         if b1||b2 then
374          true, Cic.Lambda (name, s, t)
375         else
376          not_found
377     | Cic.LetIn (name, s, t) ->
378        let b1,s = aux s in
379        let b2,t = aux t in
380         if b1||b2 then
381          true, Cic.LetIn (name, s, t)
382         else
383          not_found
384     | Cic.Appl terms ->
385        let b,terms =
386         List.fold_right
387          (fun t (b,terms) ->
388            let bt,t = aux t in
389             b||bt,t::terms
390          ) terms (false,[])
391        in
392         if b then
393          true,Cic.Appl terms
394         else
395          not_found
396     | Cic.Const (uri, subst) ->
397        let b,subst = aux_subst subst in
398         if b then
399          true, Cic.Const (uri, subst)
400         else
401          not_found
402     | Cic.MutInd (uri, tyno, subst) ->
403        let b,subst = aux_subst subst in
404         if b then
405          true, Cic.MutInd (uri, tyno, subst)
406         else
407          not_found
408     | Cic.MutConstruct (uri, tyno, consno, subst) ->
409        let b,subst = aux_subst subst in
410         if b then
411          true, Cic.MutConstruct (uri, tyno, consno, subst)
412         else
413          not_found
414     | Cic.MutCase (uri, tyno, outty, t, pat) ->
415        let b1,outty = aux outty in
416        let b2,t = aux t in
417        let b3,pat =
418         List.fold_right
419          (fun t (b,pat) ->
420            let bt,t = aux t in
421             bt||b,t::pat
422          ) pat (false,[])
423        in
424         if b1 || b2 || b3 then
425          true, Cic.MutCase (uri, tyno, outty, t, pat)
426         else
427          not_found
428     | Cic.Fix (funno, funs) ->
429         let b,funs =
430           List.fold_right
431            (fun (name, i, ty, bo) (b,funs) ->
432              let b1,ty = aux ty in
433              let b2,bo = aux bo in
434               b||b1||b2, (name, i, ty, bo)::funs) funs (false,[])
435         in
436          if b then
437           true, Cic.Fix (funno, funs)
438          else
439           not_found
440     | Cic.CoFix (funno, funs) ->
441         let b,funs =
442           List.fold_right
443            (fun (name, ty, bo) (b,funs) ->
444              let b1,ty = aux ty in
445              let b2,bo = aux bo in
446               b||b1||b2, (name, ty, bo)::funs) funs (false,[])
447         in
448          if b then
449           true, Cic.CoFix (funno, funs)
450          else
451           not_found
452     | Cic.Rel _
453     | Cic.Sort _
454     | Cic.Implicit _ -> not_found
455   and aux_subst subst =
456     List.fold_right
457      (fun (uri, t) (b,subst) ->
458        let b1,t = aux t in
459         b||b1,(uri, t)::subst) subst (false,[])
460   in
461    snd (aux term)
462
463 exception Fail of string Lazy.t
464
465   (** select metasenv conjecture pattern
466   * select all subterms of [conjecture] matching [pattern].
467   * It returns the set of matched terms (that can be compared using physical
468   * equality to the subterms of [conjecture]) together with their contexts.
469   * The representation of the set mimics the ProofEngineTypes.pattern type:
470   * a list of hypothesis (names of) together with the list of its matched
471   * subterms (and their contexts) + the list of matched subterms of the
472   * with their context conclusion. Note: in the result the list of hypothesis
473   * has an entry for each entry in the context and in the same order.
474   * Of course the list of terms (with their context) associated to the
475   * hypothesis name may be empty. 
476   *
477   * @raise Bad_pattern
478   * *)
479   let select ~metasenv ~ugraph ~conjecture:(_,context,ty)
480        ~(pattern: (Cic.term, Cic.lazy_term) ProofEngineTypes.pattern)
481   =
482    let what, hyp_patterns, goal_pattern = pattern in
483    let find_pattern_for name =
484      try Some (snd (List.find (fun (n, pat) -> Cic.Name n = name) hyp_patterns))
485      with Not_found -> None in
486    let subst,metasenv,ugraph,ty_terms =
487     select_in_term ~metasenv ~context ~ugraph ~term:ty
488      ~pattern:(what,goal_pattern) in
489    let subst,metasenv,ugraph,context_terms =
490     let subst,metasenv,ugraph,res,_ =
491      (List.fold_right
492       (fun entry (subst,metasenv,ugraph,res,context) ->
493         match entry with
494           None -> subst,metasenv,ugraph,(None::res),(None::context)
495         | Some (name,Cic.Decl term) ->
496             (match find_pattern_for name with
497             | None ->
498                subst,metasenv,ugraph,((Some (`Decl []))::res),(entry::context)
499             | Some pat ->
500                 let subst,metasenv,ugraph,terms =
501                  select_in_term ~metasenv ~context ~ugraph ~term
502                   ~pattern:(what, Some pat)
503                 in
504                  subst,metasenv,ugraph,((Some (`Decl terms))::res),
505                   (entry::context))
506         | Some (name,Cic.Def (bo, ty)) ->
507             (match find_pattern_for name with
508             | None ->
509                let selected_ty=match ty with None -> None | Some _ -> Some [] in
510                 subst,metasenv,ugraph,((Some (`Def ([],selected_ty)))::res),
511                  (entry::context)
512             | Some pat -> 
513                 let subst,metasenv,ugraph,terms_bo =
514                  select_in_term ~metasenv ~context ~ugraph ~term:bo
515                   ~pattern:(what, Some pat) in
516                 let subst,metasenv,ugraph,terms_ty =
517                  match ty with
518                     None -> subst,metasenv,ugraph,None
519                   | Some ty ->
520                      let subst,metasenv,ugraph,res =
521                       select_in_term ~metasenv ~context ~ugraph ~term:ty
522                        ~pattern:(what, Some pat)
523                      in
524                       subst,metasenv,ugraph,Some res
525                 in
526                  subst,metasenv,ugraph,((Some (`Def (terms_bo,terms_ty)))::res),
527                   (entry::context))
528       ) context (subst,metasenv,ugraph,[],[]))
529     in
530      subst,metasenv,ugraph,res
531    in
532     subst,metasenv,ugraph,context_terms, ty_terms
533
534 (** locate_in_term equality what where context
535 * [what] must match a subterm of [where] according to [equality]
536 * It returns the matched terms together with their contexts in [where]
537 * [equality] defaults to physical equality
538 * [context] must be the context of [where]
539 *)
540 let locate_in_term ?(equality=(fun _ -> (==))) what ~where context =
541   let add_ctx context name entry =
542       (Some (name, entry)) :: context in
543   let rec aux context where =
544    if equality context what where then [context,where]
545    else
546     match where with
547     | Cic.Implicit _
548     | Cic.Meta _
549     | Cic.Rel _
550     | Cic.Sort _
551     | Cic.Var _
552     | Cic.Const _
553     | Cic.MutInd _
554     | Cic.MutConstruct _ -> []
555     | Cic.Cast (te, ty) -> aux context te @ aux context ty
556     | Cic.Prod (name, s, t)
557     | Cic.Lambda (name, s, t) ->
558         aux context s @ aux (add_ctx context name (Cic.Decl s)) t
559     | Cic.LetIn (name, s, t) -> 
560         aux context s @ aux (add_ctx context name (Cic.Def (s,None))) t
561     | Cic.Appl tl -> auxs context tl
562     | Cic.MutCase (_, _, out, t, pat) ->
563         aux context out @ aux context t @ auxs context pat
564     | Cic.Fix (_, funs) ->
565        let tys =
566         List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs
567        in
568         List.concat
569           (List.map
570             (fun (_, _, ty, bo) -> 
571               aux context ty @ aux (tys @ context) bo)
572             funs)
573     | Cic.CoFix (_, funs) ->
574        let tys =
575         List.map (fun (n,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) funs
576        in
577         List.concat
578           (List.map
579             (fun (_, ty, bo) ->
580               aux context ty @ aux (tys @ context) bo)
581             funs)
582   and auxs context tl =  (* as aux for list of terms *)
583     List.concat (List.map (fun t -> aux context t) tl)
584   in
585    aux context where
586
587 (** locate_in_conjecture equality what where context
588 * [what] must match a subterm of [where] according to [equality]
589 * It returns the matched terms together with their contexts in [where]
590 * [equality] defaults to physical equality
591 * [context] must be the context of [where]
592 *)
593 let locate_in_conjecture ?(equality=fun _ -> (==)) what (_,context,ty) =
594  let context,res =
595   List.fold_right
596    (fun entry (context,res) ->
597      match entry with
598         None -> entry::context, res
599       | Some (_, Cic.Decl ty) ->
600          let res = res @ locate_in_term what ~where:ty context in
601          let context' = entry::context in
602           context',res
603       | Some (_, Cic.Def (bo,ty)) ->
604          let res = res @ locate_in_term what ~where:bo context in
605          let res =
606           match ty with
607              None -> res
608            | Some ty ->
609               res @ locate_in_term what ~where:ty context in
610          let context' = entry::context in
611           context',res
612    ) context ([],[])
613  in
614   res @ locate_in_term what ~where:ty context
615
616 (* saturate_term newmeta metasenv context ty goal_arity                       *)
617 (* Given a type [ty] (a backbone), it returns its suffix of length            *)
618 (* [goal_arity] head and a new metasenv in which there is new a META for each *)
619 (* hypothesis, a list of arguments for the new applications and the index of  *)
620 (* the last new META introduced. The nth argument in the list of arguments is *)
621 (* just the nth new META.                                                     *)
622 let saturate_term newmeta metasenv context ty goal_arity =
623  let module C = Cic in
624  let module S = CicSubstitution in
625  assert (goal_arity >= 0);
626   let rec aux newmeta ty =
627    match ty with
628       C.Cast (he,_) -> aux newmeta he
629 (* CSC: patch to generate ?1 : ?2 : Type in place of ?1 : Type to simulate ?1 :< Type
630       (* If the expected type is a Type, then also Set is OK ==>
631       *  we accept any term of type Type *)
632       (*CSC: BUG HERE: in this way it is possible for the term of
633       * type Type to be different from a Sort!!! *)
634     | C.Prod (name,(C.Sort (C.Type _) as s),t) ->
635        (* TASSI: ask CSC if BUG HERE refers to the C.Cast or C.Propd case *)
636        let irl =
637          CicMkImplicit.identity_relocation_list_for_metavariable context
638        in
639         let newargument = C.Meta (newmeta+1,irl) in
640          let (res,newmetasenv,arguments,lastmeta) =
641           aux (newmeta + 2) (S.subst newargument t)
642          in
643           res,
644            (newmeta,[],s)::(newmeta+1,context,C.Meta (newmeta,[]))::newmetasenv,
645            newargument::arguments,lastmeta
646 *)
647     | C.Prod (name,s,t) ->
648        let irl =
649          CicMkImplicit.identity_relocation_list_for_metavariable context
650        in
651         let newargument = C.Meta (newmeta,irl) in
652          let res,newmetasenv,arguments,lastmeta,prod_no =
653           aux (newmeta + 1) (S.subst newargument t)
654          in
655           if prod_no + 1 = goal_arity then
656            let head = CicReduction.normalize ~delta:false context ty in
657             head,[],[],lastmeta,goal_arity + 1
658           else
659            (** NORMALIZE RATIONALE 
660             * we normalize the target only NOW since we may be in this case:
661             * A1 -> A2 -> T where T = (\lambda x.A3 -> P) k  
662             * and we want a mesasenv with ?1:A1 and ?2:A2 and not
663             * ?1, ?2, ?3 (that is the one we whould get if we start from the
664             * beta-normalized A1 -> A2 -> A3 -> P **)
665            let s' = CicReduction.normalize ~delta:false context s in
666             res,(newmeta,context,s')::newmetasenv,newargument::arguments,
667              lastmeta,prod_no + 1
668     | t ->
669        let head = CicReduction.normalize ~delta:false context t in
670         match CicReduction.whd context head with
671            C.Prod _ as head' -> aux newmeta head'
672          | _ -> head,[],[],newmeta,0
673   in
674    (* WARNING: here we are using the invariant that above the most *)
675    (* recente new_meta() there are no used metas.                  *)
676    let res,newmetasenv,arguments,lastmeta,_ = aux newmeta ty in
677     res,metasenv @ newmetasenv,arguments,lastmeta
678
679 let lookup_type metasenv context hyp =
680    let rec aux p = function
681       | Some (Cic.Name name, Cic.Decl t) :: _ when name = hyp -> p, t
682       | Some (Cic.Name name, Cic.Def (_, Some t)) :: _ when name = hyp -> p, t
683       | Some (Cic.Name name, Cic.Def (u, _)) :: tail when name = hyp ->
684          p, fst (CicTypeChecker.type_of_aux' metasenv tail u CicUniv.empty_ugraph)
685       | _ :: tail -> aux (succ p) tail
686       | [] -> raise (ProofEngineTypes.Fail (lazy "lookup_type: not premise in the current goal"))
687    in
688    aux 1 context