src/Provers/blast.ML
author paulson
Thu, 03 Apr 1997 10:32:34 +0200
changeset 2883 fd1c0b8e9b61
parent 2854 f03b1652fc6a
child 2894 d2ffee4f811b
permissions -rw-r--r--
Now exports declConsts!
Temporarily erased target signature to aid debugging
paulson@2854
     1
(*  ID:         $Id$
paulson@2854
     2
use "/homes/lcp/Isa/new/blast.ML";
paulson@2854
     3
  SKOLEMIZES ReplaceI WRONGLY: allow new vars in prems, or forbid such rules??
paulson@2854
     4
  Needs explicit instantiation of assumptions?  (#55 takes 32s)
paulson@2854
     5
paulson@2854
     6
*)
paulson@2854
     7
paulson@2854
     8
paulson@2854
     9
proof_timing:=true;
paulson@2854
    10
print_depth 20;
paulson@2854
    11
paulson@2854
    12
structure List = List_;
paulson@2854
    13
paulson@2854
    14
(*Should be a type abbreviation?*)
paulson@2854
    15
type netpair = (int*(bool*thm)) Net.net * (int*(bool*thm)) Net.net;
paulson@2854
    16
paulson@2854
    17
paulson@2854
    18
(*Assumptions about constants:
paulson@2854
    19
  --The negation symbol is "Not"
paulson@2854
    20
  --The equality symbol is "op ="
paulson@2854
    21
  --The is-true judgement symbol is "Trueprop"
paulson@2854
    22
  --There are no constants named "*Goal* or "*False*"
paulson@2854
    23
*)
paulson@2854
    24
signature BLAST_DATA =
paulson@2854
    25
  sig
paulson@2854
    26
  type claset
paulson@2854
    27
  val notE		: thm		(* [| ~P;  P |] ==> R *)
paulson@2854
    28
  val ccontr		: thm		
paulson@2854
    29
  val contr_tac 	: int -> tactic
paulson@2854
    30
  val dup_intr		: thm -> thm
paulson@2854
    31
  val vars_gen_hyp_subst_tac : bool -> int -> tactic
paulson@2854
    32
  val claset		: claset ref
paulson@2854
    33
  val rep_claset	: 
paulson@2854
    34
      claset -> {safeIs: thm list, safeEs: thm list, 
paulson@2854
    35
		 hazIs: thm list, hazEs: thm list,
paulson@2854
    36
		 uwrapper: (int -> tactic) -> (int -> tactic),
paulson@2854
    37
		 swrapper: (int -> tactic) -> (int -> tactic),
paulson@2854
    38
		 safe0_netpair: netpair, safep_netpair: netpair,
paulson@2854
    39
		 haz_netpair: netpair, dup_netpair: netpair}
paulson@2854
    40
  end;
paulson@2854
    41
paulson@2854
    42
paulson@2854
    43
signature BLAST =
paulson@2854
    44
  sig
paulson@2854
    45
  type claset
paulson@2883
    46
  val depth_tac 	: claset -> int -> int -> tactic
paulson@2883
    47
  val blast_tac 	: claset -> int -> tactic
paulson@2883
    48
  val Blast_tac 	: int -> tactic
paulson@2883
    49
  val declConsts 	: string list * thm list -> unit
paulson@2854
    50
  end;
paulson@2854
    51
paulson@2854
    52
paulson@2883
    53
functor BlastFun(Data: BLAST_DATA) = 
paulson@2854
    54
struct
paulson@2854
    55
paulson@2854
    56
type claset = Data.claset;
paulson@2854
    57
paulson@2854
    58
val trace = ref false;
paulson@2854
    59
paulson@2854
    60
datatype term = 
paulson@2854
    61
    Const of string
paulson@2854
    62
  | OConst of string * int
paulson@2854
    63
  | Skolem of string * term option ref list
paulson@2854
    64
  | Free  of string
paulson@2854
    65
  | Var   of term option ref
paulson@2854
    66
  | Bound of int
paulson@2854
    67
  | Abs   of string*term
paulson@2854
    68
  | op $  of term*term;
paulson@2854
    69
paulson@2854
    70
paulson@2854
    71
exception DEST_EQ;
paulson@2854
    72
paulson@2854
    73
  (*Take apart an equality (plain or overloaded).  NO constant Trueprop*)
paulson@2854
    74
  fun dest_eq (Const  "op ="     $ t $ u) = (t,u)
paulson@2854
    75
    | dest_eq (OConst("op =",_)  $ t $ u) = (t,u)
paulson@2854
    76
    | dest_eq _                      = raise DEST_EQ;
paulson@2854
    77
paulson@2854
    78
(** Basic syntactic operations **)
paulson@2854
    79
paulson@2854
    80
fun is_Var (Var _) = true
paulson@2854
    81
  | is_Var _ = false;
paulson@2854
    82
paulson@2854
    83
fun dest_Var (Var x) =  x;
paulson@2854
    84
paulson@2854
    85
paulson@2854
    86
fun rand (f$x) = x;
paulson@2854
    87
paulson@2854
    88
(* maps   (f, [t1,...,tn])  to  f(t1,...,tn) *)
paulson@2854
    89
val list_comb : term * term list -> term = foldl (op $);
paulson@2854
    90
paulson@2854
    91
paulson@2854
    92
(* maps   f(t1,...,tn)  to  (f, [t1,...,tn]) ; naturally tail-recursive*)
paulson@2854
    93
fun strip_comb u : term * term list = 
paulson@2854
    94
    let fun stripc (f$t, ts) = stripc (f, t::ts)
paulson@2854
    95
        |   stripc  x =  x 
paulson@2854
    96
    in  stripc(u,[])  end;
paulson@2854
    97
paulson@2854
    98
paulson@2854
    99
(* maps   f(t1,...,tn)  to  f , which is never a combination *)
paulson@2854
   100
fun head_of (f$t) = head_of f
paulson@2854
   101
  | head_of u = u;
paulson@2854
   102
paulson@2854
   103
paulson@2854
   104
(** Particular constants **)
paulson@2854
   105
paulson@2854
   106
fun negate P = Const"Not" $ P;
paulson@2854
   107
paulson@2854
   108
fun mkGoal P = Const"*Goal*" $ P;
paulson@2854
   109
paulson@2854
   110
fun isGoal (Const"*Goal*" $ _) = true
paulson@2854
   111
  | isGoal _                   = false;
paulson@2854
   112
paulson@2854
   113
val Trueprop = Term.Const("Trueprop", Type("o",[])-->propT);
paulson@2854
   114
fun mk_tprop P = Term.$ (Trueprop, P);
paulson@2854
   115
paulson@2854
   116
fun isTrueprop (Term.Const("Trueprop",_)) = true
paulson@2854
   117
  | isTrueprop _                          = false;
paulson@2854
   118
paulson@2854
   119
paulson@2854
   120
(** Dealing with overloaded constants **)
paulson@2854
   121
paulson@2854
   122
(*Result is a symbol table, indexed by names of overloaded constants.
paulson@2854
   123
  Each constant maps to a list of (pattern,Blast.Const) pairs.
paulson@2854
   124
  Any Term.Const that matches a pattern gets replaced by the Blast.Const.
paulson@2854
   125
*)
paulson@2854
   126
fun addConsts (t as Term.Const(a,_), tab) =
paulson@2854
   127
     (case Symtab.lookup (tab,a) of
paulson@2854
   128
	  None    => tab  (*ignore: not a constant that we are looking for*)
paulson@2854
   129
	| Some patList => 
paulson@2854
   130
	      (case gen_assoc (op aconv) (patList, t) of
paulson@2854
   131
		  None => Symtab.update
paulson@2854
   132
		           ((a, (t, OConst (a, length patList)) :: patList), 
paulson@2854
   133
			    tab)
paulson@2854
   134
		 | _    => tab))
paulson@2854
   135
  | addConsts (Term.Abs(_,_,body), tab) = addConsts (body, tab)
paulson@2854
   136
  | addConsts (Term.$ (t,u), tab) = addConsts (t, addConsts (u, tab))
paulson@2854
   137
  | addConsts (_,            tab) = tab (*ignore others*);
paulson@2854
   138
paulson@2854
   139
paulson@2854
   140
fun addRules (rls,tab) = foldr addConsts (map (#prop o rep_thm) rls, tab);
paulson@2854
   141
paulson@2883
   142
fun declConst (a,tab) = 
paulson@2883
   143
    case Symtab.lookup (tab,a) of
paulson@2883
   144
	None   => Symtab.update((a,[]), tab)	(*create a brand new entry*)
paulson@2883
   145
      | Some _ => tab				(*preserve old entry*);
paulson@2854
   146
paulson@2854
   147
(*maps the name of each overloaded constant to a list of archetypal constants,
paulson@2854
   148
  which may be polymorphic.*)
paulson@2854
   149
local
paulson@2854
   150
val overLoadTab = ref (Symtab.null : (Term.term * term) list Symtab.table)
paulson@2854
   151
    (*The alists in this table should only be increased*)
paulson@2854
   152
in
paulson@2854
   153
paulson@2854
   154
fun declConsts (names, rls) =
paulson@2854
   155
    overLoadTab := addRules (rls, foldr declConst (names, !overLoadTab));
paulson@2854
   156
paulson@2854
   157
paulson@2854
   158
(*Convert a possibly overloaded Term.Const to a Blast.Const*)
paulson@2854
   159
fun fromConst tsig (t as Term.Const (a,_)) =
paulson@2854
   160
  let fun find []                  = Const a
paulson@2854
   161
	| find ((pat,t')::patList) =
paulson@2854
   162
		if Pattern.matches tsig (pat,t) then t' 
paulson@2854
   163
		else find patList
paulson@2854
   164
  in  case Symtab.lookup(!overLoadTab, a) of
paulson@2854
   165
	   None         => Const a
paulson@2854
   166
	 | Some patList => find patList
paulson@2854
   167
  end;
paulson@2854
   168
end;
paulson@2854
   169
paulson@2854
   170
paulson@2854
   171
(*Tests whether 2 terms are alpha-convertible; chases instantiations*)
paulson@2854
   172
fun (Const a)      aconv (Const b)      = a=b
paulson@2854
   173
  | (OConst ai)    aconv (OConst bj)    = ai=bj
paulson@2854
   174
  | (Skolem (a,_)) aconv (Skolem (b,_)) = a=b  (*arglists must then be equal*)
paulson@2854
   175
  | (Free a)       aconv (Free b)       = a=b
paulson@2854
   176
  | (Var(ref(Some t))) aconv u          = t aconv u
paulson@2854
   177
  | t aconv (Var(ref(Some u)))          = t aconv u
paulson@2854
   178
  | (Var v)        aconv (Var w)        = v=w	(*both Vars are un-assigned*)
paulson@2854
   179
  | (Bound i)      aconv (Bound j)      = i=j
paulson@2854
   180
  | (Abs(_,t))     aconv (Abs(_,u))     = t aconv u
paulson@2854
   181
  | (f$t)          aconv (g$u)          = (f aconv g) andalso (t aconv u)
paulson@2854
   182
  | _ aconv _  =  false;
paulson@2854
   183
paulson@2854
   184
paulson@2854
   185
fun mem_term (_, [])     = false
paulson@2854
   186
  | mem_term (t, t'::ts) = t aconv t' orelse mem_term(t,ts);
paulson@2854
   187
paulson@2854
   188
fun ins_term(t,ts) = if mem_term(t,ts) then ts else t :: ts;
paulson@2854
   189
paulson@2854
   190
fun mem_var (v: term option ref, []) = false
paulson@2854
   191
  | mem_var (v, v'::vs)              = v=v' orelse mem_var(v,vs);
paulson@2854
   192
paulson@2854
   193
fun ins_var(v,vs) = if mem_var(v,vs) then vs else v :: vs;
paulson@2854
   194
paulson@2854
   195
paulson@2854
   196
(** Vars **)
paulson@2854
   197
paulson@2854
   198
(*Accumulates the Vars in the term, suppressing duplicates*)
paulson@2854
   199
fun add_term_vars (Skolem(a,args),	vars) = add_vars_vars(args,vars)
paulson@2854
   200
  | add_term_vars (Var (v as ref None),	vars) = ins_var (v, vars)
paulson@2854
   201
  | add_term_vars (Var (ref (Some u)), vars)  = add_term_vars(u,vars)
paulson@2854
   202
  | add_term_vars (Abs (_,body),	vars) = add_term_vars(body,vars)
paulson@2854
   203
  | add_term_vars (f$t,	vars) =  add_term_vars (f, add_term_vars(t, vars))
paulson@2854
   204
  | add_term_vars (_,	vars) = vars
paulson@2854
   205
(*Term list version.  [The fold functionals are slow]*)
paulson@2854
   206
and add_terms_vars ([],    vars) = vars
paulson@2854
   207
  | add_terms_vars (t::ts, vars) = add_terms_vars (ts, add_term_vars(t,vars))
paulson@2854
   208
(*Var list version.*)
paulson@2854
   209
and add_vars_vars ([],    vars) = vars
paulson@2854
   210
  | add_vars_vars (ref (Some u) :: vs, vars) = 
paulson@2854
   211
	add_vars_vars (vs, add_term_vars(u,vars))
paulson@2854
   212
  | add_vars_vars (v::vs, vars) =   (*v must be a ref None*)
paulson@2854
   213
	add_vars_vars (vs, ins_var (v, vars));
paulson@2854
   214
paulson@2854
   215
paulson@2854
   216
(*Chase assignments in "vars"; return a list of unassigned variables*)
paulson@2854
   217
fun vars_in_vars vars = add_vars_vars(vars,[]);
paulson@2854
   218
paulson@2854
   219
paulson@2854
   220
paulson@2854
   221
(*increment a term's non-local bound variables
paulson@2854
   222
     inc is  increment for bound variables
paulson@2854
   223
     lev is  level at which a bound variable is considered 'loose'*)
paulson@2854
   224
fun incr_bv (inc, lev, u as Bound i) = if i>=lev then Bound(i+inc) else u 
paulson@2854
   225
  | incr_bv (inc, lev, Abs(a,body)) = Abs(a, incr_bv(inc,lev+1,body))
paulson@2854
   226
  | incr_bv (inc, lev, f$t) = incr_bv(inc,lev,f) $ incr_bv(inc,lev,t)
paulson@2854
   227
  | incr_bv (inc, lev, u) = u;
paulson@2854
   228
paulson@2854
   229
fun incr_boundvars  0  t = t
paulson@2854
   230
  | incr_boundvars inc t = incr_bv(inc,0,t);
paulson@2854
   231
paulson@2854
   232
paulson@2854
   233
(*Accumulate all 'loose' bound vars referring to level 'lev' or beyond.
paulson@2854
   234
   (Bound 0) is loose at level 0 *)
paulson@2854
   235
fun add_loose_bnos (Bound i, lev, js)   = if i<lev then js  
paulson@2854
   236
					  else  (i-lev) ins_int js
paulson@2854
   237
  | add_loose_bnos (Abs (_,t), lev, js) = add_loose_bnos (t, lev+1, js)
paulson@2854
   238
  | add_loose_bnos (f$t, lev, js)       =
paulson@2854
   239
	        add_loose_bnos (f, lev, add_loose_bnos (t, lev, js)) 
paulson@2854
   240
  | add_loose_bnos (_, _, js)           = js;
paulson@2854
   241
paulson@2854
   242
fun loose_bnos t = add_loose_bnos (t, 0, []);
paulson@2854
   243
paulson@2854
   244
fun subst_bound (arg, t) : term = 
paulson@2854
   245
  let fun subst (t as Bound i, lev) =
paulson@2854
   246
 	    if i<lev then  t    (*var is locally bound*)
paulson@2854
   247
	    else  if i=lev then incr_boundvars lev arg
paulson@2854
   248
		           else Bound(i-1)  (*loose: change it*)
paulson@2854
   249
	| subst (Abs(a,body), lev) = Abs(a, subst(body,lev+1))
paulson@2854
   250
	| subst (f$t, lev) =  subst(f,lev)  $  subst(t,lev)
paulson@2854
   251
	| subst (t,lev)    = t
paulson@2854
   252
  in  subst (t,0)  end;
paulson@2854
   253
paulson@2854
   254
paulson@2854
   255
(*Normalize...but not the bodies of ABSTRACTIONS*)
paulson@2854
   256
fun norm t = case t of
paulson@2854
   257
    Skolem(a,args)       => Skolem(a, vars_in_vars args)
paulson@2854
   258
  | (Var (ref None))     => t
paulson@2854
   259
  | (Var (ref (Some u))) => norm u
paulson@2854
   260
  | (f $ u) => (case norm f of
paulson@2854
   261
		    Abs(_,body) => norm (subst_bound (u, body))
paulson@2854
   262
		  | nf => nf $ norm u)
paulson@2854
   263
  | _ => t;
paulson@2854
   264
paulson@2854
   265
paulson@2854
   266
(*Weak (one-level) normalize for use in unification*)
paulson@2854
   267
fun wkNormAux t = case t of
paulson@2854
   268
    (Var v) => (case !v of
paulson@2854
   269
		    Some u => wkNorm u
paulson@2854
   270
		  | None   => t)
paulson@2854
   271
  | (f $ u) => (case wkNormAux f of
paulson@2854
   272
		    Abs(_,body) => wkNorm (subst_bound (u, body))
paulson@2854
   273
		  | nf          => nf $ u)
paulson@2854
   274
  | _ => t
paulson@2854
   275
and wkNorm t = case head_of t of
paulson@2854
   276
    Const _        => t
paulson@2854
   277
  | OConst _       => t
paulson@2854
   278
  | Skolem(a,args) => t
paulson@2854
   279
  | Free _         => t
paulson@2854
   280
  | _              => wkNormAux t;
paulson@2854
   281
paulson@2854
   282
paulson@2854
   283
(*Does variable v occur in u?  For unification.*)
paulson@2854
   284
fun varOccur v = 
paulson@2854
   285
  let fun occL [] = false	(*same as (exists occ), but faster*)
paulson@2854
   286
	| occL (u::us) = occ u orelse occL us
paulson@2854
   287
      and occ (Var w) = 
paulson@2854
   288
	      v=w orelse
paulson@2854
   289
              (case !w of None   => false
paulson@2854
   290
	                | Some u => occ u)
paulson@2854
   291
        | occ (Skolem(_,args)) = occL (map Var args)
paulson@2854
   292
        | occ (Abs(_,u)) = occ u
paulson@2854
   293
        | occ (f$u)      = occ u  orelse  occ f
paulson@2854
   294
        | occ (_)        = false;
paulson@2854
   295
  in  occ  end;
paulson@2854
   296
paulson@2854
   297
exception UNIFY;
paulson@2854
   298
paulson@2854
   299
val trail = ref [] : term option ref list ref;
paulson@2854
   300
val ntrail = ref 0;
paulson@2854
   301
paulson@2854
   302
paulson@2854
   303
(*Restore the trail to some previous state: for backtracking*)
paulson@2854
   304
fun clearTo n =
paulson@2854
   305
    while !ntrail>n do
paulson@2854
   306
	(hd(!trail) := None;
paulson@2854
   307
	 trail := tl (!trail);
paulson@2854
   308
	 ntrail := !ntrail - 1);
paulson@2854
   309
paulson@2854
   310
paulson@2854
   311
(*First-order unification with bound variables.  
paulson@2854
   312
  "vars" is a list of variables local to the rule and NOT to be put
paulson@2854
   313
	on the trail (no point in doing so)
paulson@2854
   314
*)
paulson@2854
   315
fun unify(vars,t,u) =
paulson@2854
   316
    let val n = !ntrail 
paulson@2854
   317
	fun update (t as Var v, u) =
paulson@2854
   318
	    if t aconv u then ()
paulson@2854
   319
	    else if varOccur v u then raise UNIFY 
paulson@2854
   320
	    else if mem_var(v, vars) then v := Some u
paulson@2854
   321
		 else (*avoid updating Vars in the branch if possible!*)
paulson@2854
   322
		      if is_Var u andalso mem_var(dest_Var u, vars)
paulson@2854
   323
		      then dest_Var u := Some t
paulson@2854
   324
		      else (v := Some u;
paulson@2854
   325
			    trail := v :: !trail;  ntrail := !ntrail + 1)
paulson@2854
   326
	fun unifyAux (t,u) = 
paulson@2854
   327
	    case (wkNorm t,  wkNorm u) of
paulson@2854
   328
		(nt as Var v,  nu) => update(nt,nu)
paulson@2854
   329
	      | (nu,  nt as Var v) => update(nt,nu)
paulson@2854
   330
	      | (Abs(_,t'),  Abs(_,u')) => unifyAux(t',u')
paulson@2854
   331
		    (*NB: can yield unifiers having dangling Bound vars!*)
paulson@2854
   332
	      | (f$t',  g$u') => (unifyAux(f,g); unifyAux(t',u'))
paulson@2854
   333
	      | (nt,  nu)    => if nt aconv nu then () else raise UNIFY
paulson@2854
   334
    in  unifyAux(t,u) handle UNIFY => (clearTo n; raise UNIFY)
paulson@2854
   335
    end;
paulson@2854
   336
paulson@2854
   337
paulson@2854
   338
(*Convert from "real" terms to prototerms; eta-contract*)
paulson@2854
   339
fun fromTerm tsig t =
paulson@2854
   340
  let val alist = ref []
paulson@2854
   341
      fun from (t as Term.Const _) = fromConst tsig t
paulson@2854
   342
	| from (Term.Free  (a,_)) = Free a
paulson@2854
   343
	| from (Term.Bound i)     = Bound i
paulson@2854
   344
	| from (Term.Var (ixn,T)) = 
paulson@2854
   345
	      (case (assoc_string_int(!alist,ixn)) of
paulson@2854
   346
		   None => let val t' = Var(ref None)
paulson@2854
   347
		           in  alist := (ixn, (t', T)) :: !alist;  t'
paulson@2854
   348
			   end
paulson@2854
   349
		 | Some (v,_) => v)
paulson@2854
   350
	| from (Term.Abs (a,_,u)) = 
paulson@2854
   351
	      (case  from u  of
paulson@2854
   352
		u' as (f $ Bound 0) => 
paulson@2854
   353
		  if (0 mem_int loose_bnos f) then Abs(a,u')
paulson@2854
   354
		  else incr_boundvars ~1 f 
paulson@2854
   355
	      | u' => Abs(a,u'))
paulson@2854
   356
	| from (Term.$ (f,u)) = from f $ from u
paulson@2854
   357
  in  from t  end;
paulson@2854
   358
paulson@2854
   359
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
paulson@2854
   360
fun strip_imp_prems (Const"==>" $ (Const"Trueprop" $ A) $ B) = 
paulson@2854
   361
           A :: strip_imp_prems B
paulson@2854
   362
  | strip_imp_prems (Const"==>" $ A $ B) = A :: strip_imp_prems B
paulson@2854
   363
  | strip_imp_prems _ = [];
paulson@2854
   364
paulson@2854
   365
(* A1==>...An==>B  goes to B, where B is not an implication *)
paulson@2854
   366
fun strip_imp_concl (Const"==>" $ A $ B) = strip_imp_concl B
paulson@2854
   367
  | strip_imp_concl (Const"Trueprop" $ A) = A
paulson@2854
   368
  | strip_imp_concl A = A : term;
paulson@2854
   369
paulson@2854
   370
paulson@2854
   371
(*** Conversion of Elimination Rules to Tableau Operations ***)
paulson@2854
   372
paulson@2854
   373
(*The conclusion becomes the goal/negated assumption *False*: delete it!*)
paulson@2854
   374
fun squash_nots [] = []
paulson@2854
   375
  | squash_nots (Const "*Goal*" $ (Var (ref (Some (Const"*False*")))) :: Ps) =
paulson@2854
   376
	squash_nots Ps
paulson@2854
   377
  | squash_nots (Const "Not" $ (Var (ref (Some (Const"*False*")))) :: Ps) =
paulson@2854
   378
	squash_nots Ps
paulson@2854
   379
  | squash_nots (P::Ps) = P :: squash_nots Ps;
paulson@2854
   380
paulson@2854
   381
fun skoPrem vars (Const "all" $ Abs (_, P)) =
paulson@2854
   382
        skoPrem vars (subst_bound (Skolem (gensym "S_", vars), P))
paulson@2854
   383
  | skoPrem vars P = P;
paulson@2854
   384
paulson@2854
   385
fun convertPrem t = 
paulson@2854
   386
    squash_nots (mkGoal (strip_imp_concl t) :: strip_imp_prems t);
paulson@2854
   387
paulson@2854
   388
(*Expects elimination rules to have a formula variable as conclusion*)
paulson@2854
   389
fun convertRule vars t =
paulson@2854
   390
  let val (P::Ps) = strip_imp_prems t
paulson@2854
   391
      val Var v   = strip_imp_concl t
paulson@2854
   392
  in  v := Some (Const"*False*");
paulson@2854
   393
      (P, map (convertPrem o skoPrem vars) Ps) 
paulson@2854
   394
  end;
paulson@2854
   395
paulson@2854
   396
paulson@2854
   397
(*Like dup_elim, but puts the duplicated major premise FIRST*)
paulson@2854
   398
fun rev_dup_elim th = th RSN (2, revcut_rl) |> assumption 2 |> Sequence.hd;
paulson@2854
   399
paulson@2854
   400
paulson@2854
   401
(*Count new hyps so that they can be rotated*)
paulson@2854
   402
fun nNewHyps []                         = 0
paulson@2854
   403
  | nNewHyps (Const "*Goal*" $ _ :: Ps) = nNewHyps Ps
paulson@2854
   404
  | nNewHyps (P::Ps)                    = 1 + nNewHyps Ps;
paulson@2854
   405
paulson@2854
   406
fun rot_subgoals_tac [] i st      = Sequence.single st
paulson@2854
   407
  | rot_subgoals_tac (k::ks) i st =
paulson@2854
   408
      rot_subgoals_tac ks (i+1) (Sequence.hd (rotate_tac (~k) i st))
paulson@2854
   409
      handle OPTION _ => Sequence.null;
paulson@2854
   410
paulson@2854
   411
fun TRACE rl tac st = if !trace then (prth rl; tac st) else tac st;
paulson@2854
   412
paulson@2854
   413
(*Tableau rule from elimination rule.  Flag "dup" requests duplication of the
paulson@2854
   414
  affected formula.*)
paulson@2854
   415
fun fromRule vars rl = 
paulson@2854
   416
  let val {tsig,...} = Sign.rep_sg (#sign (rep_thm rl))
paulson@2854
   417
      val trl = rl |> rep_thm |> #prop |> fromTerm tsig |> convertRule vars
paulson@2854
   418
      fun tac dup i = 
paulson@2854
   419
	  TRACE rl
paulson@2854
   420
	  (DETERM (etac (if dup then rev_dup_elim rl else rl) i))
paulson@2854
   421
	  THEN rot_subgoals_tac (map nNewHyps (#2 trl)) i
paulson@2854
   422
	  
paulson@2854
   423
  in General.SOME (trl, tac) end
paulson@2854
   424
  handle Bind => General.NONE  (*reject: conclusion is not just a variable*);
paulson@2854
   425
paulson@2854
   426
paulson@2854
   427
(*** Conversion of Introduction Rules (needed for efficiency in 
paulson@2854
   428
               proof reconstruction) ***)
paulson@2854
   429
paulson@2854
   430
fun convertIntrPrem t = mkGoal (strip_imp_concl t) :: strip_imp_prems t;
paulson@2854
   431
paulson@2854
   432
fun convertIntrRule vars t =
paulson@2854
   433
  let val Ps = strip_imp_prems t
paulson@2854
   434
      val P  = strip_imp_concl t
paulson@2854
   435
  in  (mkGoal P, map (convertIntrPrem o skoPrem vars) Ps) 
paulson@2854
   436
  end;
paulson@2854
   437
paulson@2854
   438
(*Tableau rule from introduction rule.  Since haz rules are now delayed, 
paulson@2854
   439
  "dup" is always FALSE for introduction rules.*)
paulson@2854
   440
fun fromIntrRule vars rl = 
paulson@2854
   441
  let val {tsig,...} = Sign.rep_sg (#sign (rep_thm rl))
paulson@2854
   442
      val trl = rl |> rep_thm |> #prop |> fromTerm tsig |> convertIntrRule vars
paulson@2854
   443
      fun tac dup i = 
paulson@2854
   444
	  TRACE rl (DETERM (rtac (if dup then Data.dup_intr rl else rl) i))
paulson@2854
   445
	  THEN rot_subgoals_tac (map nNewHyps (#2 trl)) i
paulson@2854
   446
  in (trl, tac) end;
paulson@2854
   447
paulson@2854
   448
paulson@2854
   449
val dummyVar = Term.Var (("Doom",666), dummyT);
paulson@2854
   450
paulson@2854
   451
(*Convert from prototerms to ordinary terms with dummy types
paulson@2854
   452
  Ignore abstractions; identify all Vars*)
paulson@2854
   453
fun dummyTerm 0 _             = dummyVar
paulson@2854
   454
  | dummyTerm d (Const a)     = Term.Const (a,dummyT)
paulson@2854
   455
  | dummyTerm d (OConst(a,_)) = Term.Const (a,dummyT)
paulson@2854
   456
  | dummyTerm d (Skolem(a,_)) = Term.Const (a,dummyT)
paulson@2854
   457
  | dummyTerm d (Free a)      = Term.Free  (a,dummyT)
paulson@2854
   458
  | dummyTerm d (Bound i)     = Term.Bound i
paulson@2854
   459
  | dummyTerm d (Var _)       = dummyVar
paulson@2854
   460
  | dummyTerm d (Abs(a,_))    = dummyVar
paulson@2854
   461
  | dummyTerm d (f $ u)       = Term.$ (dummyTerm d f, dummyTerm (d-1) u);
paulson@2854
   462
paulson@2854
   463
paulson@2854
   464
fun netMkRules P vars (nps: netpair list) =
paulson@2854
   465
  case P of
paulson@2854
   466
      (Const "*Goal*" $ G) =>
paulson@2854
   467
	let val pG = mk_tprop (dummyTerm 2 G)
paulson@2854
   468
	    val intrs = List.concat 
paulson@2854
   469
		             (map (fn (inet,_) => Net.unify_term inet pG) 
paulson@2854
   470
			      nps)
paulson@2854
   471
	in  map (fromIntrRule vars o #2) (orderlist intrs)  end
paulson@2854
   472
    | _ =>
paulson@2854
   473
	let val pP = mk_tprop (dummyTerm 3 P)
paulson@2854
   474
	    val elims = List.concat 
paulson@2854
   475
		             (map (fn (_,enet) => Net.unify_term enet pP) 
paulson@2854
   476
			      nps)
paulson@2854
   477
	in  List.mapPartial (fromRule vars o #2) (orderlist elims)  end;
paulson@2854
   478
paulson@2854
   479
(**
paulson@2854
   480
end;
paulson@2854
   481
**)
paulson@2854
   482
paulson@2854
   483
(*** Code for handling equality: naive substitution, like hyp_subst_tac ***)
paulson@2854
   484
paulson@2854
   485
(*Replace the ATOMIC term "old" by "new" in t*)  
paulson@2854
   486
fun subst_atomic (old,new) t =
paulson@2854
   487
    let fun subst (Var(ref(Some u))) = subst u
paulson@2854
   488
	  | subst (Abs(a,body))      = Abs(a, subst body)
paulson@2854
   489
	  | subst (f$t)              = subst f $ subst t
paulson@2854
   490
	  | subst t                  = if t aconv old then new else t
paulson@2854
   491
    in  subst t  end;
paulson@2854
   492
paulson@2854
   493
(*Eta-contract a term from outside: just enough to reduce it to an atom*)
paulson@2854
   494
fun eta_contract_atom (t0 as Abs(a, body)) = 
paulson@2854
   495
      (case  eta_contract2 body  of
paulson@2854
   496
        f $ Bound 0 => if (0 mem_int loose_bnos f) then t0
paulson@2854
   497
		       else eta_contract_atom (incr_boundvars ~1 f)
paulson@2854
   498
      | _ => t0)
paulson@2854
   499
  | eta_contract_atom t = t
paulson@2854
   500
and eta_contract2 (f$t) = f $ eta_contract_atom t
paulson@2854
   501
  | eta_contract2 t     = eta_contract_atom t;
paulson@2854
   502
paulson@2854
   503
paulson@2854
   504
(*When can we safely delete the equality?
paulson@2854
   505
    Not if it equates two constants; consider 0=1.
paulson@2854
   506
    Not if it resembles x=t[x], since substitution does not eliminate x.
paulson@2854
   507
    Not if it resembles ?x=0; another goal could instantiate ?x to Suc(i)
paulson@2854
   508
  Prefer to eliminate Bound variables if possible.
paulson@2854
   509
  Result:  true = use as is,  false = reorient first *)
paulson@2854
   510
paulson@2854
   511
(*Does t occur in u?  For substitution.  
paulson@2854
   512
  Does NOT check args of Skolem terms: substitution does not affect them.
paulson@2854
   513
  NOT reflexive since hyp_subst_tac fails on x=x.*)
paulson@2854
   514
fun substOccur t = 
paulson@2854
   515
  let fun occEq u = (t aconv u) orelse occ u
paulson@2854
   516
      and occ (Var(ref None))    = false
paulson@2854
   517
	| occ (Var(ref(Some u))) = occEq u
paulson@2854
   518
        | occ (Abs(_,u))         = occEq u
paulson@2854
   519
        | occ (f$u)              = occEq u  orelse  occEq f
paulson@2854
   520
        | occ (_)                = false;
paulson@2854
   521
  in  occEq  end;
paulson@2854
   522
paulson@2854
   523
fun check (t,u,v) = if substOccur t u then raise DEST_EQ else v;
paulson@2854
   524
paulson@2854
   525
(*IF the goal is an equality with a substitutable variable 
paulson@2854
   526
  THEN orient that equality ELSE raise exception DEST_EQ*)
paulson@2854
   527
fun orientGoal (t,u) =
paulson@2854
   528
  case (eta_contract_atom t, eta_contract_atom u) of
paulson@2854
   529
       (Skolem _, _) => check(t,u,(t,u))	(*eliminates t*)
paulson@2854
   530
     | (_, Skolem _) => check(u,t,(u,t))	(*eliminates u*)
paulson@2854
   531
     | (Free _, _)   => check(t,u,(t,u))	(*eliminates t*)
paulson@2854
   532
     | (_, Free _)   => check(u,t,(u,t))	(*eliminates u*)
paulson@2854
   533
     | _             => raise DEST_EQ;
paulson@2854
   534
paulson@2854
   535
paulson@2854
   536
(*Convert a Goal to an ordinary Not.  Used also in dup_intr, where a goal like
paulson@2854
   537
  Ex(P) is duplicated as the assumption ~Ex(P). *)
paulson@2854
   538
fun negOfGoal (Const"*Goal*" $ G, md) = (negate G, md)
paulson@2854
   539
  | negOfGoal G                   = G;
paulson@2854
   540
paulson@2854
   541
(*Substitute through the branch if an equality goal (else raise DEST_EQ)*)
paulson@2854
   542
fun equalSubst (G, br, hazs, lits, vars, lim) = 
paulson@2854
   543
  let val subst = subst_atomic (orientGoal(dest_eq G))
paulson@2854
   544
      fun subst2(G,md) = (subst G, md)
paulson@2854
   545
      fun subLits ([],        br, nlits) = 
paulson@2854
   546
	    (br, map (map subst2) hazs, nlits, vars, lim)
paulson@2854
   547
	| subLits (lit::lits, br, nlits) =
paulson@2854
   548
	    let val nlit = subst lit
paulson@2854
   549
	    in  if nlit aconv lit then subLits (lits, br, nlit::nlits)
paulson@2854
   550
		                  else subLits (lits, (nlit,true)::br, nlits)
paulson@2854
   551
            end
paulson@2854
   552
  in  subLits (rev lits,  map subst2 br,  [])  
paulson@2854
   553
  end;
paulson@2854
   554
paulson@2854
   555
paulson@2854
   556
exception NEWBRANCHES and CLOSEF;
paulson@2854
   557
paulson@2854
   558
type branch = (term*bool) list *	(*pending formulae with md flags*)
paulson@2854
   559
              (term*bool) list list *   (*stack of haz formulae*)
paulson@2854
   560
	      term list *               (*literals: irreducible formulae*)
paulson@2854
   561
	      term option ref list *    (*variables occurring in branch*)
paulson@2854
   562
	      int;                      (*resource limit*)
paulson@2854
   563
paulson@2854
   564
val fullTrace = ref[] : branch list list ref;
paulson@2854
   565
paulson@2854
   566
exception PROVE;
paulson@2854
   567
paulson@2854
   568
val eq_contr_tac = eresolve_tac [Data.notE]  THEN'  eq_assume_tac;
paulson@2854
   569
paulson@2854
   570
val eContr_tac  = TRACE Data.notE (eq_contr_tac ORELSE' Data.contr_tac);
paulson@2854
   571
val eAssume_tac = TRACE asm_rl   (eq_assume_tac ORELSE' assume_tac);
paulson@2854
   572
paulson@2854
   573
(*Try to unify complementary literals and return the corresponding tactic. *) 
paulson@2854
   574
fun tryClose (Const"*Goal*" $ G,  L) = (unify([],G,L); eAssume_tac)
paulson@2854
   575
  | tryClose (G,  Const"*Goal*" $ L) = (unify([],G,L); eAssume_tac)
paulson@2854
   576
  | tryClose (Const"Not" $ G,  L)    = (unify([],G,L); eContr_tac)
paulson@2854
   577
  | tryClose (G,  Const"Not" $ L)    = (unify([],G,L); eContr_tac)
paulson@2854
   578
  | tryClose _                       = raise UNIFY;
paulson@2854
   579
paulson@2854
   580
paulson@2854
   581
(*hazs is a list of lists of unsafe formulae.  This "stack" keeps them
paulson@2854
   582
  in the right relative order: they must go after *all* safe formulae, 
paulson@2854
   583
  with newly introduced ones coming before older ones.*)
paulson@2854
   584
paulson@2854
   585
(*Add an empty "stack frame" unless there's already one there*)
paulson@2854
   586
fun nilHaz hazs =
paulson@2854
   587
    case hazs of []::_ => hazs
paulson@2854
   588
               | _     => []::hazs;
paulson@2854
   589
paulson@2854
   590
fun addHaz (G, haz::hazs) = (haz@[negOfGoal G]) :: hazs;
paulson@2854
   591
paulson@2854
   592
(*Convert *Goal* to negated assumption in FIRST position*)
paulson@2854
   593
val negOfGoal_tac = rtac Data.ccontr THEN' rotate_tac ~1;
paulson@2854
   594
paulson@2854
   595
(*Were there Skolem terms in the premise?  Must NOT chase Vars*)
paulson@2854
   596
fun hasSkolem (Skolem _)     = true
paulson@2854
   597
  | hasSkolem (Abs (_,body)) = hasSkolem body 
paulson@2854
   598
  | hasSkolem (f$t)          =  hasSkolem f orelse hasSkolem t
paulson@2854
   599
  | hasSkolem _              = false;
paulson@2854
   600
paulson@2854
   601
(*Attach the right "may duplicate" flag to new formulae: if they contain
paulson@2854
   602
  Skolem terms then allow duplication.*)
paulson@2854
   603
fun joinMd md [] = []
paulson@2854
   604
  | joinMd md (G::Gs) = (G, hasSkolem G orelse md) :: joinMd md Gs;
paulson@2854
   605
paulson@2854
   606
(*Join new formulae to a branch.*)
paulson@2854
   607
fun appendBr md (ts,us) = 
paulson@2854
   608
    if (exists isGoal ts) then joinMd md ts @ map negOfGoal us
paulson@2854
   609
    else joinMd md ts @ us;
paulson@2854
   610
paulson@2854
   611
(** Backtracking and Pruning **)
paulson@2854
   612
paulson@2854
   613
(*clashVar vars (n,trail) determines whether any of the last n elements
paulson@2854
   614
  of "trail" occur in "vars" OR in their instantiations*)
paulson@2854
   615
fun clashVar [] = (fn _ => false)
paulson@2854
   616
  | clashVar vars =
paulson@2854
   617
      let fun clash (0, _)     = false
paulson@2854
   618
	    | clash (_, [])    = false
paulson@2854
   619
	    | clash (n, v::vs) = exists (varOccur v) vars orelse clash(n-1,vs)
paulson@2854
   620
      in  clash  end;
paulson@2854
   621
paulson@2854
   622
paulson@2854
   623
(*nbrs = # of branches just prior to closing this one.  Delete choice points
paulson@2854
   624
  for goals proved by the latest inference, provided NO variables in the
paulson@2854
   625
  next branch have been updated.*)
paulson@2854
   626
fun prune (1, nxtVars, choices) = choices  (*DON'T prune at very end: allow 
paulson@2854
   627
					     backtracking over bad proofs*)
paulson@2854
   628
  | prune (nbrs, nxtVars, choices) =
paulson@2854
   629
      let fun traceIt last =
paulson@2854
   630
		let val ll = length last
paulson@2854
   631
		    and lc = length choices
paulson@2854
   632
		in if !trace andalso ll<lc then
paulson@2854
   633
		    (writeln("PRUNING " ^ Int.toString(lc-ll) ^ " LEVELS"); 
paulson@2854
   634
		     last)
paulson@2854
   635
		   else last
paulson@2854
   636
		end
paulson@2854
   637
	  fun pruneAux (last, _, _, []) = last
paulson@2854
   638
	    | pruneAux (last, ntrl, trl, ch' as (ntrl',nbrs',exn) :: choices) =
paulson@2854
   639
		if nbrs' < nbrs 
paulson@2854
   640
		then last  (*don't backtrack beyond first solution of goal*)
paulson@2854
   641
		else if nbrs' > nbrs then pruneAux (last, ntrl, trl, choices)
paulson@2854
   642
		else (* nbrs'=nbrs *)
paulson@2854
   643
		     if clashVar nxtVars (ntrl-ntrl', trl) then last
paulson@2854
   644
		     else (*no clashes: can go back at least this far!*)
paulson@2854
   645
			  pruneAux(choices, ntrl', List.drop(trl, ntrl-ntrl'), 
paulson@2854
   646
				   choices)
paulson@2854
   647
  in  traceIt (pruneAux (choices, !ntrail, !trail, choices))  end;
paulson@2854
   648
paulson@2854
   649
fun nextVars ((br, hazs, lits, vars, lim) :: _) = map Var vars
paulson@2854
   650
  | nextVars []                                 = [];
paulson@2854
   651
paulson@2854
   652
fun backtrack ((_, _, exn)::_) = raise exn
paulson@2854
   653
  | backtrack _                = raise PROVE;
paulson@2854
   654
paulson@2854
   655
(*Change all *Goal* literals to Not.  Also delete all those identical to G.*)
paulson@2854
   656
fun addLit (Const "*Goal*" $ G,lits) = 
paulson@2854
   657
      let fun bad (Const"*Goal*" $ _) = true
paulson@2854
   658
	    | bad (Const"Not" $ G')   = G aconv G'
paulson@2854
   659
	    | bad _                   = false;
paulson@2854
   660
	  fun change [] = []
paulson@2854
   661
	    | change (Const"*Goal*" $ G' :: lits) = 
paulson@2854
   662
		  if G aconv G' then change lits
paulson@2854
   663
		  else Const"Not" $ G' :: change lits
paulson@2854
   664
	    | change (Const"Not" $ G' :: lits)    = 
paulson@2854
   665
		  if G aconv G' then change lits
paulson@2854
   666
		  else Const"Not" $ G' :: change lits
paulson@2854
   667
	    | change (lit::lits) = lit :: change lits
paulson@2854
   668
      in
paulson@2854
   669
	Const "*Goal*" $ G :: (if exists bad lits then change lits else lits)
paulson@2854
   670
      end
paulson@2854
   671
  | addLit (G,lits) = ins_term(G, lits)
paulson@2854
   672
paulson@2854
   673
paulson@2854
   674
(*Tableau prover based on leanTaP.  Argument is a list of branches.  Each 
paulson@2854
   675
  branch contains a list of unexpanded formulae, a list of literals, and a 
paulson@2854
   676
  bound on unsafe expansions.*)
paulson@2854
   677
fun prove (cs, brs, cont) =
paulson@2854
   678
 let val {safe0_netpair, safep_netpair, haz_netpair, ...} = Data.rep_claset cs
paulson@2854
   679
     val safeList = [safe0_netpair, safep_netpair]
paulson@2854
   680
     and hazList  = [haz_netpair]
paulson@2854
   681
     fun prv (tacs, trs, choices, []) = (cont (trs,choices,tacs))
paulson@2854
   682
       | prv (tacs, trs, choices, 
paulson@2854
   683
	      brs0 as ((G,md)::br, hazs, lits, vars, lim) :: brs) =
paulson@2854
   684
	  let exception PRV (*backtrack to precisely this recursion!*)
paulson@2854
   685
	      val ntrl = !ntrail 
paulson@2854
   686
	      val nbrs = length brs0
paulson@2854
   687
              val nxtVars = nextVars brs
paulson@2854
   688
	      val G = norm G
paulson@2854
   689
	      (*Make a new branch, decrementing "lim" if instantiations occur*)
paulson@2854
   690
	      fun newBr vars prems =
paulson@2854
   691
		  map (fn prem => (appendBr md (prem, br),  
paulson@2854
   692
				   nilHaz hazs,  lits,
paulson@2854
   693
				   add_terms_vars (prem,vars), 
paulson@2854
   694
				   if ntrl < !ntrail then lim-3 else lim)) 
paulson@2854
   695
		  prems @
paulson@2854
   696
		  brs		  
paulson@2854
   697
	      (*Seek a matching rule.  If unifiable then add new premises
paulson@2854
   698
                to branch.*)
paulson@2854
   699
	      fun deeper [] = raise NEWBRANCHES
paulson@2854
   700
		| deeper (((P,prems),tac)::grls) =
paulson@2854
   701
		    let val dummy = unify(add_term_vars(P,[]), P, G)
paulson@2854
   702
			    (*P comes from the rule; G comes from the branch.*)
paulson@2854
   703
                        val ntrl' = !ntrail
paulson@2854
   704
			val choices' = (ntrl, nbrs, PRV) :: choices
paulson@2854
   705
                    in
paulson@2854
   706
			if null prems then (*closed the branch: prune!*)
paulson@2854
   707
			  prv(tac false :: tacs,	(*no duplication*)
paulson@2854
   708
			      brs0::trs, 
paulson@2854
   709
			      prune (nbrs, nxtVars, choices'),
paulson@2854
   710
			      brs)
paulson@2854
   711
			  handle PRV => 
paulson@2854
   712
			      (*reset Vars and try another rule*)
paulson@2854
   713
			      (clearTo ntrl;  deeper grls)
paulson@2854
   714
		        else 
paulson@2854
   715
			  prv(tac false :: tacs,	(*no duplication*)
paulson@2854
   716
			      brs0::trs, choices',
paulson@2854
   717
			      newBr (vars_in_vars vars) prems)
paulson@2854
   718
			  handle PRV => 
paulson@2854
   719
			      if ntrl < ntrl' then
paulson@2854
   720
				   (*Vars have been updated: must backtrack
paulson@2854
   721
				     even if not mentioned in other goals!
paulson@2854
   722
				     Reset Vars and try another rule*)
paulson@2854
   723
				   (clearTo ntrl;  deeper grls)
paulson@2854
   724
			      else (*backtrack to previous level*)
paulson@2854
   725
				   backtrack choices
paulson@2854
   726
		    end
paulson@2854
   727
		    handle UNIFY => deeper grls
paulson@2854
   728
	      (*Try to close branch by unifying with head goal*)
paulson@2854
   729
	      fun closeF [] = raise CLOSEF
paulson@2854
   730
		| closeF (L::Ls) = 
paulson@2854
   731
		    let val tacs' = tryClose(G,L)::tacs
paulson@2854
   732
			val choices' = prune (nbrs, nxtVars, 
paulson@2854
   733
					      (ntrl, nbrs, PRV) :: choices)
paulson@2854
   734
		    in  prv (tacs', brs0::trs, choices', brs)
paulson@2854
   735
			handle PRV => 
paulson@2854
   736
			    (*reset Vars and try another literal
paulson@2854
   737
			      [this handler is pruned if possible!]*)
paulson@2854
   738
			 (clearTo ntrl;  closeF Ls)
paulson@2854
   739
                    end
paulson@2854
   740
		    handle UNIFY => closeF Ls
paulson@2854
   741
	  in if !trace then fullTrace := brs0 :: !fullTrace else ();
paulson@2854
   742
	     if lim<0 then backtrack choices
paulson@2854
   743
	     else
paulson@2854
   744
	     prv (Data.vars_gen_hyp_subst_tac false :: tacs, 
paulson@2854
   745
		  brs0::trs,  choices,
paulson@2854
   746
		  equalSubst (G, br, hazs, lits, vars, lim) :: brs)
paulson@2854
   747
	     handle DEST_EQ => closeF lits
paulson@2854
   748
	      handle CLOSEF => closeF (map #1 br)
paulson@2854
   749
	       handle CLOSEF => closeF (map #1 (List.concat hazs))
paulson@2854
   750
	        handle CLOSEF => 
paulson@2854
   751
		   (deeper (netMkRules G vars safeList)
paulson@2854
   752
		    handle NEWBRANCHES => 
paulson@2854
   753
		     (case netMkRules G vars hazList of
paulson@2854
   754
			 [] => (*no plausible rules: move G to literals*)
paulson@2883
   755
			     prv (tacs, brs0::trs, choices,
paulson@2854
   756
				  (br, hazs, addLit(G,lits), vars, lim)::brs)
paulson@2854
   757
		      | _ => (*G admits some haz rules: try later*)
paulson@2854
   758
			     prv (if isGoal G then negOfGoal_tac :: tacs
paulson@2854
   759
				  else tacs, 
paulson@2883
   760
				  brs0::trs,  choices,
paulson@2854
   761
				  (br, addHaz((G,md),hazs), lits, vars, lim)
paulson@2854
   762
				  ::brs)))
paulson@2854
   763
	  end
paulson@2854
   764
       | prv (tacs, trs, choices, ([], []::hazs, lits, vars, lim) :: brs) =
paulson@2854
   765
			(*removal of empty list from hazs*)
paulson@2854
   766
	   prv (tacs, trs, choices, ([], hazs, lits, vars, lim) :: brs)
paulson@2854
   767
       | prv (tacs, trs, choices, 
paulson@2854
   768
	      brs0 as ([], ((G,md)::Gs)::hazs, lits, vars, lim) :: brs) =
paulson@2854
   769
			(*application of haz rule*)
paulson@2854
   770
	  let exception PRV (*backtrack to precisely this recursion!*)
paulson@2854
   771
	      val G = norm G
paulson@2854
   772
	      val ntrl = !ntrail
paulson@2854
   773
	      fun newPrem (vars,dup) prem = 
paulson@2854
   774
		  (map (fn P => (P,false)) prem,   
paulson@2854
   775
		   nilHaz (if dup then Gs :: hazs @ [[negOfGoal (G,md)]]
paulson@2854
   776
			   else Gs :: hazs),  
paulson@2854
   777
		   lits,  
paulson@2854
   778
		   vars,  
paulson@2854
   779
		   (*Decrement "lim" if instantiations occur or the
paulson@2854
   780
		     formula is duplicated*)
paulson@2854
   781
		   if ntrl < !ntrail then lim-3 
paulson@2854
   782
		   else if dup then lim-1 else lim)
paulson@2854
   783
	      fun newBr x prems = map (newPrem x) prems  @  brs
paulson@2854
   784
	      (*Seek a matching rule.  If unifiable then add new premises
paulson@2854
   785
                to branch.*)
paulson@2854
   786
	      fun deeper [] = raise NEWBRANCHES
paulson@2854
   787
		| deeper (((P,prems),tac)::grls) =
paulson@2854
   788
		    let val dummy = unify(add_term_vars(P,[]), P, G)
paulson@2854
   789
			val ntrl' = !ntrail
paulson@2854
   790
                        val vars  = vars_in_vars vars
paulson@2854
   791
                        val vars' = foldr add_terms_vars (prems, vars)
paulson@2854
   792
                        val dup = md andalso vars' <> vars
paulson@2854
   793
			(*duplicate G only if md and the premise has new vars*)
paulson@2854
   794
                    in
paulson@2854
   795
		      prv(tac dup :: tacs, 
paulson@2854
   796
			  brs0::trs, 
paulson@2854
   797
			  (ntrl, length brs0, PRV) :: choices, 
paulson@2854
   798
			  newBr (vars', dup) prems)
paulson@2854
   799
		     handle PRV => 
paulson@2854
   800
			 if ntrl < ntrl'       (*variables updated?*)
paulson@2883
   801
                            orelse vars=vars'  (*pseudo-unsafe: no new Vars?*)
paulson@2854
   802
			 then (*reset Vars and try another rule*)
paulson@2854
   803
			      (clearTo ntrl;  deeper grls)
paulson@2854
   804
			 else (*backtrack to previous level*)
paulson@2854
   805
			      backtrack choices
paulson@2854
   806
		    end
paulson@2854
   807
		    handle UNIFY => deeper grls
paulson@2854
   808
	  in if !trace then fullTrace := brs0 :: !fullTrace else ();
paulson@2854
   809
	     if lim<1 then backtrack choices
paulson@2854
   810
	     else
paulson@2854
   811
	     deeper (netMkRules G vars hazList)
paulson@2854
   812
	     handle NEWBRANCHES => 
paulson@2854
   813
		 (*cannot close branch: move G to literals*)
paulson@2854
   814
		 prv (tacs,  brs0::trs,  choices,
paulson@2854
   815
		      ([], Gs::hazs, G::lits, vars, lim)::brs)
paulson@2854
   816
	  end
paulson@2854
   817
       | prv (tacs, trs, choices, _ :: brs) = backtrack choices
paulson@2854
   818
 in prv ([], [], [(!ntrail, length brs, PROVE)], brs) end;
paulson@2854
   819
paulson@2854
   820
paulson@2883
   821
(*Construct an initial branch.*)
paulson@2854
   822
fun initBranch (ts,lim) = 
paulson@2854
   823
    (map (fn t => (t,true)) ts, 
paulson@2854
   824
     [[]], [], add_terms_vars (ts,[]), lim);
paulson@2854
   825
paulson@2854
   826
paulson@2854
   827
(*** Conversion & Skolemization of the Isabelle proof state ***)
paulson@2854
   828
paulson@2854
   829
(*Make a list of all the parameters in a subgoal, even if nested*)
paulson@2854
   830
local open Term 
paulson@2854
   831
in
paulson@2854
   832
fun discard_foralls (Const("all",_)$Abs(a,T,t)) = discard_foralls t
paulson@2854
   833
  | discard_foralls t = t;
paulson@2854
   834
end;
paulson@2854
   835
paulson@2854
   836
paulson@2854
   837
(*List of variables not appearing as arguments to the given parameter*)
paulson@2854
   838
fun getVars []                  i = []
paulson@2854
   839
  | getVars ((_,(v,is))::alist) i =
paulson@2854
   840
	if i mem is then getVars alist i
paulson@2854
   841
	else v :: getVars alist i;
paulson@2854
   842
paulson@2854
   843
paulson@2854
   844
(*Conversion of a subgoal: Skolemize all parameters*)
paulson@2854
   845
fun fromSubgoal tsig t =
paulson@2854
   846
  let val alist = ref []
paulson@2854
   847
      fun hdvar ((ix,(v,is))::_) = v
paulson@2854
   848
      fun from lev t =
paulson@2854
   849
	let val (ht,ts) = Term.strip_comb t
paulson@2854
   850
	    fun apply u = list_comb (u, map (from lev) ts)
paulson@2854
   851
	    fun bounds [] = []
paulson@2854
   852
	      | bounds (Term.Bound i::ts) = 
paulson@2854
   853
		  if i<lev then error"Function Var's argument not a parameter"
paulson@2854
   854
		  else i-lev :: bounds ts
paulson@2854
   855
	      | bounds ts = error"Function Var's argument not a bound variable"
paulson@2854
   856
        in
paulson@2854
   857
	  case ht of 
paulson@2854
   858
	      t as Term.Const _ => apply (fromConst tsig t)	    
paulson@2854
   859
	    | Term.Free  (a,_) => apply (Free a)
paulson@2854
   860
	    | Term.Bound i     => apply (Bound i)
paulson@2854
   861
	    | Term.Var (ix,_) => 
paulson@2854
   862
		  (case (assoc_string_int(!alist,ix)) of
paulson@2854
   863
		       None => (alist := (ix, (ref None, bounds ts))
paulson@2854
   864
					  :: !alist;
paulson@2854
   865
				Var (hdvar(!alist)))
paulson@2854
   866
		     | Some(v,is) => if is=bounds ts then Var v
paulson@2854
   867
			    else error("Discrepancy among occurrences of ?"
paulson@2854
   868
				       ^ Syntax.string_of_vname ix))
paulson@2854
   869
	    | Term.Abs (a,_,body) => 
paulson@2854
   870
		  if null ts then Abs(a, from (lev+1) body)
paulson@2854
   871
		  else error "fromSubgoal: argument not in normal form"
paulson@2854
   872
        end
paulson@2854
   873
paulson@2854
   874
      val npars = length (Logic.strip_params t)
paulson@2854
   875
paulson@2854
   876
      (*Skolemize a subgoal from a proof state*)
paulson@2854
   877
      fun skoSubgoal i t =
paulson@2854
   878
	  if i<npars then 
paulson@2854
   879
	      skoSubgoal (i+1)
paulson@2854
   880
		(subst_bound (Skolem (gensym "SG_", getVars (!alist) i), 
paulson@2854
   881
			      t))
paulson@2854
   882
	  else t
paulson@2854
   883
paulson@2854
   884
  in  skoSubgoal 0 (from 0 (discard_foralls t))  end;
paulson@2854
   885
paulson@2854
   886
paulson@2854
   887
(*Tactic using tableau engine and proof reconstruction.  
paulson@2854
   888
 "lim" is depth limit.*)
paulson@2854
   889
fun depth_tac cs lim i st = 
paulson@2854
   890
    (fullTrace:=[];  trail := [];  ntrail := 0;
paulson@2854
   891
     let val {tsig,...} = Sign.rep_sg (#sign (rep_thm st))
paulson@2854
   892
	 val skoprem = fromSubgoal tsig (List.nth(prems_of st, i-1))
paulson@2854
   893
         val hyps  = strip_imp_prems skoprem
paulson@2854
   894
         and concl = strip_imp_concl skoprem
paulson@2854
   895
         fun cont (_,choices,tacs) = 
paulson@2854
   896
	     (case Sequence.pull(EVERY' (rev tacs) i st) of
paulson@2854
   897
		  None => (writeln ("PROOF FAILED for depth " ^
paulson@2854
   898
				    Int.toString lim);
paulson@2854
   899
			   backtrack choices)
paulson@2854
   900
		| cell => Sequence.seqof(fn()=> cell))
paulson@2854
   901
     in prove (cs, [initBranch (mkGoal concl :: hyps, lim)], cont)
paulson@2854
   902
     end
paulson@2854
   903
     handle Subscript => Sequence.null
paulson@2854
   904
	  | PROVE     => Sequence.null);
paulson@2854
   905
paulson@2854
   906
fun blast_tac cs = (DEEPEN (1,20) (depth_tac cs) 0);
paulson@2854
   907
paulson@2854
   908
fun Blast_tac i = blast_tac (!Data.claset) i;
paulson@2854
   909
paulson@2854
   910
end;
paulson@2854
   911