]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_tactics/nTactics.ml
Added initial support for inversion principles in Matita NG.
[helm.git] / helm / software / components / ng_tactics / nTactics.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: nCic.ml 9058 2008-10-13 17:42:30Z tassi $ *)
13
14 open Printf
15
16 let debug = false
17 let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
18
19 open Continuationals.Stack
20 open NTacStatus
21 module Ast = CicNotationPt
22
23 let id_tac status = status ;;
24 let print_tac print_status message status = 
25   if print_status then pp_status status;
26   prerr_endline message; 
27   status
28 ;;
29
30 let dot_tac status =
31   let gstatus = 
32     match status#stack with
33     | [] -> assert false
34     | ([], _, [], _) :: _ as stack ->
35         (* backward compatibility: do-nothing-dot *)
36         stack
37     | (g, t, k, tag) :: s ->
38         match filter_open g, k with
39         | loc :: loc_tl, _ -> 
40              (([ loc ], t, loc_tl @+ k, tag) :: s) 
41         | [], loc :: k ->
42             assert (is_open loc);
43             (([ loc ], t, k, tag) :: s)
44         | _ -> fail (lazy "can't use \".\" here")
45   in
46    status#set_stack gstatus
47 ;;
48
49 let branch_tac status =
50   let gstatus = 
51     match status#stack with
52     | [] -> assert false
53     | (g, t, k, tag) :: s ->
54           match init_pos g with (* TODO *)
55           | [] | [ _ ] -> fail (lazy "too few goals to branch");
56           | loc :: loc_tl ->
57                ([ loc ], [], [], `BranchTag) :: (loc_tl, t, k, tag) :: s
58   in
59    status#set_stack gstatus
60 ;;
61
62 let shift_tac status =
63   let gstatus = 
64     match status#stack with
65     | (g, t, k, `BranchTag) :: (g', t', k', tag) :: s ->
66           (match g' with
67           | [] -> fail (lazy "no more goals to shift")
68           | loc :: loc_tl ->
69                 (([ loc ], t @+ filter_open g @+ k, [],`BranchTag)
70                 :: (loc_tl, t', k', tag) :: s))
71      | _ -> fail (lazy "can't shift goals here")
72   in
73    status#set_stack gstatus
74 ;;
75
76 let pos_tac i_s status =
77   let gstatus = 
78     match status#stack with
79     | [] -> assert false
80     | ([ loc ], t, [],`BranchTag) :: (g', t', k', tag) :: s
81       when is_fresh loc ->
82         let l_js = List.filter (fun (i, _) -> List.mem i i_s) ([loc] @+ g') in
83           ((l_js, t , [],`BranchTag)
84            :: (([ loc ] @+ g') @- l_js, t', k', tag) :: s)
85     | _ -> fail (lazy "can't use relative positioning here")
86   in
87    status#set_stack gstatus
88 ;;
89
90 let case_tac lab status =
91   let gstatus = 
92     match status#stack with
93     | [] -> assert false
94     | ([ loc ], t, [],`BranchTag) :: (g', t', k', tag) :: s
95       when is_fresh loc ->
96         let l_js = List.filter 
97                      (fun curloc -> 
98                         let _,_,metasenv,_,_ = status#obj in
99                         match NCicUtils.lookup_meta (goal_of_loc curloc) metasenv with
100                             Some s,_,_ when s = lab -> true
101                           | _ -> false) ([loc] @+ g') in
102           ((l_js, t , [],`BranchTag)
103            :: (([ loc ] @+ g') @- l_js, t', k', tag) :: s)
104     | _ -> fail (lazy "can't use relative positioning here")
105   in
106    status#set_stack gstatus
107 ;;
108
109 let wildcard_tac status =
110   let gstatus = 
111     match status#stack with
112     | [] -> assert false
113     | ([ loc ] , t, [], `BranchTag) :: (g', t', k', tag) :: s
114        when is_fresh loc ->
115             (([loc] @+ g', t, [], `BranchTag) :: ([], t', k', tag) :: s)
116     | _ -> fail (lazy "can't use wildcard here")
117   in
118    status#set_stack gstatus
119 ;;
120
121 let merge_tac status =
122   let gstatus = 
123     match status#stack with
124     | [] -> assert false
125     | (g, t, k,`BranchTag) :: (g', t', k', tag) :: s ->
126         ((t @+ filter_open g @+ g' @+ k, t', k', tag) :: s)
127     | _ -> fail (lazy "can't merge goals here")
128   in
129    status#set_stack gstatus
130 ;;
131       
132 let focus_tac gs status =
133   let gstatus = 
134     match status#stack with
135     | [] -> assert false
136     | s -> assert(gs <> []);
137           let stack_locs =
138             let add_l acc _ _ l = if is_open l then l :: acc else acc in
139             fold ~env:add_l ~cont:add_l ~todo:add_l [] s
140           in
141           List.iter
142             (fun g ->
143               if not (List.exists (fun l -> goal_of_loc l = g) stack_locs) then
144                 fail (lazy (sprintf "goal %d not found (or closed)" g)))
145             gs;
146           (zero_pos gs, [], [], `FocusTag) :: deep_close gs s
147   in
148    status#set_stack gstatus
149 ;;
150
151 let unfocus_tac status =
152   let gstatus = 
153     match status#stack with
154     | [] -> assert false
155     | ([], [], [], `FocusTag) :: s -> s
156     | _ -> fail (lazy "can't unfocus, some goals are still open")
157   in
158    status#set_stack gstatus
159 ;;
160
161 let skip_tac status =
162   let gstatus = 
163     match status#stack with
164     | [] -> assert false
165     | (gl, t, k, tag) :: s -> 
166         let gl = List.map switch_of_loc gl in
167         if List.exists (function Open _ -> true | Closed _ -> false) gl then 
168           fail (lazy "cannot skip an open goal")
169         else 
170           ([],t,k,tag) :: s
171   in
172    status#set_stack gstatus
173 ;;
174
175 let block_tac l status =
176   List.fold_left (fun status tac -> tac status) status l
177 ;;
178
179
180 let compare_statuses ~past ~present =
181  let _,_,past,_,_ = past#obj in 
182  let _,_,present,_,_ = present#obj in 
183  List.map fst (List.filter (fun (i,_) -> not(List.mem_assoc i past)) present),
184  List.map fst (List.filter (fun (i,_) -> not (List.mem_assoc i present)) past)
185 ;;
186
187
188
189 (* Exec and distribute_tac form a retraction pair:
190     1) exec (distribute_tac low_tac) (s,i) = low_tac (s,i)
191     2) tac [s]::G = G1::...::Gn::G' && G' is G with some goals closed =>
192          distribute_tac (exec tac) [s]::G = (G1@...Gn)::G'
193     3) tac G = distribute_tac (exec tac) G if  
194        tac = distribute_tac lowtac
195     4) atomic_tac t === distribute_tac (exec t)
196
197    Note that executing an high tactic on a set of goals may be stronger
198    than executing the same tactic on those goals, but once at a time
199    (e.g. the tactic could perform a global analysis of the set of goals)
200 *)
201
202 let exec tac (low_status : #lowtac_status) g =
203   let stack = [ [0,Open g], [], [], `NoTag ] in
204   let status =
205    (new NTacStatus.status low_status#obj stack)#set_estatus low_status
206   in
207   let status = tac status in
208    (low_status#set_estatus status)#set_obj status#obj
209 ;;
210
211 let distribute_tac tac (status : #tac_status) =
212   match status#stack with
213   | [] -> assert false
214   | (g, t, k, tag) :: s ->
215       debug_print (lazy ("context length " ^string_of_int (List.length g)));
216       let rec aux s go gc =
217         function
218         | [] -> 
219             debug_print (lazy "no selected goals");
220             s, go, gc
221         | loc :: loc_tl ->
222             debug_print (lazy "inner eval tactical");
223             let s, go, gc =
224               if List.exists ((=) (goal_of_loc loc)) gc then
225                 s, go, gc
226               else
227                 match switch_of_loc loc with
228                 | Closed _ -> fail (lazy "cannot apply to a Closed goal")
229                 | Open n -> 
230                    let sn = tac s n in
231                    let go', gc' = compare_statuses ~past:s ~present:sn in
232                    sn, ((go @+ [n]) @- gc') @+ go', gc @+ gc'
233             in
234             aux s go gc loc_tl
235       in
236       let s0 =
237        (new NTacStatus.status status#obj ())#set_estatus
238         (status :> NEstatus.status) in
239       let s0, go0, gc0 = s0, [], [] in
240       let sn, gon, gcn = aux s0 go0 gc0 g in
241       debug_print (lazy ("opened: "
242         ^ String.concat " " (List.map string_of_int gon)));
243       debug_print (lazy ("closed: "
244         ^ String.concat " " (List.map string_of_int gcn)));
245       let stack =
246         (zero_pos gon, t @~- gcn, k @~- gcn, tag) :: deep_close gcn s
247       in
248        ((status#set_stack stack)#set_obj(sn:>lowtac_status)#obj)#set_estatus sn
249 ;;
250
251 let atomic_tac htac : #tac_status as 'a -> 'a = distribute_tac (exec htac) ;;
252
253 let repeat_tac t s = 
254   let rec repeat t (status : #tac_status as 'a) : 'a = 
255     try repeat t (t status)
256     with NTacStatus.Error _ -> status
257   in
258     atomic_tac (repeat t) s
259 ;;
260
261
262 let try_tac tac status =
263   try
264     tac status
265   with NTacStatus.Error _ ->
266     status
267 ;;
268
269 let first_tac tacl status =
270   let res = 
271    HExtlib.list_findopt
272     (fun tac _ -> try Some (tac status) with NTacStatus.Error _ -> None) tacl
273   in
274     match res with
275       | None -> fail (lazy "No tactics left")
276       | Some x -> x
277 ;;
278
279 let exact_tac t : 's tactic = distribute_tac (fun status goal ->
280  let goalty = get_goalty status goal in
281  let status, t = disambiguate status t (Some goalty) (ctx_of goalty) in
282  instantiate status goal t)
283 ;;
284
285 let assumption_tac status = distribute_tac (fun status goal ->
286   let gty = get_goalty status goal in
287   let context = ctx_of gty in
288   let htac = 
289    first_tac
290     (List.map (fun (name,_) -> exact_tac ("",0,(Ast.Ident (name,None))))
291       context)
292   in
293     exec htac status goal) status
294 ;;
295
296 let find_in_context name context =
297   let rec aux acc = function
298     | [] -> raise Not_found
299     | (hd,_) :: tl when hd = name -> acc
300     | _ :: tl ->  aux (acc + 1) tl
301   in
302   aux 1 context
303 ;;
304
305 let clear_tac names =
306  if names = [] then id_tac
307  else
308   distribute_tac (fun status goal ->
309    let goalty = get_goalty status goal in
310    let js =
311      List.map 
312      (fun name -> 
313         try find_in_context name (ctx_of goalty)
314         with Not_found -> 
315           fail (lazy ("hypothesis '" ^ name ^ "' not found"))) 
316      names
317    in
318    let n,h,metasenv,subst,o = status#obj in
319    let metasenv,subst,_ = NCicMetaSubst.restrict metasenv subst goal js in
320     status#set_obj (n,h,metasenv,subst,o))
321 ;;
322
323 let generalize0_tac args =
324  if args = [] then id_tac
325  else exact_tac ("",0,Ast.Appl (Ast.Implicit `JustOne :: args))
326 ;;
327
328 let select0_tac ~where:(wanted,hyps,where) ~job  =
329  let found, postprocess = 
330    match job with
331    | `Substexpand argsno -> mk_in_scope, mk_out_scope argsno
332    | `Collect l -> (fun s t -> l := t::!l; mk_in_scope s t), mk_out_scope 1
333    | `ChangeWith f -> f,(fun s t -> s, t)
334  in
335  distribute_tac (fun status goal ->
336    let goalty = get_goalty status goal in
337    let path = 
338      match where with None -> NCic.Implicit `Term | Some where -> where 
339    in
340    let status, newgoalctx =
341       List.fold_right
342        (fun (name,d as entry) (status,ctx) ->
343          try
344           let path = List.assoc name hyps in
345            match d with
346               NCic.Decl ty ->
347                let status,ty =
348                 select_term status ~found ~postprocess (mk_cic_term ctx ty)
349                  (wanted,path) in
350                let status,ty = term_of_cic_term status ty ctx in
351                 status,(name,NCic.Decl ty)::ctx
352             | NCic.Def (bo,ty) ->
353                let status,bo =
354                 select_term status ~found ~postprocess (mk_cic_term ctx bo)
355                  (wanted,path) in
356                let status,bo = term_of_cic_term status bo ctx in
357                 status,(name,NCic.Def (bo,ty))::ctx
358          with
359           Not_found -> status, entry::ctx
360        ) (ctx_of goalty) (status,[])
361    in
362    let status, newgoalty = 
363      select_term status ~found ~postprocess goalty (wanted,path) in
364    (* WARNING: the next two lines simply change the context of newgoalty
365       from the old to the new one. Otherwise mk_meta will do that herself,
366       calling relocate that calls delift. However, newgoalty is now
367       ?[out_scope] and thus the delift would trigger the special unification
368       case, which is wrong now :-( *)
369    let status,newgoalty = term_of_cic_term status newgoalty (ctx_of goalty) in
370    let newgoalty = mk_cic_term newgoalctx newgoalty in
371
372    let status, instance = 
373      mk_meta status newgoalctx (`Decl newgoalty) 
374    in
375    instantiate status goal instance)
376 ;;
377
378 let select_tac ~where ~job move_down_hyps = 
379  let (wanted,hyps,where) = GrafiteDisambiguate.disambiguate_npattern where in
380  let path = 
381   match where with None -> NCic.Implicit `Term | Some where -> where in
382  if not move_down_hyps then
383   select0_tac ~where:(wanted,hyps,Some path) ~job
384  else
385   let path = 
386    List.fold_left
387      (fun path (name,path_name) -> NCic.Prod ("_",path_name,path))
388      path (List.rev hyps)
389   in
390    block_tac [ 
391      generalize0_tac (List.map (fun (name,_) -> Ast.Ident (name,None)) hyps);
392      select0_tac ~where:(wanted,[],Some path) ~job;
393      clear_tac (List.map fst hyps) ]
394 ;;
395
396 let generalize_tac ~where = 
397  let l = ref [] in
398  block_tac [ 
399    select_tac ~where ~job:(`Collect l) true; 
400    print_tac true "ha selezionato?";
401    (fun s -> distribute_tac (fun status goal ->
402       let goalty = get_goalty status goal in
403       let status,canon,rest =
404        match !l with
405           [] ->
406            (match where with
407                _,_,(None,_,_)  -> fail (lazy "No term to generalize")
408              | txt,txtlen,(Some what,_,_) ->
409                 let status, what =
410                  disambiguate status (txt,txtlen,what) None (ctx_of goalty)
411                 in
412                  status,what,[]
413            )
414         | he::tl -> status,he,tl in
415       let status = 
416        List.fold_left 
417          (fun s t -> unify s (ctx_of goalty) canon t) status rest in
418       let status, canon = term_of_cic_term status canon (ctx_of goalty) in
419       instantiate status goal 
420        (mk_cic_term (ctx_of goalty) (NCic.Appl [NCic.Implicit `Term ; canon ]))
421    ) s) ]
422 ;;
423
424 let cut_tac t = 
425  block_tac [ 
426   exact_tac ("",0, Ast.Appl [Ast.Implicit `JustOne; Ast.Implicit `JustOne]);
427   branch_tac;
428    pos_tac [2]; exact_tac t;
429    shift_tac; pos_tac [1]; skip_tac;
430   merge_tac ]
431 ;;
432
433 let lapply_tac (s,n,t) = 
434  exact_tac (s,n, Ast.Appl [Ast.Implicit `JustOne; t])
435 ;;
436
437 let reduce_tac ~reduction ~where =
438   let change status t = 
439     match reduction with
440     | `Normalize perform_delta ->
441         normalize status
442          ?delta:(if perform_delta then None else Some max_int) (ctx_of t) t
443     | `Whd perform_delta -> 
444         whd status
445          ?delta:(if perform_delta then None else Some max_int) (ctx_of t) t
446   in
447   let where = GrafiteDisambiguate.disambiguate_npattern where in
448   select0_tac ~where ~job:(`ChangeWith change)
449 ;;
450
451 let change_tac ~where ~with_what =
452   let change status t = 
453     let status, ww = disambiguate status with_what  None (ctx_of t) in
454     let status = unify status (ctx_of t) t ww in
455     status, ww
456   in
457   let where = GrafiteDisambiguate.disambiguate_npattern where in
458   select0_tac ~where ~job:(`ChangeWith change)
459 ;;
460
461 let letin_tac ~where ~what:(_,_,w) name =
462  block_tac [
463   select_tac ~where ~job:(`Substexpand 1) true;
464   exact_tac
465    ("",0,Ast.LetIn((Ast.Ident (name,None),None),w,Ast.Implicit `JustOne));
466  ]
467 ;;
468
469 let apply_tac (s,n,t) = 
470   let t = Ast.Appl [t; Ast.Implicit `Vector] in
471   exact_tac (s,n,t)
472 ;;
473
474 type indtyinfo = {
475         rightno: int;
476         leftno: int;
477         consno: int;
478         lefts: NCic.term list;
479         rights: NCic.term list;
480         reference: NReference.reference;
481  }
482 ;;
483
484 let analyze_indty_tac ~what indtyref =
485  distribute_tac (fun status goal ->
486   let goalty = get_goalty status goal in
487   let status, what = disambiguate status what None (ctx_of goalty) in
488   let status, ty_what = typeof status (ctx_of what) what in 
489   let status, (r,consno,lefts,rights) = analyse_indty status ty_what in
490   let leftno = List.length lefts in
491   let rightno = List.length rights in
492   indtyref := Some { 
493     rightno = rightno; leftno = leftno; consno = consno;
494     lefts = lefts; rights = rights; reference = r;
495   };
496   exec id_tac status goal)
497 ;;
498
499 let sort_of_goal_tac sortref = distribute_tac (fun status goal ->
500   let goalty = get_goalty status goal in
501   let status,sort = typeof status (ctx_of goalty) goalty in
502   let sort = fix_sorts sort in
503   let status, sort = term_of_cic_term status sort (ctx_of goalty) in
504    sortref := sort;
505    status)
506 ;;
507
508 let elim_tac ~what:(txt,len,what) ~where = 
509   let what = txt, len, Ast.Appl [what; Ast.Implicit `Vector] in
510   let indtyinfo = ref None in
511   let sort = ref (NCic.Rel 1) in
512   atomic_tac (block_tac [
513     analyze_indty_tac ~what indtyinfo;    
514     (fun s -> select_tac 
515       ~where ~job:(`Substexpand ((HExtlib.unopt !indtyinfo).rightno+1)) true s);
516     sort_of_goal_tac sort;
517     (fun status ->
518      let ity = HExtlib.unopt !indtyinfo in
519      let NReference.Ref (uri, _) = ity.reference in
520      let name = 
521        NUri.name_of_uri uri ^ "_" ^
522         snd (NCicElim.ast_of_sort 
523           (match !sort with NCic.Sort x -> x | _ -> assert false))
524      in
525      let eliminator = 
526        let _,_,w = what in
527        Ast.Appl [ Ast.Ident (name,None) ; Ast.Implicit `Vector ; w ]
528      in
529      exact_tac ("",0,eliminator) status) ]) 
530 ;;
531
532 let rewrite_tac ~dir ~what:(_,_,what) ~where status =
533  let sortref = ref (NCic.Rel 1) in
534  let status = sort_of_goal_tac sortref status in
535  let suffix = "_" ^ snd (NCicElim.ast_of_sort 
536    (match !sortref with NCic.Sort x -> x | _ -> assert false))
537  in
538  let name =
539   match dir with
540      `LeftToRight -> "eq" ^ suffix ^ "_r"
541    | `RightToLeft -> "eq" ^ suffix
542  in
543   block_tac
544    [ select_tac ~where ~job:(`Substexpand 1) true;
545      exact_tac
546       ("",0,
547        Ast.Appl(Ast.Ident(name,None)::HExtlib.mk_list (Ast.Implicit `JustOne) 5@
548         [what]))] status
549 ;;
550
551 let intro_tac name =
552  block_tac
553   [ exact_tac
554      ("",0,(Ast.Binder (`Lambda,
555       (Ast.Ident (name,None),None),Ast.Implicit `JustOne)));
556     if name = "_" then clear_tac [name] else id_tac ]
557 ;;
558
559 let cases ~what status goal =
560  let gty = get_goalty status goal in
561  let status, what = disambiguate status what None (ctx_of gty) in
562  let status, ty = typeof status (ctx_of what) what in
563  let status, (ref, consno, _, _) = analyse_indty status ty in
564  let status, what = term_of_cic_term status what (ctx_of gty) in
565  let t =
566   NCic.Match (ref,NCic.Implicit `Term, what,
567     HExtlib.mk_list (NCic.Implicit `Term) consno)
568  in
569  let ctx = ctx_of gty in
570  let status,t,ty = refine status ctx (mk_cic_term ctx t) (Some gty) in
571  instantiate status goal t
572 ;;
573
574 let cases_tac ~what:(txt,len,what) ~where = 
575   let what = txt, len, Ast.Appl [what; Ast.Implicit `Vector] in
576   let indtyinfo = ref None in
577   atomic_tac 
578    (block_tac [
579       analyze_indty_tac ~what indtyinfo;
580       (fun s -> select_tac 
581        ~where ~job:(`Substexpand ((HExtlib.unopt !indtyinfo).rightno+1))true s);
582       distribute_tac (cases ~what) ])
583 ;;
584
585 let case1_tac name =
586  let name = if name = "_" then "_clearme" else name in
587  block_tac [ intro_tac name; 
588              cases_tac 
589               ~where:("",0,(None,[],None)) 
590               ~what:("",0,Ast.Ident (name,None));
591              if name = "_clearme" then clear_tac ["_clearme"] else id_tac ]
592 ;;
593
594 let constructor ?(num=1) ~args status goal = 
595   if num < 1 then fail (lazy "constructor numbers begin with 1");
596   let gty = get_goalty status goal in
597   let status, (r,_,_,_) = analyse_indty status gty in
598   let ref = NReference.mk_constructor num r in
599   let t = 
600     if args = [] then Ast.NRef ref else
601     Ast.Appl (HExtlib.list_concat ~sep:[Ast.Implicit `Vector]
602       ([Ast.NRef ref] :: List.map (fun _,_,x -> [x]) args))
603   in
604   exec (apply_tac ("",0,t)) status goal
605 ;;
606
607 let constructor_tac ?num ~args = distribute_tac (constructor ?num ~args);;
608
609 let assert0_tac (hyps,concl) = distribute_tac (fun status goal ->
610  let gty = get_goalty status goal in
611  let eq status ctx t1 t2 =
612   let status,t1 = disambiguate status t1 None ctx in
613   let status,t1 = apply_subst status ctx t1 in
614   let status,t1 = term_of_cic_term status t1 ctx in
615   let t2 = mk_cic_term ctx t2 in
616   let status,t2 = apply_subst status ctx t2 in
617   let status,t2 = term_of_cic_term status t2 ctx in
618   prerr_endline ("COMPARING: " ^ NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:ctx t1 ^ " vs " ^ NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:ctx t2);
619   assert (t1=t2);
620   status
621  in
622  let status,gty' = term_of_cic_term status gty (ctx_of gty) in
623  let status = eq status (ctx_of gty) concl gty' in
624  let status,_ =
625   List.fold_right2
626    (fun (id1,e1) ((id2,e2) as item) (status,ctx) ->
627      assert (id1=id2 || (prerr_endline (id1 ^ " vs " ^ id2); false));
628      match e1,e2 with
629         `Decl t1, NCic.Decl t2 ->
630           let status = eq status ctx t1 t2 in
631           status,item::ctx
632       | `Def (b1,t1), NCic.Def (b2,t2) ->
633           let status = eq status ctx t1 t2 in
634           let status = eq status ctx b1 b2 in
635           status,item::ctx
636       | _ -> assert false
637    ) hyps (ctx_of gty) (status,[])
638  in
639   exec id_tac status goal)
640 ;;
641
642 let assert_tac seqs status =
643  match status#stack with
644   | [] -> assert false
645   | (g,_,_,_) :: s ->
646      assert (List.length g = List.length seqs);
647      (match seqs with
648          [] -> id_tac
649        | [seq] -> assert0_tac seq
650        | _ ->
651          block_tac
652           (branch_tac::
653           HExtlib.list_concat ~sep:[shift_tac]
654             (List.map (fun seq -> [assert0_tac seq]) seqs)@
655           [merge_tac])
656      ) status
657 ;;
658
659 let auto ~params:(l,_) status goal =
660   let gty = get_goalty status goal in
661   let n,h,metasenv,subst,o = status#obj in
662   let status,t = term_of_cic_term status gty (ctx_of gty) in
663   let status, l = 
664     List.fold_left
665       (fun (status, l) t ->
666         let status, t = disambiguate status t None (ctx_of gty) in
667         let status, ty = typeof status (ctx_of t) t in
668         let status, t =  term_of_cic_term status t (ctx_of gty) in
669         let status, ty = term_of_cic_term status ty (ctx_of ty) in
670         (status, (t,ty) :: l))
671       (status,[]) l
672   in
673   match
674     NCicParamod.nparamod status metasenv subst (ctx_of gty) (NCic.Rel ~-1,t) l 
675   with
676   | [] -> raise (NTacStatus.Error (lazy "no proof found",None))
677   | (pt, metasenv, subst)::_ -> 
678       let status = status#set_obj (n,h,metasenv,subst,o) in
679       instantiate status goal (NTacStatus.mk_cic_term (ctx_of gty) pt)
680 ;;
681
682 let auto_tac ~params status =
683   distribute_tac (auto ~params) status
684 ;;
685