src/Pure/logic.ML
author berghofe
Mon Oct 21 17:07:27 2002 +0200 (2002-10-21)
changeset 13659 3cf622f6b0b2
parent 12902 a23dc0b7566f
child 13799 77614fe09362
permissions -rw-r--r--
Removed obsolete functions dealing with flex-flex constraints.
wenzelm@9460
     1
(*  Title:      Pure/logic.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@9460
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   Cambridge University 1992
clasohm@0
     5
wenzelm@9460
     6
Abstract syntax operations of the Pure meta-logic.
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
infix occs;
clasohm@0
    10
wenzelm@9460
    11
signature LOGIC =
wenzelm@4345
    12
sig
nipkow@5041
    13
  val is_all            : term -> bool
wenzelm@9460
    14
  val mk_equals         : term * term -> term
wenzelm@9460
    15
  val dest_equals       : term -> term * term
nipkow@3963
    16
  val is_equals         : term -> bool
wenzelm@9460
    17
  val mk_implies        : term * term -> term
wenzelm@9460
    18
  val dest_implies      : term -> term * term
nipkow@5041
    19
  val is_implies        : term -> bool
wenzelm@9460
    20
  val list_implies      : term list * term -> term
wenzelm@9460
    21
  val strip_imp_prems   : term -> term list
wenzelm@9460
    22
  val strip_imp_concl   : term -> term
wenzelm@9460
    23
  val strip_prems       : int * term list * term -> term list * term
wenzelm@9460
    24
  val count_prems       : term * int -> int
wenzelm@12137
    25
  val mk_conjunction    : term * term -> term
wenzelm@12757
    26
  val mk_conjunction_list: term list -> term
berghofe@13659
    27
  val strip_horn        : term -> term list * term
wenzelm@9460
    28
  val mk_cond_defpair   : term list -> term * term -> string * term
wenzelm@9460
    29
  val mk_defpair        : term * term -> string * term
wenzelm@9460
    30
  val mk_type           : typ -> term
wenzelm@9460
    31
  val dest_type         : term -> typ
wenzelm@9460
    32
  val mk_inclass        : typ * class -> term
wenzelm@9460
    33
  val dest_inclass      : term -> typ * class
wenzelm@9460
    34
  val goal_const        : term
wenzelm@9460
    35
  val mk_goal           : term -> term
wenzelm@9460
    36
  val dest_goal         : term -> term
wenzelm@9460
    37
  val occs              : term * term -> bool
wenzelm@9460
    38
  val close_form        : term -> term
wenzelm@9460
    39
  val incr_indexes      : typ list * int -> term -> term
wenzelm@9460
    40
  val lift_fns          : term * int -> (term -> term) * (term -> term)
wenzelm@9460
    41
  val strip_assums_hyp  : term -> term list
wenzelm@9460
    42
  val strip_assums_concl: term -> term
wenzelm@9460
    43
  val strip_params      : term -> (string * typ) list
wenzelm@9667
    44
  val has_meta_prems    : term -> int -> bool
wenzelm@9460
    45
  val flatten_params    : int -> term -> term
wenzelm@9460
    46
  val auto_rename       : bool ref
wenzelm@9460
    47
  val set_rename_prefix : string -> unit
clasohm@0
    48
  val list_rename_params: string list * term -> term
wenzelm@9460
    49
  val assum_pairs       : term -> (term*term)list
wenzelm@9460
    50
  val varify            : term -> term
wenzelm@9460
    51
  val unvarify          : 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)
wenzelm@9460
   104
    goes to   ([Ai, A(i-1),...,A1] , B)         (REVERSED)
clasohm@0
   105
  if  i<0 or else i too big then raises  TERM*)
wenzelm@9460
   106
fun strip_prems (0, As, B) = (As, B)
wenzelm@9460
   107
  | strip_prems (i, As, Const("==>", _) $ A $ B) =
wenzelm@9460
   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
berghofe@13659
   115
(*strip a proof state (Horn clause):
berghofe@13659
   116
  B1 ==> ... Bn ==> C   goes to   ([B1, ..., Bn], C)    *)
berghofe@13659
   117
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
berghofe@13659
   118
wenzelm@4822
   119
wenzelm@12137
   120
(** conjunction **)
wenzelm@12137
   121
wenzelm@12137
   122
fun mk_conjunction (t, u) =
wenzelm@12137
   123
  Term.list_all ([("C", propT)], mk_implies (list_implies ([t, u], Bound 0), Bound 0));
wenzelm@12137
   124
wenzelm@12757
   125
fun mk_conjunction_list [] = Term.all propT $ Abs ("dummy", propT, mk_implies (Bound 0, Bound 0))
wenzelm@12757
   126
  | mk_conjunction_list ts = foldr1 mk_conjunction ts;
wenzelm@12137
   127
