src/Provers/simp.ML
author clasohm
Thu Sep 16 12:20:38 1993 +0200 (1993-09-16 ago)
changeset 0 a5a9c433f639
child 1 c6a6e3db5353
permissions -rw-r--r--
Initial revision
clasohm@0
     1
(*  Title:      Provers/simp
clasohm@0
     2
    ID:         $Id$
clasohm@0
     3
    Author:     Tobias Nipkow
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
clasohm@0
     6
Generic simplifier, suitable for most logics.  The only known exception is
clasohm@0
     7
Constructive Type Theory.  The reflexivity axiom must be unconditional,
clasohm@0
     8
namely a=a not a:A ==> a=a:A.  Used typedsimp.ML otherwise.  
clasohm@0
     9
*)
clasohm@0
    10
clasohm@0
    11
signature SIMP_DATA =
clasohm@0
    12
sig
clasohm@0
    13
  val dest_red     : term -> term * term * term
clasohm@0
    14
  val mk_rew_rules : thm -> thm list
clasohm@0
    15
  val norm_thms    : (thm*thm) list (* [(?x>>norm(?x), norm(?x)>>?x), ...] *)
clasohm@0
    16
  val red1         : thm        (*  ?P>>?Q  ==>  ?P  ==>  ?Q  *)
clasohm@0
    17
  val red2         : thm        (*  ?P>>?Q  ==>  ?Q  ==>  ?P  *)
clasohm@0
    18
  val refl_thms    : thm list
clasohm@0
    19
  val subst_thms   : thm list   (* [ ?a>>?b ==> ?P(?a) ==> ?P(?b), ...] *)
clasohm@0
    20
  val trans_thms   : thm list
clasohm@0
    21
end;
clasohm@0
    22
clasohm@0
    23
clasohm@0
    24
infix 4 addrews addcongs addsplits delrews delcongs setauto;
clasohm@0
    25
clasohm@0
    26
signature SIMP =
clasohm@0
    27
sig
clasohm@0
    28
  type simpset
clasohm@0
    29
  val empty_ss  : simpset
clasohm@0
    30
  val addcongs  : simpset * thm list -> simpset
clasohm@0
    31
  val addrews   : simpset * thm list -> simpset
clasohm@0
    32
  val addsplits : simpset * thm list -> simpset
clasohm@0
    33
  val delcongs  : simpset * thm list -> simpset
clasohm@0
    34
  val delrews   : simpset * thm list -> simpset
clasohm@0
    35
  val dest_ss   : simpset -> thm list * thm list
clasohm@0
    36
  val print_ss  : simpset -> unit
clasohm@0
    37
  val setauto   : simpset * (thm list -> int -> tactic) -> simpset
clasohm@0
    38
  val ASM_SIMP_TAC      : simpset -> int -> tactic
clasohm@0
    39
  val SPLIT_TAC          : simpset -> int -> tactic
clasohm@0
    40
  val SIMP_SPLIT2_TAC    : simpset -> int -> tactic
clasohm@0
    41
  val SIMP_THM          : simpset -> thm -> thm
clasohm@0
    42
  val SIMP_TAC          : simpset -> int -> tactic
clasohm@0
    43
  val mk_congs          : theory -> string list -> thm list
clasohm@0
    44
  val mk_typed_congs    : theory -> (string * string) list -> thm list
clasohm@0
    45
(* temporarily disabled:
clasohm@0
    46
  val extract_free_congs        : unit -> thm list
clasohm@0
    47
*)
clasohm@0
    48
  val tracing   : bool ref
clasohm@0
    49
end;
clasohm@0
    50
clasohm@0
    51
functor SimpFun (Simp_data: SIMP_DATA) : SIMP = 
clasohm@0
    52
struct
clasohm@0
    53
clasohm@0
    54
local open Simp_data Logic in
clasohm@0
    55
clasohm@0
    56
(*For taking apart reductions into left, right hand sides*)
clasohm@0
    57
val lhs_of = #2 o dest_red;
clasohm@0
    58
val rhs_of = #3 o dest_red;
clasohm@0
    59
clasohm@0
    60
(*** Indexing and filtering of theorems ***)
clasohm@0
    61
clasohm@0
    62
fun eq_brl ((b1,th1),(b2,th2)) = b1=b2 andalso eq_thm(th1,th2);
clasohm@0
    63
clasohm@0
    64
(*insert a thm in a discrimination net by its lhs*)
clasohm@0
    65
fun lhs_insert_thm (th,net) =
clasohm@0
    66
    Net.insert_term((lhs_of (concl_of th), (false,th)), net, eq_brl)
clasohm@0
    67
    handle  Net.INSERT => net;
clasohm@0
    68
clasohm@0
    69
(*match subgoal i against possible theorems in the net.
clasohm@0
    70
  Similar to match_from_nat_tac, but the net does not contain numbers;
clasohm@0
    71
  rewrite rules are not ordered.*)
clasohm@0
    72
fun net_tac net =
clasohm@0
    73
  SUBGOAL(fn (prem,i) => 
clasohm@0
    74
	  match_tac (Net.match_term net (strip_assums_concl prem)) i);
clasohm@0
    75
clasohm@0
    76
(*match subgoal i against possible theorems indexed by lhs in the net*)
clasohm@0
    77
fun lhs_net_tac net =
clasohm@0
    78
  SUBGOAL(fn (prem,i) => 
clasohm@0
    79
	  bimatch_tac (Net.match_term net
clasohm@0
    80
		       (lhs_of (strip_assums_concl prem))) i);
clasohm@0
    81
clasohm@0
    82
fun nth_subgoal i thm = nth_elem(i-1,prems_of thm);
clasohm@0
    83
clasohm@0
    84
