src/Pure/logic.ML
author wenzelm
Thu Jul 08 18:31:04 1999 +0200 (1999-07-08)
changeset 6930 4b40fb299f9f
parent 5041 a1d0a6d555cd
child 9460 53d7ad5bec39
permissions -rw-r--r--
improved error msgs of cterm_instantiate;
fixed incr_indexes;
clasohm@1460
     1
(*  Title: 	Pure/logic.ML
clasohm@0
     2
    ID:         $Id$
clasohm@1460
     3
    Author: 	Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   Cambridge University 1992
clasohm@0
     5
clasohm@0
     6
Supporting code for defining the abstract type "thm"
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
infix occs;
clasohm@0
    10
clasohm@0
    11
signature LOGIC = 
wenzelm@4345
    12
sig
clasohm@1460
    13
  val assum_pairs	: term -> (term*term)list
clasohm@1460
    14
  val auto_rename	: bool ref   
clasohm@1460
    15
  val close_form	: term -> term   
clasohm@1460
    16
  val count_prems	: term * int -> int
clasohm@1460
    17
  val dest_equals	: term -> term * term
clasohm@1460
    18
  val dest_flexpair	: term -> term * term
clasohm@1460
    19
  val dest_implies	: term -> term * term
clasohm@1460
    20
  val dest_inclass	: term -> typ * class
clasohm@1460
    21
  val dest_type		: term -> typ
clasohm@1460
    22
  val flatten_params	: int -> term -> term
clasohm@1460
    23
  val incr_indexes	: typ list * int -> term -> term
nipkow@5041
    24
  val is_all            : term -> bool
nipkow@3963
    25
  val is_equals         : term -> bool
nipkow@5041
    26
  val is_implies        : term -> bool
clasohm@1460
    27
  val lift_fns		: term * int -> (term -> term) * (term -> term)
clasohm@1460
    28
  val list_flexpairs	: (term*term)list * term -> term
clasohm@1460
    29
  val list_implies	: term list * term -> term
clasohm@0
    30
  val list_rename_params: string list * term -> term
wenzelm@4822
    31
  val mk_cond_defpair	: term list -> term * term -> string * term
wenzelm@4822
    32
  val mk_defpair	: term * term -> string * term
clasohm@1460
    33
  val mk_equals		: term * term -> term
clasohm@1460
    34
  val mk_flexpair	: term * term -> term
clasohm@1460
    35
  val mk_implies	: term * term -> term
clasohm@1460
    36
  val mk_inclass	: typ * class -> term
clasohm@1460
    37
  val mk_type		: typ -> term
clasohm@1460
    38
  val occs		: term * term -> bool
clasohm@1460
    39
  val rule_of		: (term*term)list * term list * term -> term
clasohm@1460
    40
  val set_rename_prefix	: string -> unit   
clasohm@1460
    41
  val skip_flexpairs	: term -> term
clasohm@0
    42
  val strip_assums_concl: term -> term
clasohm@1460
    43
  val strip_assums_hyp	: term -> term list
clasohm@1460
    44
  val strip_flexpairs	: term -> (term*term)list * term
clasohm@1460
    45
  val strip_horn	: term -> (term*term)list * term list * term
clasohm@1460
    46
  val strip_imp_concl	: term -> term
clasohm@1460
    47
  val strip_imp_prems	: term -> term list
clasohm@1460
    48
  val strip_params	: term -> (string * typ) list
clasohm@1460
    49
  val strip_prems	: int * term list * term -> term list * term
wenzelm@2508
    50
  val unvarify		: term -> term
wenzelm@2508
    51
  val varify		: term -> term
wenzelm@4345
    52
end;
clasohm@0
    53
paulson@1500
    54
structure Logic : LOGIC =
clasohm@0
    55
struct
wenzelm@398
    56
wenzelm@4345
    57
clasohm@0
    58
(*** Abstract syntax operations on the meta-connectives ***)
clasohm@0
    59
nipkow@5041
    60
(** all **)
nipkow@5041
    61
nipkow@5041
    62
fun is_all (Const ("all", _) $ _) = true
nipkow@5041
    63
  | is_all _ = false;
nipkow@5041
    64
nipkow@5041
    65
clasohm@0
    66
(** equality **)
clasohm@0
    67
paulson@1835
    68
(*Make an equality.  DOES NOT CHECK TYPE OF u*)
lcp@64
    69
fun mk_equals(t,u) = equals(fastype_of t) $ t $ u;
clasohm@0
    70
clasohm@0
    71
fun dest_equals (Const("==",_) $ t $ u)  =  (t,u)
clasohm@0
    72
  | dest_equals t = raise TERM("dest_equals", [t]);
clasohm@0
    73
wenzelm@637
    74
fun is_equals (Const ("==", _) $ _ $ _) = true
wenzelm@637
    75
  | is_equals _ = false;
wenzelm@637
    76
wenzelm@637
    77
clasohm@0
    78
(** implies **)
clasohm@0
    79
clasohm@0
    80
fun mk_implies(A,B) = implies $ A $ B;
clasohm@0
    81
clasohm@0
    82
fun dest_implies (Const("==>",_) $ A $ B)  =  (A,B)
clasohm@0
    83
  | dest_implies A = raise TERM("dest_implies", [A]);
clasohm@0
    84
nipkow@5041
    85
fun is_implies (Const ("==>", _) $ _ $ _) = true
nipkow@5041
    86
  | is_implies _ = false;
nipkow@5041
    87
wenzelm@4822
    88
clasohm@0
    89
(** nested implications **)
clasohm@0
    90
clasohm@0
    91
(* [A1,...,An], B  goes to  A1==>...An==>B  *)
clasohm@0
    92
fun list_implies ([], B) = B : term
clasohm@0
    93
  | list_implies (A::AS, B) = implies $ A $ list_implies(AS,B);
clasohm@0
    94
clasohm@0
    95
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
clasohm@0
    96
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
clasohm@0
    97
  | strip_imp_prems _ = [];
clasohm@0
    98
clasohm@0
    99
(* A1==>...An==>B  goes to B, where B is not an implication *)
clasohm@0
   100
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
clasohm@0
   101
  | strip_imp_concl A = A : term;
clasohm@0
   102
clasohm@0
   103
(*Strip and return premises: (i, [], A1==>...Ai==>B)
clasohm@1460
   104
    goes to   ([Ai, A(i-1),...,A1] , B) 	(REVERSED) 
clasohm@0
   105
  if  i<0 or else i too big then raises  TERM*)
clasohm@0
   106
fun strip_prems (0, As, B) = (As, B) 
clasohm@0
   107
  | strip_prems (i, As, Const("==>", _) $ A $ B) = 
clasohm@1460
   108
	strip_prems (i-1, A::As, B)
clasohm@0
   109
  | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
clasohm@0
   110
clasohm@0
   111
(*Count premises -- quicker than (length ostrip_prems) *)
clasohm@0
   112
fun count_prems (Const("==>", _) $ A $ B, n) = count_prems (B,n+1)
clasohm@0
   113
  | count_prems (_,n) = n;
clasohm@0
   114
wenzelm@4822
   115
clasohm@0
   116
(** flex-flex constraints **)
clasohm@0
   117
lcp@64
   118
(*Make a constraint.*)
lcp@64
   119
fun mk_flexpair(t,u) = flexpair(fastype_of t) $ t $ u;
clasohm@0
   120
clasohm@0
   121
fun dest_flexpair (Const("=?=",_) $ t $ u)  =  (t,u)
clasohm@0
   122
  | dest_flexpair t = raise TERM("dest_flexpair", [t]);
clasohm@0
   123
clasohm@0
   124
(*make flexflex antecedents: ( [(a1,b1),...,(an,bn)] , C )
clasohm@0
   125
    goes to (a1=?=b1) ==>...(an=?=bn)==>C *)
clasohm@0
   126
fun list_flexpairs ([], A) = A
clasohm@0
   127
  | list_flexpairs ((t,u)::pairs, A) =
clasohm@1460
   128
	implies $ (mk_flexpair(t,u)) $ list_flexpairs(pairs,A);
clasohm@0
   129
clasohm@0
   130
(*Make the object-rule tpairs==>As==>B   *)
clasohm@0
   131
fun rule_of (tpairs, As, B) = list_flexpairs(tpairs, list_implies(As, B));
clasohm@0
   132
clasohm@0
   133
(*Remove and return flexflex pairs: 
clasohm@1460
   134
    (a1=?=b1)==>...(an=?=bn)==>C  to  ( [(a1,b1),...,(an,bn)] , C )	
clasohm@0
   135
  [Tail recursive in order to return a pair of results] *)
clasohm@0
   136
fun strip_flex_aux (pairs, Const("==>", _) $ (Const("=?=",_)$t$u) $ C) =
clasohm@0
   137
        strip_flex_aux ((t,u)::pairs, C)
clasohm@0
   138
  | strip_flex_aux (pairs,C) = (rev pairs, C);
clasohm@0
   139
clasohm@0
   140
fun strip_flexpairs A = strip_flex_aux([], A);
clasohm@0
   141
clasohm@0
   142
(*Discard flexflex pairs*)
clasohm@0
   143
fun skip_flexpairs (Const("==>", _) $ (Const("=?=",_)$_$_) $ C) =
clasohm@1460
   144
	skip_flexpairs C
clasohm@0
   145
  | skip_flexpairs C = C;
clasohm@0
   146
clasohm@0
   147
(*strip a proof state (Horn clause): 
clasohm@0
   148
   (a1==b1)==>...(am==bm)==>B1==>...Bn==>C
clasohm@0
   149
    goes to   ( [(a1,b1),...,(am,bm)] , [B1,...,Bn] , C)    *)
clasohm@0
   150
fun strip_horn A =
clasohm@0
   151
  let val (tpairs,horn) = strip_flexpairs A 
clasohm@0
   152
  in  (tpairs, strip_imp_prems horn, strip_imp_concl horn)   end;
clasohm@0
   153
wenzelm@4822
   154
wenzelm@4822
   155
(** definitions **)
wenzelm@4822
   156
wenzelm@4822
   157
fun mk_cond_defpair As (lhs, rhs) =
wenzelm@4822
   158
  (case Term.head_of lhs of
wenzelm@4822
   159
    Const (name, _) =>
wenzelm@4822
   160
      (Sign.base_name name ^ "_def", list_implies (As, mk_equals (lhs, rhs)))
wenzelm@4822
   161
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
wenzelm@4822
   162
wenzelm@4822
   163
fun mk_defpair lhs_rhs = mk_cond_defpair [] lhs_rhs;
wenzelm@4822
   164
wenzelm@4822
   165
wenzelm@398
   166
(** types as terms **)
wenzelm@398
   167
wenzelm@398
   168
fun mk_type ty = Const ("TYPE", itselfT ty);
wenzelm@398
   169
wenzelm@398
   170
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
wenzelm@398
   171
  | dest_type t = raise TERM ("dest_type", [t]);
wenzelm@398
   172
wenzelm@4822
   173
wenzelm@447
   174
(** class constraints **)
wenzelm@398
   175
wenzelm@398
   176
fun mk_inclass (ty, c) =
wenzelm@398
   177
  Const (Sign.const_of_class c, itselfT ty --> propT) $ mk_type ty;
wenzelm@398
   178
wenzelm@398
   179
fun dest_inclass (t as Const (c_class, _) $ ty) =
wenzelm@398
   180
      ((dest_type ty, Sign.class_of_const c_class)
wenzelm@398
   181
        handle TERM _ => raise TERM ("dest_inclass", [t]))
wenzelm@398
   182
  | dest_inclass t = raise TERM ("dest_inclass", [t]);
wenzelm@398
   183
clasohm@0
   184
clasohm@0
   185
(*** Low-level term operations ***)
clasohm@0
   186
clasohm@0
   187
(*Does t occur in u?  Or is alpha-convertible to u?
clasohm@0
   188
  The term t must contain no loose bound variables*)
nipkow@4631
   189
fun t occs u = exists_subterm (fn s => t aconv s) u;
clasohm@0
   190
clasohm@0
   191
(*Close up a formula over all free variables by quantification*)
clasohm@0
   192
fun close_form A =
wenzelm@4443
   193
  list_all_free (sort_wrt fst (map dest_Free (term_frees A)), A);
clasohm@0
   194
clasohm@0
   195
clasohm@0
   196
(*** Specialized operations for resolution... ***)
clasohm@0
   197
clasohm@0
   198
(*For all variables in the term, increment indexnames and lift over the Us
clasohm@0
   199
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
clasohm@0
   200
fun incr_indexes (Us: typ list, inc:int) t = 
clasohm@0
   201
  let fun incr (Var ((a,i), T), lev) = 
clasohm@1460
   202
		Unify.combound (Var((a, i+inc), Us---> incr_tvar inc T),
clasohm@1460
   203
				lev, length Us)
clasohm@1460
   204
	| incr (Abs (a,T,body), lev) =
clasohm@1460
   205
		Abs (a, incr_tvar inc T, incr(body,lev+1))
clasohm@1460
   206
	| incr (Const(a,T),_) = Const(a, incr_tvar inc T)
clasohm@1460
   207
	| incr (Free(a,T),_) = Free(a, incr_tvar inc T)
clasohm@1460
   208
	| incr (f$t, lev) = incr(f,lev) $ incr(t,lev)
clasohm@1460
   209
	| incr (t,lev) = t
clasohm@0
   210
  in  incr(t,0)  end;
clasohm@0
   211
clasohm@0
   212
(*Make lifting functions from subgoal and increment.
clasohm@0
   213
    lift_abs operates on tpairs (unification constraints)
clasohm@0
   214
    lift_all operates on propositions     *)
clasohm@0
   215
fun lift_fns (B,inc) =
clasohm@0
   216
  let fun lift_abs (Us, Const("==>", _) $ _ $ B) u = lift_abs (Us,B) u
clasohm@1460
   217
	| lift_abs (Us, Const("all",_)$Abs(a,T,t)) u =
clasohm@1460
   218
	      Abs(a, T, lift_abs (T::Us, t) u)
clasohm@1460
   219
	| lift_abs (Us, _) u = incr_indexes(rev Us, inc) u
clasohm@0
   220
      fun lift_all (Us, Const("==>", _) $ A $ B) u =
clasohm@1460
   221
	      implies $ A $ lift_all (Us,B) u
clasohm@1460
   222
	| lift_all (Us, Const("all",_)$Abs(a,T,t)) u = 
clasohm@1460
   223
	      all T $ Abs(a, T, lift_all (T::Us,t) u)
clasohm@1460
   224
	| lift_all (Us, _) u = incr_indexes(rev Us, inc) u;
clasohm@0
   225
  in  (lift_abs([],B), lift_all([],B))  end;
clasohm@0
   226
clasohm@0
   227
(*Strips assumptions in goal, yielding list of hypotheses.   *)
clasohm@0
   228
fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B
clasohm@0
   229
  | strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t
clasohm@0
   230
  | strip_assums_hyp B = [];
clasohm@0
   231
clasohm@0
   232
(*Strips assumptions in goal, yielding conclusion.   *)
clasohm@0
   233
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
clasohm@0
   234
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
clasohm@0
   235
  | strip_assums_concl B = B;
clasohm@0
   236
clasohm@0
   237
(*Make a list of all the parameters in a subgoal, even if nested*)
clasohm@0
   238
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
clasohm@0
   239
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
clasohm@0
   240
  | strip_params B = [];
clasohm@0
   241
clasohm@0
   242
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
clasohm@0
   243
    where j is the total number of parameters (precomputed) 
clasohm@0
   244
  If n>0 then deletes assumption n. *)
clasohm@0
   245
fun remove_params j n A = 
clasohm@0
   246
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
clasohm@0
   247
    else case A of
clasohm@0
   248
        Const("==>", _) $ H $ B => 
clasohm@1460
   249
	  if n=1 then                           (remove_params j (n-1) B)
clasohm@1460
   250
	  else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
clasohm@0
   251
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
clasohm@0
   252
      | _ => if n>0 then raise TERM("remove_params", [A])
clasohm@0
   253
             else A;
clasohm@0
   254
clasohm@0
   255
(** Auto-renaming of parameters in subgoals **)
clasohm@0
   256
clasohm@0
   257
val auto_rename = ref false
clasohm@0
   258
and rename_prefix = ref "ka";
clasohm@0
   259
clasohm@0
   260
(*rename_prefix is not exported; it is set by this function.*)
clasohm@0
   261
fun set_rename_prefix a =
wenzelm@4693
   262
    if a<>"" andalso forall Symbol.is_letter (Symbol.explode a)
clasohm@0
   263
    then  (rename_prefix := a;  auto_rename := true)
clasohm@0
   264
    else  error"rename prefix must be nonempty and consist of letters";
clasohm@0
   265
clasohm@0
   266
(*Makes parameters in a goal have distinctive names (not guaranteed unique!)
clasohm@0
   267
  A name clash could cause the printer to rename bound vars;
clasohm@0
   268
    then res_inst_tac would not work properly.*)
clasohm@0
   269
fun rename_vars (a, []) = []
clasohm@0
   270
  | rename_vars (a, (_,T)::vars) =
clasohm@0
   271
        (a,T) :: rename_vars (bump_string a, vars);
clasohm@0
   272
clasohm@0
   273
(*Move all parameters to the front of the subgoal, renaming them apart;
clasohm@0
   274
  if n>0 then deletes assumption n. *)
clasohm@0
   275
fun flatten_params n A =
clasohm@0
   276
    let val params = strip_params A;
clasohm@1460
   277
	val vars = if !auto_rename 
clasohm@1460
   278
		   then rename_vars (!rename_prefix, params)
paulson@2266
   279
		   else ListPair.zip (variantlist(map #1 params,[]),
paulson@2266
   280
				      map #2 params)
clasohm@0
   281
    in  list_all (vars, remove_params (length vars) n A)
clasohm@0
   282
    end;
clasohm@0
   283
clasohm@0
   284
(*Makes parameters in a goal have the names supplied by the list cs.*)
clasohm@0
   285
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
clasohm@0
   286
      implies $ A $ list_rename_params (cs, B)
clasohm@0
   287
  | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) = 
clasohm@0
   288
      all T $ Abs(c, T, list_rename_params (cs, t))
clasohm@0
   289
  | list_rename_params (cs, B) = B;
clasohm@0
   290
clasohm@0
   291
(*Strips assumptions in goal yielding  ( [Hn,...,H1], [xm,...,x1], B )
clasohm@0
   292
  where H1,...,Hn are the hypotheses and x1...xm are the parameters.   *)
clasohm@0
   293
fun strip_assums_aux (Hs, params, Const("==>", _) $ H $ B) = 
clasohm@1460
   294
	strip_assums_aux (H::Hs, params, B)
clasohm@0
   295
  | strip_assums_aux (Hs, params, Const("all",_)$Abs(a,T,t)) =
clasohm@1460
   296
	strip_assums_aux (Hs, (a,T)::params, t)
clasohm@0
   297
  | strip_assums_aux (Hs, params, B) = (Hs, params, B);
clasohm@0
   298
clasohm@0
   299
fun strip_assums A = strip_assums_aux ([],[],A);
clasohm@0
   300
clasohm@0
   301
clasohm@0
   302
(*Produces disagreement pairs, one for each assumption proof, in order.
clasohm@0
   303
  A is the first premise of the lifted rule, and thus has the form
clasohm@0
   304
    H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B) *)
clasohm@0
   305
fun assum_pairs A =
clasohm@0
   306
  let val (Hs, params, B) = strip_assums A
clasohm@0
   307
      val D = Unify.rlist_abs(params, B)
clasohm@0
   308
      fun pairrev ([],pairs) = pairs  
clasohm@0
   309
        | pairrev (H::Hs,pairs) = 
clasohm@1460
   310
	    pairrev(Hs, (Unify.rlist_abs(params,H), D) :: pairs)
clasohm@0
   311
  in  pairrev (Hs,[])   (*WAS:  map pair (rev Hs)  *)
clasohm@0
   312
  end;
clasohm@0
   313
clasohm@0
   314
clasohm@0
   315
(*Converts Frees to Vars and TFrees to TVars so that axioms can be written
clasohm@0
   316
  without (?) everywhere*)
clasohm@0
   317
fun varify (Const(a,T)) = Const(a, Type.varifyT T)
clasohm@0
   318
  | varify (Free(a,T)) = Var((a,0), Type.varifyT T)
clasohm@0
   319
  | varify (Var(ixn,T)) = Var(ixn, Type.varifyT T)
clasohm@0
   320
  | varify (Abs (a,T,body)) = Abs (a, Type.varifyT T, varify body)
clasohm@0
   321
  | varify (f$t) = varify f $ varify t
clasohm@0
   322
  | varify t = t;
clasohm@0
   323
lcp@546
   324
(*Inverse of varify.  Converts axioms back to their original form.*)
lcp@585
   325
fun unvarify (Const(a,T))    = Const(a, Type.unvarifyT T)
lcp@585
   326
  | unvarify (Var((a,0), T)) = Free(a, Type.unvarifyT T)
lcp@585
   327
  | unvarify (Var(ixn,T))    = Var(ixn, Type.unvarifyT T)  (*non-0 index!*)
lcp@585
   328
  | unvarify (Abs (a,T,body)) = Abs (a, Type.unvarifyT T, unvarify body)
lcp@546
   329
  | unvarify (f$t) = unvarify f $ unvarify t
lcp@546
   330
  | unvarify t = t;
lcp@546
   331
clasohm@0
   332
end;