wenzelm@12137
   128
wenzelm@4822
   129
(** definitions **)
wenzelm@4822
   130
wenzelm@4822
   131
fun mk_cond_defpair As (lhs, rhs) =
wenzelm@4822
   132
  (case Term.head_of lhs of
wenzelm@4822
   133
    Const (name, _) =>
wenzelm@4822
   134
      (Sign.base_name name ^ "_def", list_implies (As, mk_equals (lhs, rhs)))
wenzelm@4822
   135
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
wenzelm@4822
   136
wenzelm@4822
   137
fun mk_defpair lhs_rhs = mk_cond_defpair [] lhs_rhs;
wenzelm@4822
   138
wenzelm@4822
   139
wenzelm@398
   140
(** types as terms **)
wenzelm@398
   141
wenzelm@398
   142
fun mk_type ty = Const ("TYPE", itselfT ty);
wenzelm@398
   143
wenzelm@398
   144
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
wenzelm@398
   145
  | dest_type t = raise TERM ("dest_type", [t]);
wenzelm@398
   146
wenzelm@4822
   147
wenzelm@447
   148
(** class constraints **)
wenzelm@398
   149
wenzelm@398
   150
fun mk_inclass (ty, c) =
wenzelm@398
   151
  Const (Sign.const_of_class c, itselfT ty --> propT) $ mk_type ty;
wenzelm@398
   152
wenzelm@398
   153
fun dest_inclass (t as Const (c_class, _) $ ty) =
wenzelm@398
   154
      ((dest_type ty, Sign.class_of_const c_class)
wenzelm@398
   155
        handle TERM _ => raise TERM ("dest_inclass", [t]))
wenzelm@398
   156
  | dest_inclass t = raise TERM ("dest_inclass", [t]);
wenzelm@398
   157
clasohm@0
   158
wenzelm@9460
   159
(** atomic goals **)
wenzelm@9460
   160
wenzelm@9460
   161
val goal_const = Const ("Goal", propT --> propT);
wenzelm@9460
   162
fun mk_goal t = goal_const $ t;
wenzelm@9460
   163
wenzelm@9460
   164
fun dest_goal (Const ("Goal", _) $ t) = t
wenzelm@9460
   165
  | dest_goal t = raise TERM ("dest_goal", [t]);
wenzelm@9460
   166
wenzelm@9460
   167
clasohm@0
   168
(*** Low-level term operations ***)
clasohm@0
   169
clasohm@0
   170
(*Does t occur in u?  Or is alpha-convertible to u?
clasohm@0
   171
  The term t must contain no loose bound variables*)
nipkow@4631
   172
fun t occs u = exists_subterm (fn s => t aconv s) u;
clasohm@0
   173
clasohm@0
   174
(*Close up a formula over all free variables by quantification*)
clasohm@0
   175
fun close_form A =
wenzelm@4443
   176
  list_all_free (sort_wrt fst (map dest_Free (term_frees A)), A);
clasohm@0
   177
clasohm@0
   178
clasohm@0
   179
(*** Specialized operations for resolution... ***)
clasohm@0
   180
clasohm@0
   181
(*For all variables in the term, increment indexnames and lift over the Us
clasohm@0
   182
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
wenzelm@9460
   183
fun incr_indexes (Us: typ list, inc:int) t =
wenzelm@9460
   184
  let fun incr (Var ((a,i), T), lev) =
wenzelm@9460
   185
                Unify.combound (Var((a, i+inc), Us---> incr_tvar inc T),
wenzelm@9460
   186
                                lev, length Us)
wenzelm@9460
   187
        | incr (Abs (a,T,body), lev) =
wenzelm@9460
   188
                Abs (a, incr_tvar inc T, incr(body,lev+1))
wenzelm@9460
   189
        | incr (Const(a,T),_) = Const(a, incr_tvar inc T)
wenzelm@9460
   190
        | incr (Free(a,T),_) = Free(a, incr_tvar inc T)
wenzelm@9460
   191
        | incr (f$t, lev) = incr(f,lev) $ incr(t,lev)
wenzelm@9460
   192
        | incr (t,lev) = t
clasohm@0
   193
  in  incr(t,0)  end;
clasohm@0
   194
clasohm@0
   195
(*Make lifting functions from subgoal and increment.
clasohm@0
   196
    lift_abs operates on tpairs (unification constraints)
clasohm@0
   197
    lift_all operates on propositions     *)
clasohm@0
   198
fun lift_fns (B,inc) =
clasohm@0
   199
  let fun lift_abs (Us, Const("==>", _) $ _ $ B) u = lift_abs (Us,B) u
wenzelm@9460
   200
        | lift_abs (Us, Const("all",_)$Abs(a,T,t)) u =
wenzelm@9460
   201
              Abs(a, T, lift_abs (T::Us, t) u)
wenzelm@9460
   202
        | lift_abs (Us, _) u = incr_indexes(rev Us, inc) u
clasohm@0
   203
      fun lift_all (Us, Const("==>", _) $ A $ B) u =
wenzelm@9460
   204
              implies $ A $ lift_all (Us,B) u
wenzelm@9460
   205
        | lift_all (Us, Const("all",_)$Abs(a,T,t)) u =
wenzelm@9460
   206
              all T $ Abs(a, T, lift_all (T::Us,t) u)
wenzelm@9460
   207
        | lift_all (Us, _) u = incr_indexes(rev Us, inc) u;
clasohm@0
   208
  in  (lift_abs([],B), lift_all([],B))  end;
clasohm@0
   209
clasohm@0
   210
(*Strips assumptions in goal, yielding list of hypotheses.   *)
clasohm@0
   211
fun strip_assums_hyp (Const("==>", _) $ H $ B) = H :: strip_assums_hyp B
clasohm@0
   212
  | strip_assums_hyp (Const("all",_)$Abs(a,T,t)) = strip_assums_hyp t
clasohm@0
   213
  | strip_assums_hyp B = [];
clasohm@0
   214
clasohm@0
   215
(*Strips assumptions in goal, yielding conclusion.   *)
clasohm@0
   216
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
clasohm@0
   217
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
clasohm@0
   218
  | strip_assums_concl B = B;
clasohm@0
   219
clasohm@0
   220
(*Make a list of all the parameters in a subgoal, even if nested*)
clasohm@0
   221
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
clasohm@0
   222
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
clasohm@0
   223
  | strip_params B = [];
clasohm@0
   224
wenzelm@9667
   225
(*test for meta connectives in prems of a 'subgoal'*)
wenzelm@9667
   226
fun has_meta_prems prop i =
wenzelm@9667
   227
  let
wenzelm@9667
   228
    fun is_meta (Const ("==>", _) $ _ $ _) = true
wenzelm@10442
   229
      | is_meta (Const ("==", _) $ _ $ _) = true
wenzelm@9667
   230
      | is_meta (Const ("all", _) $ _) = true
wenzelm@9667
   231
      | is_meta _ = false;
wenzelm@9667
   232
  in
berghofe@13659
   233
    (case strip_prems (i, [], prop) of
wenzelm@9667
   234
      (B :: _, _) => exists is_meta (strip_assums_hyp B)
wenzelm@9667
   235
    | _ => false) handle TERM _ => false
wenzelm@9667
   236
  end;
wenzelm@9483
   237
clasohm@0
   238
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
wenzelm@9460
   239
    where j is the total number of parameters (precomputed)
clasohm@0
   240
  If n>0 then deletes assumption n. *)
wenzelm@9460
   241
fun remove_params j n A =
clasohm@0
   242
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
clasohm@0
   243
    else case A of
wenzelm@9460
   244
        Const("==>", _) $ H $ B =>
wenzelm@9460
   245
          if n=1 then                           (remove_params j (n-1) B)
wenzelm@9460
   246
          else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
clasohm@0
   247
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
clasohm@0
   248
      | _ => if n>0 then raise TERM("remove_params", [A])
clasohm@0
   249
             else A;
clasohm@0
   250
clasohm@0
   251
(** Auto-renaming of parameters in subgoals **)
clasohm@0
   252
clasohm@0
   253
val auto_rename = ref false
clasohm@0
   254
and rename_prefix = ref "ka";
clasohm@0
   255
clasohm@0
   256
(*rename_prefix is not exported; it is set by this function.*)
clasohm@0
   257
fun set_rename_prefix a =
wenzelm@4693
   258
    if a<>"" andalso forall Symbol.is_letter (Symbol.explode a)
clasohm@0
   259
    then  (rename_prefix := a;  auto_rename := true)
clasohm@0
   260
    else  error"rename prefix must be nonempty and consist of letters";
clasohm@0
   261
clasohm@0
   262
(*Makes parameters in a goal have distinctive names (not guaranteed unique!)
clasohm@0
   263
  A name clash could cause the printer to rename bound vars;
clasohm@0
   264
    then res_inst_tac would not work properly.*)
clasohm@0
   265
fun rename_vars (a, []) = []
clasohm@0
   266
  | rename_vars (a, (_,T)::vars) =
wenzelm@12902
   267
        (a,T) :: rename_vars (Symbol.bump_string a, vars);
clasohm@0
   268
clasohm@0
   269
(*Move all parameters to the front of the subgoal, renaming them apart;
clasohm@0
   270
  if n>0 then deletes assumption n. *)
clasohm@0
   271
fun flatten_params n A =
clasohm@0
   272
    let val params = strip_params A;
wenzelm@9460
   273
        val vars = if !auto_rename
wenzelm@9460
   274
                   then rename_vars (!rename_prefix, params)
wenzelm@9460
   275
                   else ListPair.zip (variantlist(map #1 params,[]),
wenzelm@9460
   276
                                      map #2 params)
clasohm@0
   277
    in  list_all (vars, remove_params (length vars) n A)
clasohm@0
   278
    end;
clasohm@0
   279
clasohm@0
   280
(*Makes parameters in a goal have the names supplied by the list cs.*)
clasohm@0
   281
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
clasohm@0
   282
      implies $ A $ list_rename_params (cs, B)
wenzelm@9460
   283
  | list_rename_params (c::cs, Const("all",_)$Abs(_,T,t)) =
clasohm@0
   284
      all T $ Abs(c, T, list_rename_params (cs, t))
clasohm@0
   285
  | list_rename_params (cs, B) = B;
clasohm@0
   286
paulson@9684
   287
(*Strips assumptions in goal yielding  ( [HPn,...,HP1], [xm,...,x1], B ).
paulson@9684
   288
  Where HPi has the form (Hi,nparams_i) and x1...xm are the parameters.
paulson@9684
   289
  We need nparams_i only when the parameters aren't flattened; then we
paulson@9684
   290
    must call incr_boundvars to make up the difference.  See assum_pairs.
paulson@9684
   291
  Without this refinement we can get the wrong answer, e.g. by
wenzelm@10816
   292
        Goal "!!f. EX B. Q(f,B) ==> (!!y. P(f,y))";
wenzelm@10816
   293
        by (etac exE 1);
paulson@9684
   294
 *)
paulson@9684
   295
fun strip_assums_aux (HPs, params, Const("==>", _) $ H $ B) =
paulson@9684
   296
        strip_assums_aux ((H,length params)::HPs, params, B)
paulson@9684
   297
  | strip_assums_aux (HPs, params, Const("all",_)$Abs(a,T,t)) =
paulson@9684
   298
        strip_assums_aux (HPs, (a,T)::params, t)
paulson@9684
   299
  | strip_assums_aux (HPs, params, B) = (HPs, params, B);
clasohm@0
   300
clasohm@0
   301
fun strip_assums A = strip_assums_aux ([],[],A);
clasohm@0
   302
clasohm@0
   303
clasohm@0
   304
(*Produces disagreement pairs, one for each assumption proof, in order.
clasohm@0
   305
  A is the first premise of the lifted rule, and thus has the form
clasohm@0
   306
    H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B) *)
clasohm@0
   307
fun assum_pairs A =
paulson@9684
   308
  let val (HPs, params, B) = strip_assums A
paulson@9684
   309
      val nparams = length params
clasohm@0
   310
      val D = Unify.rlist_abs(params, B)
wenzelm@10816
   311
      fun incr_hyp(H,np) =
wenzelm@10816
   312
          Unify.rlist_abs(params, incr_boundvars (nparams-np) H)
wenzelm@9460
   313
      fun pairrev ([],pairs) = pairs
paulson@9684
   314
        | pairrev ((H,np)::HPs, pairs) =
paulson@9684
   315
            pairrev(HPs,  (incr_hyp(H,np),D) :: pairs)
wenzelm@10816
   316
  in  pairrev (HPs,[])
clasohm@0
   317
  end;
clasohm@0
   318
clasohm@0
   319
(*Converts Frees to Vars and TFrees to TVars so that axioms can be written
clasohm@0
   320
  without (?) everywhere*)
clasohm@0
   321
fun varify (Const(a,T)) = Const(a, Type.varifyT T)
clasohm@0
   322
  | varify (Free(a,T)) = Var((a,0), Type.varifyT T)
clasohm@0
   323
  | varify (Var(ixn,T)) = Var(ixn, Type.varifyT T)
clasohm@0
   324
  | varify (Abs (a,T,body)) = Abs (a, Type.varifyT T, varify body)
clasohm@0
   325
  | varify (f$t) = varify f $ varify t
clasohm@0
   326
  | varify t = t;
clasohm@0
   327
lcp@546
   328
(*Inverse of varify.  Converts axioms back to their original form.*)
lcp@585
   329
fun unvarify (Const(a,T))    = Const(a, Type.unvarifyT T)
lcp@585
   330
  | unvarify (Var((a,0), T)) = Free(a, Type.unvarifyT T)
lcp@585
   331
  | unvarify (Var(ixn,T))    = Var(ixn, Type.unvarifyT T)  (*non-0 index!*)
lcp@585
   332
  | unvarify (Abs (a,T,body)) = Abs (a, Type.unvarifyT T, unvarify body)
lcp@546
   333
  | unvarify (f$t) = unvarify f $ unvarify t
lcp@546
   334
  | unvarify t = t;
lcp@546
   335
clasohm@0
   336
end;