fun goal_concl i thm = strip_assums_concl(nth_subgoal i thm);
clasohm@0
    85
clasohm@0
    86
fun lhs_of_eq i thm = lhs_of(goal_concl i thm)
clasohm@0
    87
and rhs_of_eq i thm = rhs_of(goal_concl i thm);
clasohm@0
    88
clasohm@0
    89
fun var_lhs(thm,i) =
clasohm@0
    90
let fun var(Var _) = true
clasohm@0
    91
      | var(Abs(_,_,t)) = var t
clasohm@0
    92
      | var(f$_) = var f
clasohm@0
    93
      | var _ = false;
clasohm@0
    94
in var(lhs_of_eq i thm) end;
clasohm@0
    95
clasohm@0
    96
fun contains_op opns =
clasohm@0
    97
    let fun contains(Const(s,_)) = s mem opns |
clasohm@0
    98
            contains(s$t) = contains s orelse contains t |
clasohm@0
    99
            contains(Abs(_,_,t)) = contains t |
clasohm@0
   100
            contains _ = false;
clasohm@0
   101
    in contains end;
clasohm@0
   102
clasohm@0
   103
fun may_match(match_ops,i) = contains_op match_ops o lhs_of_eq i;
clasohm@0
   104
clasohm@0
   105
val (normI_thms,normE_thms) = split_list norm_thms;
clasohm@0
   106
clasohm@0
   107
(*Get the norm constants from norm_thms*)
clasohm@0
   108
val norms =
clasohm@0
   109
  let fun norm thm = 
clasohm@0
   110
      case lhs_of(concl_of thm) of
clasohm@0
   111
	  Const(n,_)$_ => n
clasohm@0
   112
	| _ => (prths normE_thms; error"No constant in lhs of a norm_thm")
clasohm@0
   113
  in map norm normE_thms end;
clasohm@0
   114
clasohm@0
   115
fun lhs_is_NORM(thm,i) = case lhs_of_eq i thm of
clasohm@0
   116
	Const(s,_)$_ => s mem norms | _ => false;
clasohm@0
   117
clasohm@0
   118
val refl_tac = resolve_tac refl_thms;
clasohm@0
   119
clasohm@0
   120
fun find_res thms thm =
clasohm@0
   121
    let fun find [] = (prths thms; error"Check Simp_Data")
clasohm@0
   122
          | find(th::thms) = thm RS th handle _ => find thms
clasohm@0
   123
    in find thms end;
clasohm@0
   124
clasohm@0
   125
val mk_trans = find_res trans_thms;
clasohm@0
   126
clasohm@0
   127
fun mk_trans2 thm =
clasohm@0
   128
let fun mk[] = error"Check transitivity"
clasohm@0
   129
      | mk(t::ts) = (thm RSN (2,t))  handle _  => mk ts
clasohm@0
   130
in mk trans_thms end;
clasohm@0
   131
clasohm@0
   132
(*Applies tactic and returns the first resulting state, FAILS if none!*)
clasohm@0
   133
fun one_result(tac,thm) = case Sequence.pull(tapply(tac,thm)) of
clasohm@0
   134
	Some(thm',_) => thm'
clasohm@0
   135
      | None => raise THM("Simplifier: could not continue", 0, [thm]);
clasohm@0
   136
clasohm@0
   137
fun res1(thm,thms,i) = one_result(resolve_tac thms i,thm);
clasohm@0
   138
clasohm@0
   139
clasohm@0
   140
(**** Adding "NORM" tags ****)
clasohm@0
   141
clasohm@0
   142
(*get name of the constant from conclusion of a congruence rule*)
clasohm@0
   143
fun cong_const cong = 
clasohm@0
   144
    case head_of (lhs_of (concl_of cong)) of
clasohm@0
   145
	Const(c,_) => c
clasohm@0
   146
      | _ => ""			(*a placeholder distinct from const names*);
clasohm@0
   147
clasohm@0
   148
(*true if the term is an atomic proposition (no ==> signs) *)
clasohm@0
   149
val atomic = null o strip_assums_hyp;
clasohm@0
   150
clasohm@0
   151
(*ccs contains the names of the constants possessing congruence rules*)
clasohm@0
   152
fun add_hidden_vars ccs =
clasohm@0
   153
  let fun add_hvars(tm,hvars) = case tm of
clasohm@0
   154
	      Abs(_,_,body) => add_term_vars(body,hvars)
clasohm@0
   155
	    | _$_ => let val (f,args) = strip_comb tm 
clasohm@0
   156
		     in case f of
clasohm@0
   157
			    Const(c,T) => 
clasohm@0
   158
				if c mem ccs
clasohm@0
   159
				then foldr add_hvars (args,hvars)
clasohm@0
   160
				else add_term_vars(tm,hvars)
clasohm@0
   161
			  | _ => add_term_vars(tm,hvars)
clasohm@0
   162
		     end
clasohm@0
   163
	    | _ => hvars;
clasohm@0
   164
  in add_hvars end;
clasohm@0
   165
clasohm@0
   166
fun add_new_asm_vars new_asms =
clasohm@0
   167
    let fun itf((tm,at),vars) =
clasohm@0
   168
		if at then vars else add_term_vars(tm,vars)
clasohm@0
   169
	fun add_list(tm,al,vars) = let val (_,tml) = strip_comb tm
clasohm@0
   170
		in if length(tml)=length(al)
clasohm@0
   171
		   then foldr itf (tml~~al,vars)
clasohm@0
   172
		   else vars
clasohm@0
   173
		end
clasohm@0
   174
	fun add_vars (tm,vars) = case tm of
clasohm@0
   175
		  Abs (_,_,body) => add_vars(body,vars)
clasohm@0
   176
		| r$s => (case head_of tm of
clasohm@0
   177
			  Const(c,T) => (case assoc(new_asms,c) of
clasohm@0
   178
				  None => add_vars(r,add_vars(s,vars))
clasohm@0
   179
				| Some(al) => add_list(tm,al,vars))
clasohm@0
   180
			| _ => add_vars(r,add_vars(s,vars)))
clasohm@0
   181
		| _ => vars
clasohm@0
   182
    in add_vars end;
clasohm@0
   183
clasohm@0
   184
clasohm@0
   185
fun add_norms(congs,ccs,new_asms) thm =
clasohm@0
   186
let val thm' = mk_trans2 thm;
clasohm@0
   187
(* thm': [?z -> l; Prems; r -> ?t] ==> ?z -> ?t *)
clasohm@0
   188
    val nops = nprems_of thm'
clasohm@0
   189
    val lhs = rhs_of_eq 1 thm'
clasohm@0
   190
    val rhs = lhs_of_eq nops thm'
clasohm@0
   191
    val asms = tl(rev(tl(prems_of thm')))
clasohm@0
   192
    val hvars = foldr (add_hidden_vars ccs) (lhs::rhs::asms,[])
clasohm@0
   193
    val hvars = add_new_asm_vars new_asms (rhs,hvars)
clasohm@0
   194
    fun it_asms (asm,hvars) =
clasohm@0
   195
	if atomic asm then add_new_asm_vars new_asms (asm,hvars)
clasohm@0
   196
	else add_term_frees(asm,hvars)
clasohm@0
   197
    val hvars = foldr it_asms (asms,hvars)
clasohm@0
   198
    val hvs = map (#1 o dest_Var) hvars
clasohm@0
   199
    val refl1_tac = refl_tac 1
clasohm@0
   200
    val add_norm_tac = DEPTH_FIRST (has_fewer_prems nops)
clasohm@0
   201
	      (STATE(fn thm =>
clasohm@0
   202
		case head_of(rhs_of_eq 1 thm) of
clasohm@0
   203
		  Var(ixn,_) => if ixn mem hvs then refl1_tac
clasohm@0
   204
				else resolve_tac normI_thms 1 ORELSE refl1_tac
clasohm@0
   205
		| Const _ => resolve_tac normI_thms 1 ORELSE
clasohm@0
   206
			     resolve_tac congs 1 ORELSE refl1_tac
clasohm@0
   207
		| Free _ => resolve_tac congs 1 ORELSE refl1_tac
clasohm@0
   208
		| _ => refl1_tac))
clasohm@0
   209
    val Some(thm'',_) = Sequence.pull(tapply(add_norm_tac,thm'))
clasohm@0
   210
in thm'' end;
clasohm@0
   211
clasohm@0
   212
fun add_norm_tags congs =
clasohm@0
   213
    let val ccs = map cong_const congs
clasohm@0
   214
	val new_asms = filter (exists not o #2)
clasohm@0
   215
		(ccs ~~ (map (map atomic o prems_of) congs));
clasohm@0
   216
    in add_norms(congs,ccs,new_asms) end;
clasohm@0
   217
clasohm@0
   218
fun normed_rews congs =
clasohm@0
   219
  let val add_norms = add_norm_tags congs;
clasohm@0
   220
  in fn thm => map (varifyT o add_norms o mk_trans) (mk_rew_rules(freezeT thm))
clasohm@0
   221
  end;
clasohm@0
   222
clasohm@0
   223
fun NORM norm_lhs_tac = EVERY'[resolve_tac [red2], norm_lhs_tac, refl_tac];
clasohm@0
   224
clasohm@0
   225
val trans_norms = map mk_trans normE_thms;
clasohm@0
   226
clasohm@0
   227
clasohm@0
   228
(* SIMPSET *)
clasohm@0
   229
clasohm@0
   230
datatype simpset =
clasohm@0
   231
	SS of {auto_tac: thm list -> int -> tactic,
clasohm@0
   232
	       congs: thm list,
clasohm@0
   233
	       cong_net: thm Net.net,
clasohm@0
   234
	       mk_simps: thm -> thm list,
clasohm@0
   235
	       simps: (thm * thm list) list,
clasohm@0
   236
	       simp_net: thm Net.net,
clasohm@0
   237
               splits: thm list,
clasohm@0
   238
               split_consts: string list}
clasohm@0
   239
clasohm@0
   240
val empty_ss = SS{auto_tac= K (K no_tac), congs=[], cong_net=Net.empty,
clasohm@0
   241
		  mk_simps=normed_rews[], simps=[], simp_net=Net.empty,
clasohm@0
   242
                  splits=[], split_consts=[]};
clasohm@0
   243
clasohm@0
   244
(** Insertion of congruences, rewrites and case splits **)
clasohm@0
   245
clasohm@0
   246
(*insert a thm in a thm net*)
clasohm@0
   247
fun insert_thm_warn (th,net) = 
clasohm@0
   248
  Net.insert_term((concl_of th, th), net, eq_thm)
clasohm@0
   249
  handle Net.INSERT => 
clasohm@0
   250
    (writeln"\nDuplicate rewrite or congruence rule:"; print_thm th;
clasohm@0
   251
     net);
clasohm@0
   252
clasohm@0
   253
val insert_thms = foldr insert_thm_warn;
clasohm@0
   254
clasohm@0
   255
fun addrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
clasohm@0
   256
              splits,split_consts}, thm) =
clasohm@0
   257
let val thms = mk_simps thm
clasohm@0
   258
in SS{auto_tac=auto_tac,congs=congs, cong_net=cong_net, mk_simps=mk_simps,
clasohm@0
   259
      simps = (thm,thms)::simps, simp_net = insert_thms(thms,simp_net),
clasohm@0
   260
      splits=splits,split_consts=split_consts}
clasohm@0
   261
end;
clasohm@0
   262
clasohm@0
   263
val op addrews = foldl addrew;
clasohm@0
   264
clasohm@0
   265
fun op addcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
clasohm@0
   266
                   splits,split_consts}, thms) =
clasohm@0
   267
let val congs' = thms @ congs;
clasohm@0
   268
in SS{auto_tac=auto_tac, congs= congs',
clasohm@0
   269
      cong_net= insert_thms (map mk_trans thms,cong_net),
clasohm@0
   270
      mk_simps= normed_rews congs', simps=simps, simp_net=simp_net,
clasohm@0
   271
      splits=splits,split_consts=split_consts}
clasohm@0
   272
end;
clasohm@0
   273
clasohm@0
   274
fun split_err() = error("split rule not of the form ?P(c(...)) = ...");
clasohm@0
   275
clasohm@0
   276
fun split_const(_ $ t) =
clasohm@0
   277
       (case head_of t of Const(a,_) => a | _ => split_err())
clasohm@0
   278
  | split_const _ = split_err();
clasohm@0
   279
clasohm@0
   280
fun addsplit(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
clasohm@0
   281
                splits,split_consts}, thm) =
clasohm@0
   282
let val a = split_const(lhs_of(concl_of thm))
clasohm@0
   283
in SS{auto_tac=auto_tac,congs=congs,cong_net=cong_net,
clasohm@0
   284
      mk_simps=mk_simps,simps=simps,simp_net=simp_net,
clasohm@0
   285
      splits=splits@[mk_trans thm],split_consts=split_consts@[a]} end;
clasohm@0
   286
clasohm@0
   287
val op addsplits = foldl addsplit;
clasohm@0
   288
clasohm@0
   289
(** Deletion of congruences and rewrites **)
clasohm@0
   290
clasohm@0
   291
(*delete a thm from a thm net*)
clasohm@0
   292
fun delete_thm_warn (th,net) = 
clasohm@0
   293
  Net.delete_term((concl_of th, th), net, eq_thm)
clasohm@0
   294
  handle Net.DELETE => 
clasohm@0
   295
    (writeln"\nNo such rewrite or congruence rule:";  print_thm th;
clasohm@0
   296
     net);
clasohm@0
   297
clasohm@0
   298
val delete_thms = foldr delete_thm_warn;
clasohm@0
   299
clasohm@0
   300
fun op delcongs(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
clasohm@0
   301
                   splits,split_consts}, thms) =
clasohm@0
   302
let val congs' = foldl (gen_rem eq_thm) (congs,thms)
clasohm@0
   303
in SS{auto_tac=auto_tac, congs= congs',
clasohm@0
   304
      cong_net= delete_thms(map mk_trans thms,cong_net),
clasohm@0
   305
      mk_simps= normed_rews congs', simps=simps, simp_net=simp_net,
clasohm@0
   306
      splits=splits,split_consts=split_consts}
clasohm@0
   307
end;
clasohm@0
   308
clasohm@0
   309
fun delrew(SS{auto_tac,congs,cong_net,mk_simps,simps,simp_net,
clasohm@0
   310
              splits,split_consts}, thm) =
clasohm@0
   311
let fun find((p as (th,ths))::ps',ps) =
clasohm@0
   312
	  if eq_thm(thm,th) then (ths,ps@ps') else find(ps',p::ps)
clasohm@0
   313
      | find([],simps') = (writeln"\nNo such rewrite or congruence rule:";
clasohm@0
   314
			   print_thm thm;
clasohm@0
   315
			   ([],simps'))
clasohm@0
   316
    val (thms,simps') = find(simps,[])
clasohm@0
   317
in SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps,
clasohm@0
   318
      simps = simps', simp_net = delete_thms(thms,simp_net),
clasohm@0
   319
      splits=splits,split_consts=split_consts}
clasohm@0
   320
end;
clasohm@0
   321
clasohm@0
   322
val op delrews = foldl delrew;
clasohm@0
   323
clasohm@0
   324
clasohm@0
   325
fun op setauto(SS{congs,cong_net,mk_simps,simps,simp_net,
clasohm@0
   326
                  splits,split_consts,...}, auto_tac) =
clasohm@0
   327
    SS{auto_tac=auto_tac, congs=congs, cong_net=cong_net, mk_simps=mk_simps,
clasohm@0
   328
       simps=simps, simp_net=simp_net,splits=splits,split_consts=split_consts};
clasohm@0
   329
clasohm@0
   330
clasohm@0
   331
(** Inspection of a simpset **)
clasohm@0
   332
clasohm@0
   333
fun dest_ss(SS{congs,simps,...}) = (congs, map #1 simps);
clasohm@0
   334
clasohm@0
   335
fun print_ss(SS{congs,simps,splits,...}) =
clasohm@0
   336
	(writeln"Congruences:"; prths congs;
clasohm@0
   337
         writeln"Case Splits"; prths splits;
clasohm@0
   338
	 writeln"Rewrite Rules:"; prths (map #1 simps); ());
clasohm@0
   339
clasohm@0
   340
clasohm@0
   341
(* Rewriting with case splits *)
clasohm@0
   342
clasohm@0
   343
fun splittable a i thm =
clasohm@0
   344
    let val tm = goal_concl i thm
clasohm@0
   345
	fun nobound(Abs(_,_,tm),j,k) = nobound(tm,j,k+1)
clasohm@0
   346
	  | nobound(s$t,j,k) = nobound(s,j,k) andalso nobound(t,j,k)
clasohm@0
   347
	  | nobound(Bound n,j,k) = n < k orelse k+j <= n
clasohm@0
   348
	  | nobound(_) = true;
clasohm@0
   349
	fun check_args(al,j) = forall (fn t => nobound(t,j,0)) al
clasohm@0
   350
	fun find_if(Abs(_,_,tm),j) = find_if(tm,j+1)
clasohm@0
   351
	  | find_if(tm as s$t,j) = let val (f,al) = strip_comb tm in
clasohm@0
   352
		case f of Const(c,_) =>	if c=a then check_args(al,j)
clasohm@0
   353
			else find_if(s,j) orelse find_if(t,j)
clasohm@0
   354
		| _ => find_if(s,j) orelse find_if(t,j) end
clasohm@0
   355
	  | find_if(_) = false;
clasohm@0
   356
    in find_if(tm,0) end;
clasohm@0
   357
clasohm@0
   358
fun split_tac (cong_tac,splits,split_consts) i =
clasohm@0
   359
    let fun seq_try (split::splits,a::bs) thm = tapply(
clasohm@0
   360
		COND (splittable a i) (DETERM(resolve_tac[split]i))
clasohm@0
   361
			(Tactic(seq_try(splits,bs))), thm)
clasohm@0
   362
	      | seq_try([],_) thm = tapply(no_tac,thm)
clasohm@0
   363
	and try_rew thm = tapply(Tactic(seq_try(splits,split_consts))
clasohm@0
   364
				 ORELSE Tactic one_subt, thm)
clasohm@0
   365
	and one_subt thm =
clasohm@0
   366
		let val test = has_fewer_prems (nprems_of thm + 1)
clasohm@0
   367
		    fun loop thm = tapply(COND test no_tac
clasohm@0
   368
			((Tactic try_rew THEN DEPTH_FIRST test (refl_tac i))
clasohm@0
   369
			 ORELSE (refl_tac i THEN Tactic loop)), thm)
clasohm@0
   370
		in tapply(cong_tac THEN Tactic loop, thm) end
clasohm@0
   371
    in if null splits then no_tac
clasohm@0
   372
       else COND (may_match(split_consts,i)) (Tactic try_rew) no_tac
clasohm@0
   373
    end;
clasohm@0
   374
clasohm@0
   375
fun SPLIT_TAC (SS{cong_net,splits,split_consts,...}) i =
clasohm@0
   376
let val cong_tac = net_tac cong_net i
clasohm@0
   377
in NORM (split_tac (cong_tac,splits,split_consts)) i end;
clasohm@0
   378
clasohm@0
   379
(* Rewriting Automaton *)
clasohm@0
   380
clasohm@0
   381
datatype cntrl = STOP | MK_EQ | ASMS of int | SIMP_LHS | REW | REFL | TRUE
clasohm@0
   382
	       | PROVE | POP_CS | POP_ARTR | SPLIT;
clasohm@0
   383
(*
clasohm@0
   384
fun pr_cntrl c = case c of STOP => prs("STOP") | MK_EQ => prs("MK_EQ") |
clasohm@0
   385
ASMS i => print_int i | POP_ARTR => prs("POP_ARTR") |
clasohm@0
   386
SIMP_LHS => prs("SIMP_LHS") | REW => prs("REW") | REFL => prs("REFL") |
clasohm@0
   387
TRUE => prs("TRUE") | PROVE => prs("PROVE") | POP_CS => prs("POP_CS") | SPLIT
clasohm@0
   388
=> prs("SPLIT");
clasohm@0
   389
*)
clasohm@0
   390
fun simp_refl([],_,ss) = ss
clasohm@0
   391
  | simp_refl(a'::ns,a,ss) = if a'=a then simp_refl(ns,a,SIMP_LHS::REFL::ss)
clasohm@0
   392
	else simp_refl(ns,a,ASMS(a)::SIMP_LHS::REFL::POP_ARTR::ss);
clasohm@0
   393
clasohm@0
   394
(** Tracing **)
clasohm@0
   395
clasohm@0
   396
val tracing = ref false;
clasohm@0
   397
clasohm@0
   398
(*Replace parameters by Free variables in P*)
clasohm@0
   399
fun variants_abs ([],P) = P
clasohm@0
   400
  | variants_abs ((a,T)::aTs, P) =
clasohm@0
   401
      variants_abs (aTs, #2 (variant_abs(a,T,P)));
clasohm@0
   402
clasohm@0
   403
(*Select subgoal i from proof state; substitute parameters, for printing*)
clasohm@0
   404
fun prepare_goal i st =
clasohm@0
   405
    let val subgi = nth_subgoal i st
clasohm@0
   406
	val params = rev(strip_params subgi)
clasohm@0
   407
    in variants_abs (params, strip_assums_concl subgi) end;
clasohm@0
   408
clasohm@0
   409
(*print lhs of conclusion of subgoal i*)
clasohm@0
   410
fun pr_goal_lhs i st =
clasohm@0
   411
    writeln (Sign.string_of_term (#sign(rep_thm st)) 
clasohm@0
   412
	     (lhs_of (prepare_goal i st)));
clasohm@0
   413
clasohm@0
   414
(*print conclusion of subgoal i*)
clasohm@0
   415
fun pr_goal_concl i st =
clasohm@0
   416
    writeln (Sign.string_of_term (#sign(rep_thm st)) (prepare_goal i st)) 
clasohm@0
   417
clasohm@0
   418
(*print subgoals i to j (inclusive)*)
clasohm@0
   419
fun pr_goals (i,j) st =
clasohm@0
   420
    if i>j then ()
clasohm@0
   421
    else (pr_goal_concl i st;  pr_goals (i+1,j) st);
clasohm@0
   422
clasohm@0
   423
(*Print rewrite for tracing; i=subgoal#, n=number of new subgoals,
clasohm@0
   424
  thm=old state, thm'=new state *)
clasohm@0
   425
fun pr_rew (i,n,thm,thm',not_asms) =
clasohm@0
   426
    if !tracing
clasohm@0
   427
    then (if not_asms then () else writeln"Assumption used in";
clasohm@0
   428
          pr_goal_lhs i thm; writeln"->"; pr_goal_lhs (i+n) thm';
clasohm@0
   429
	  if n>0 then (writeln"Conditions:"; pr_goals (i, i+n-1) thm')
clasohm@0
   430
          else ();
clasohm@0
   431
          writeln"" )
clasohm@0
   432
    else ();
clasohm@0
   433
clasohm@0
   434
(* Skip the first n hyps of a goal, and return the rest in generalized form *)
clasohm@0
   435
fun strip_varify(Const("==>", _) $ H $ B, n, vs) =
clasohm@0
   436
	if n=0 then subst_bounds(vs,H)::strip_varify(B,0,vs)
clasohm@0
   437
	else strip_varify(B,n-1,vs)
clasohm@0
   438
  | strip_varify(Const("all",_)$Abs(_,T,t), n, vs) =
clasohm@0
   439
	strip_varify(t,n,Var(("?",length vs),T)::vs)
clasohm@0
   440
  | strip_varify  _  = [];
clasohm@0
   441
clasohm@0
   442
fun execute(ss,if_fl,auto_tac,cong_tac,splits,split_consts,net,i) thm = let
clasohm@0
   443
clasohm@0
   444
fun simp_lhs(thm,ss,anet,ats,cs) =
clasohm@0
   445
    if var_lhs(thm,i) then (ss,thm,anet,ats,cs) else
clasohm@0
   446
    if lhs_is_NORM(thm,i) then (ss, res1(thm,trans_norms,i), anet,ats,cs)
clasohm@0
   447
    else case Sequence.pull(tapply(cong_tac i,thm)) of
clasohm@0
   448
	    Some(thm',_) =>
clasohm@0
   449
		    let val ps = prems_of thm and ps' = prems_of thm';
clasohm@0
   450
			val n = length(ps')-length(ps);
clasohm@0
   451
			val a = length(strip_assums_hyp(nth_elem(i-1,ps)))
clasohm@0
   452
			val l = map (fn p => length(strip_assums_hyp(p)))
clasohm@0
   453
				    (take(n,drop(i-1,ps')));
clasohm@0
   454
		    in (simp_refl(rev(l),a,REW::ss),thm',anet,ats,cs) end
clasohm@0
   455
	  | None => (REW::ss,thm,anet,ats,cs);
clasohm@0
   456
clasohm@0
   457
(*NB: the "Adding rewrites:" trace will look strange because assumptions
clasohm@0
   458
      are represented by rules, generalized over their parameters*)
clasohm@0
   459
fun add_asms(ss,thm,a,anet,ats,cs) =
clasohm@0
   460
    let val As = strip_varify(nth_subgoal i thm, a, []);
clasohm@0
   461
	val thms = map (trivial o Sign.cterm_of(#sign(rep_thm(thm))))As;
clasohm@0
   462
	val new_rws = flat(map mk_rew_rules thms);
clasohm@0
   463
	val rwrls = map mk_trans (flat(map mk_rew_rules thms));
clasohm@0
   464
	val anet' = foldr lhs_insert_thm (rwrls,anet)
clasohm@0
   465
    in  if !tracing andalso not(null new_rws)
clasohm@0
   466
	then (writeln"Adding rewrites:";  prths new_rws;  ())
clasohm@0
   467
	else ();
clasohm@0
   468
	(ss,thm,anet',anet::ats,cs) 
clasohm@0
   469
    end;
clasohm@0
   470
clasohm@0
   471
fun rew(seq,thm,ss,anet,ats,cs, more) = case Sequence.pull seq of
clasohm@0
   472
      Some(thm',seq') =>
clasohm@0
   473
	    let val n = (nprems_of thm') - (nprems_of thm)
clasohm@0
   474
	    in pr_rew(i,n,thm,thm',more);
clasohm@0
   475
	       if n=0 then (SIMP_LHS::ss, thm', anet, ats, cs)
clasohm@0
   476
	       else ((replicate n PROVE) @ (POP_CS::SIMP_LHS::ss),
clasohm@0
   477
		     thm', anet, ats, (ss,thm,anet,ats,seq',more)::cs)
clasohm@0
   478
	    end
clasohm@0
   479
    | None => if more
clasohm@0
   480
	    then rew(tapply(lhs_net_tac anet i THEN assume_tac i,thm),
clasohm@0
   481
		     thm,ss,anet,ats,cs,false)
clasohm@0
   482
	    else (ss,thm,anet,ats,cs);
clasohm@0
   483
clasohm@0
   484
fun try_true(thm,ss,anet,ats,cs) =
clasohm@0
   485
    case Sequence.pull(tapply(auto_tac i,thm)) of
clasohm@0
   486
      Some(thm',_) => (ss,thm',anet,ats,cs)
clasohm@0
   487
    | None => let val (ss0,thm0,anet0,ats0,seq,more)::cs0 = cs
clasohm@0
   488
	      in if !tracing
clasohm@0
   489
		 then (writeln"*** Failed to prove precondition. Normal form:";
clasohm@0
   490
		       pr_goal_concl i thm;  writeln"")
clasohm@0
   491
		 else ();
clasohm@0
   492
		 rew(seq,thm0,ss0,anet0,ats0,cs0,more)
clasohm@0
   493
	      end;
clasohm@0
   494
clasohm@0
   495
fun split(thm,ss,anet,ats,cs) =
clasohm@0
   496
	case Sequence.pull(tapply(split_tac
clasohm@0
   497
                                  (cong_tac i,splits,split_consts) i,thm)) of
clasohm@0
   498
		Some(thm',_) => (SIMP_LHS::SPLIT::ss,thm',anet,ats,cs)
clasohm@0
   499
	      | None => (ss,thm,anet,ats,cs);
clasohm@0
   500
clasohm@0
   501
fun step(s::ss, thm, anet, ats, cs) = case s of
clasohm@0
   502
	  MK_EQ => (ss, res1(thm,[red2],i), anet, ats, cs)
clasohm@0
   503
	| ASMS(a) => add_asms(ss,thm,a,anet,ats,cs)
clasohm@0
   504
	| SIMP_LHS => simp_lhs(thm,ss,anet,ats,cs)
clasohm@0
   505
	| REW => rew(tapply(net_tac net i,thm),thm,ss,anet,ats,cs,true)
clasohm@0
   506
	| REFL => (ss, res1(thm,refl_thms,i), anet, ats, cs)
clasohm@0
   507
	| TRUE => try_true(res1(thm,refl_thms,i),ss,anet,ats,cs)
clasohm@0
   508
	| PROVE => (if if_fl then MK_EQ::SIMP_LHS::SPLIT::TRUE::ss
clasohm@0
   509
		    else MK_EQ::SIMP_LHS::TRUE::ss, thm, anet, ats, cs)
clasohm@0
   510
	| POP_ARTR => (ss,thm,hd ats,tl ats,cs)
clasohm@0
   511
	| POP_CS => (ss,thm,anet,ats,tl cs)
clasohm@0
   512
	| SPLIT => split(thm,ss,anet,ats,cs);
clasohm@0
   513
clasohm@0
   514
fun exec(state as (s::ss, thm, _, _, _)) =
clasohm@0
   515
	if s=STOP then thm else exec(step(state));
clasohm@0
   516
clasohm@0
   517
in exec(ss, thm, Net.empty, [], []) end;
clasohm@0
   518
clasohm@0
   519
clasohm@0
   520
(*ss = list of commands (not simpset!); 
clasohm@0
   521
  fl = even use case splits to solve conditional rewrite rules;
clasohm@0
   522
  addhyps = add hyps to simpset*)
clasohm@0
   523
fun EXEC_TAC (ss,fl,addhyps) simpset = METAHYPS 
clasohm@0
   524
 (fn hyps => 
clasohm@0
   525
     case (if addhyps then simpset addrews hyps else simpset) of
clasohm@0
   526
         (SS{auto_tac,cong_net,simp_net,splits,split_consts,...}) =>
clasohm@0
   527
	     PRIMITIVE(execute(ss,fl,auto_tac hyps,
clasohm@0
   528
			       net_tac cong_net,splits,split_consts,
clasohm@0
   529
                               simp_net, 1))
clasohm@0
   530
	     THEN TRY(auto_tac hyps 1));
clasohm@0
   531
clasohm@0
   532
val SIMP_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,SPLIT,REFL,STOP],false,false);
clasohm@0
   533
clasohm@0
   534
val ASM_SIMP_TAC = 
clasohm@0
   535
    EXEC_TAC([ASMS(0),MK_EQ,SIMP_LHS,SPLIT,REFL,STOP],false,true);
clasohm@0
   536
clasohm@0
   537
val SIMP_SPLIT2_TAC = EXEC_TAC([MK_EQ,SIMP_LHS,SPLIT,REFL,STOP],true,false);
clasohm@0
   538
clasohm@0
   539
fun REWRITE (ss,fl) (SS{auto_tac,cong_net,simp_net,splits,split_consts,...}) =
clasohm@0
   540
let val cong_tac = net_tac cong_net
clasohm@0
   541
in fn thm =>
clasohm@0
   542
   let val state = thm RSN (2,red1)
clasohm@0
   543
   in execute(ss,fl,auto_tac[],cong_tac,splits,split_consts,simp_net,1)state
clasohm@0
   544
   end
clasohm@0
   545
end;
clasohm@0
   546
clasohm@0
   547
val SIMP_THM = REWRITE ([ASMS(0),SIMP_LHS,SPLIT,REFL,STOP],false);
clasohm@0
   548
clasohm@0
   549
clasohm@0
   550
(* Compute Congruence rules for individual constants using the substition
clasohm@0
   551
   rules *)
clasohm@0
   552
clasohm@0
   553
val subst_thms = map standard subst_thms;
clasohm@0
   554
clasohm@0
   555
clasohm@0
   556
fun exp_app(0,t) = t
clasohm@0
   557
  | exp_app(i,t) = exp_app(i-1,t $ Bound (i-1));
clasohm@0
   558
clasohm@0
   559
fun exp_abs(Type("fun",[T1,T2]),t,i) =
clasohm@0
   560
	Abs("x"^string_of_int i,T1,exp_abs(T2,t,i+1))
clasohm@0
   561
  | exp_abs(T,t,i) = exp_app(i,t);
clasohm@0
   562
clasohm@0
   563
fun eta_Var(ixn,T) = exp_abs(T,Var(ixn,T),0);
clasohm@0
   564
clasohm@0
   565
clasohm@0
   566
fun Pinst(f,fT,(eq,eqT),k,i,T,yik,Ts) =
clasohm@0
   567
let fun xn_list(x,n) =
clasohm@0
   568
	let val ixs = map (fn i => (x^(radixstring(26,"a",i)),0)) (0 upto n);
clasohm@0
   569
	in map eta_Var (ixs ~~ (take(n+1,Ts))) end
clasohm@0
   570
    val lhs = list_comb(f,xn_list("X",k-1))
clasohm@0
   571
    val rhs = list_comb(f,xn_list("X",i-1) @ [Bound 0] @ yik)
clasohm@0
   572
in Abs("", T, Const(eq,[fT,fT]--->eqT) $ lhs $ rhs) end;
clasohm@0
   573
clasohm@0
   574
fun find_subst tsig T =
clasohm@0
   575
let fun find (thm::thms) =
clasohm@0
   576
	let val (Const(_,cT), va, vb) =	dest_red(hd(prems_of thm));
clasohm@0
   577
	    val [P] = term_vars(concl_of thm) \\ [va,vb]
clasohm@0
   578
	    val eqT::_ = binder_types cT
clasohm@0
   579
        in if Type.typ_instance(tsig,T,eqT) then Some(thm,va,vb,P)
clasohm@0
   580
	   else find thms
clasohm@0
   581
	end
clasohm@0
   582
      | find [] = None
clasohm@0
   583
in find subst_thms end;
clasohm@0
   584
clasohm@0
   585
fun mk_cong sg (f,aTs,rT) (refl,eq) =
clasohm@0
   586
let val tsig = #tsig(Sign.rep_sg sg);
clasohm@0
   587
    val k = length aTs;
clasohm@0
   588
    fun ri((subst,va as Var(_,Ta),vb as Var(_,Tb),P),i,si,T,yik) =
clasohm@0
   589
	let val ca = Sign.cterm_of sg va
clasohm@0
   590
	    and cx = Sign.cterm_of sg (eta_Var(("X"^si,0),T))
clasohm@0
   591
	    val cb = Sign.cterm_of sg vb
clasohm@0
   592
	    and cy = Sign.cterm_of sg (eta_Var(("Y"^si,0),T))
clasohm@0
   593
	    val cP = Sign.cterm_of sg P
clasohm@0
   594
	    and cp = Sign.cterm_of sg (Pinst(f,rT,eq,k,i,T,yik,aTs))
clasohm@0
   595
	in cterm_instantiate [(ca,cx),(cb,cy),(cP,cp)] subst end;
clasohm@0
   596
    fun mk(c,T::Ts,i,yik) =
clasohm@0
   597
	let val si = radixstring(26,"a",i)
clasohm@0
   598
	in case find_subst tsig T of
clasohm@0
   599
	     None => mk(c,Ts,i-1,eta_Var(("X"^si,0),T)::yik)
clasohm@0
   600
	   | Some s => let val c' = c RSN (2,ri(s,i,si,T,yik))
clasohm@0
   601
		       in mk(c',Ts,i-1,eta_Var(("Y"^si,0),T)::yik) end
clasohm@0
   602
	end
clasohm@0
   603
      | mk(c,[],_,_) = c;
clasohm@0
   604
in mk(refl,rev aTs,k-1,[]) end;
clasohm@0
   605
clasohm@0
   606
fun mk_cong_type sg (f,T) =
clasohm@0
   607
let val (aTs,rT) = strip_type T;
clasohm@0
   608
    val tsig = #tsig(Sign.rep_sg sg);
clasohm@0
   609
    fun find_refl(r::rs) =
clasohm@0
   610
	let val (Const(eq,eqT),_,_) = dest_red(concl_of r)
clasohm@0
   611
	in if Type.typ_instance(tsig, rT, hd(binder_types eqT))
clasohm@0
   612
	   then Some(r,(eq,body_type eqT)) else find_refl rs
clasohm@0
   613
	end
clasohm@0
   614
      | find_refl([]) = None;
clasohm@0
   615
in case find_refl refl_thms of
clasohm@0
   616
     None => []  |  Some(refl) => [mk_cong sg (f,aTs,rT) refl]
clasohm@0
   617
end;
clasohm@0
   618
clasohm@0
   619
fun mk_cong_thy thy f =
clasohm@0
   620
let val sg = sign_of thy;
clasohm@0
   621
    val T = case Sign.Symtab.lookup(#const_tab(Sign.rep_sg sg),f) of
clasohm@0
   622
		None => error(f^" not declared") | Some(T) => T;
clasohm@0
   623
    val T' = incr_tvar 9 T;
clasohm@0
   624
in mk_cong_type sg (Const(f,T'),T') end;
clasohm@0
   625
clasohm@0
   626
fun mk_congs thy = filter_out is_fact o flat o map (mk_cong_thy thy);
clasohm@0
   627
clasohm@0
   628
fun mk_typed_congs thy =
clasohm@0
   629
let val sg = sign_of thy;
clasohm@0
   630
    val S0 = Type.defaultS(#tsig(Sign.rep_sg sg))
clasohm@0
   631
    fun readfT(f,s) =
clasohm@0
   632
	let val T = incr_tvar 9 (Sign.read_typ(sg,K(Some(S0))) s);
clasohm@0
   633
	    val t = case Sign.Symtab.lookup(#const_tab(Sign.rep_sg sg),f) of
clasohm@0
   634
		      Some(_) => Const(f,T) | None => Free(f,T)
clasohm@0
   635
	in (t,T) end
clasohm@0
   636
in flat o map (mk_cong_type sg o readfT) end;
clasohm@0
   637
clasohm@0
   638
(* This code is fishy, esp the "let val T' = ..." 
clasohm@0
   639
fun extract_free_congs() =
clasohm@0
   640
let val {prop,sign,...} = rep_thm(topthm());
clasohm@0
   641
    val frees = add_term_frees(prop,[]);
clasohm@0
   642
    fun filter(Free(a,T as Type("fun",_))) =
clasohm@0
   643
	  let val T' = incr_tvar 9 (Type.varifyT T)
clasohm@0
   644
	  in [(Free(a,T),T)] end
clasohm@0
   645
      | filter _ = []
clasohm@0
   646
in flat(map (mk_cong_type sign) (flat (map filter frees))) end;
clasohm@0
   647
*)
clasohm@0
   648
clasohm@0
   649
end (* local *)
clasohm@0
   650
end (* SIMP *);