src/Pure/logic.ML
author wenzelm
Sun May 09 18:09:30 2010 +0200 (2010-05-09)
changeset 36767 d0095729e1f1
parent 35854 d452abc96459
child 36768 46be86127972
permissions -rw-r--r--
added Logic.unconstrain_allTs, based on calculations_for_thm_proof by krauss/schropp, but treat type variables uniformly as in strip_shyps (in thm.ML) or stripped_sorts (in more_thm.ML);
wenzelm@9460
     1
(*  Title:      Pure/logic.ML
wenzelm@9460
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@29276
     3
    Author:     Makarius
clasohm@0
     4
wenzelm@9460
     5
Abstract syntax operations of the Pure meta-logic.
clasohm@0
     6
*)
clasohm@0
     7
wenzelm@9460
     8
signature LOGIC =
wenzelm@4345
     9
sig
wenzelm@27334
    10
  val all: term -> term -> term
wenzelm@27334
    11
  val is_all: term -> bool
wenzelm@27334
    12
  val dest_all: term -> (string * typ) * term
wenzelm@18181
    13
  val mk_equals: term * term -> term
wenzelm@18181
    14
  val dest_equals: term -> term * term
wenzelm@27334
    15
  val implies: term
wenzelm@18181
    16
  val mk_implies: term * term -> term
wenzelm@18181
    17
  val dest_implies: term -> term * term
wenzelm@18181
    18
  val list_implies: term list * term -> term
wenzelm@18181
    19
  val strip_imp_prems: term -> term list
wenzelm@18181
    20
  val strip_imp_concl: term -> term
wenzelm@18181
    21
  val strip_prems: int * term list * term -> term list * term
wenzelm@21576
    22
  val count_prems: term -> int
wenzelm@18181
    23
  val nth_prem: int * term -> term
wenzelm@23418
    24
  val true_prop: term
wenzelm@19125
    25
  val conjunction: term
wenzelm@18181
    26
  val mk_conjunction: term * term -> term
wenzelm@12757
    27
  val mk_conjunction_list: term list -> term
wenzelm@23418
    28
  val mk_conjunction_balanced: term list -> term
wenzelm@18469
    29
  val dest_conjunction: term -> term * term
wenzelm@19425
    30
  val dest_conjunction_list: term -> term list
wenzelm@23418
    31
  val dest_conjunction_balanced: int -> term -> term list
wenzelm@18469
    32
  val dest_conjunctions: term -> term list
wenzelm@18181
    33
  val strip_horn: term -> term list * term
wenzelm@21520
    34
  val mk_type: typ -> term
wenzelm@18938
    35
  val dest_type: term -> typ
wenzelm@21520
    36
  val type_map: (term -> term) -> typ -> typ
wenzelm@18938
    37
  val const_of_class: class -> string
wenzelm@18938
    38
  val class_of_const: string -> class
wenzelm@31943
    39
  val mk_of_class: typ * class -> term
wenzelm@31943
    40
  val dest_of_class: term -> typ * class
wenzelm@35854
    41
  val mk_of_sort: typ * sort -> term list
wenzelm@20630
    42
  val name_classrel: string * string -> string
wenzelm@19406
    43
  val mk_classrel: class * class -> term
wenzelm@19406
    44
  val dest_classrel: term -> class * class
wenzelm@20630
    45
  val name_arities: arity -> string list
wenzelm@20630
    46
  val name_arity: string * sort list * class -> string
wenzelm@20630
    47
  val mk_arities: arity -> term list
wenzelm@19406
    48
  val dest_arity: term -> string * sort list * class
wenzelm@36767
    49
  val unconstrain_allTs: sort list -> term -> ((typ -> typ) * ((typ * class) * term) list) * term
wenzelm@18181
    50
  val protectC: term
wenzelm@18181
    51
  val protect: term -> term
wenzelm@18181
    52
  val unprotect: term -> term
wenzelm@19775
    53
  val mk_term: term -> term
wenzelm@19775
    54
  val dest_term: term -> term
wenzelm@18181
    55
  val occs: term * term -> bool
wenzelm@18181
    56
  val close_form: term -> term
wenzelm@18938
    57
  val combound: term * int * int -> term
wenzelm@18938
    58
  val rlist_abs: (string * typ) list * term -> term
wenzelm@32023
    59
  val incr_tvar_same: int -> typ Same.operation
wenzelm@32023
    60
  val incr_tvar: int -> typ -> typ
wenzelm@32026
    61
  val incr_indexes_same: typ list * int -> term Same.operation
wenzelm@18181
    62
  val incr_indexes: typ list * int -> term -> term
wenzelm@18181
    63
  val lift_abs: int -> term -> term -> term
wenzelm@18181
    64
  val lift_all: int -> term -> term -> term
wenzelm@18181
    65
  val strip_assums_hyp: term -> term list
wenzelm@9460
    66
  val strip_assums_concl: term -> term
wenzelm@18181
    67
  val strip_params: term -> (string * typ) list
wenzelm@23597
    68
  val has_meta_prems: term -> bool
wenzelm@18181
    69
  val flatten_params: int -> term -> term
clasohm@0
    70
  val list_rename_params: string list * term -> term
wenzelm@18181
    71
  val assum_pairs: int * term -> (term*term)list
wenzelm@30554
    72
  val assum_problems: int * term -> (term -> term) * term list * term
wenzelm@35845
    73
  val varifyT_global: typ -> typ
wenzelm@35845
    74
  val unvarifyT_global: typ -> typ
wenzelm@35845
    75
  val varify_global: term -> term
wenzelm@35845
    76
  val unvarify_global: term -> term
wenzelm@18181
    77
  val get_goal: term -> int -> term
wenzelm@18181
    78
  val goal_params: term -> int -> term * term list
wenzelm@18181
    79
  val prems_of_goal: term -> int -> term list
wenzelm@18181
    80
  val concl_of_goal: term -> int -> term
wenzelm@4345
    81
end;
clasohm@0
    82
paulson@1500
    83
structure Logic : LOGIC =
clasohm@0
    84
struct
wenzelm@398
    85
wenzelm@4345
    86
clasohm@0
    87
(*** Abstract syntax operations on the meta-connectives ***)
clasohm@0
    88
wenzelm@23238
    89
(** all **)
nipkow@5041
    90
wenzelm@27334
    91
fun all v t = Const ("all", (Term.fastype_of v --> propT) --> propT) $ lambda v t;
wenzelm@27334
    92
wenzelm@27334
    93
fun is_all (Const ("all", _) $ Abs _) = true
wenzelm@27334
    94
  | is_all _ = false;
wenzelm@27334
    95
wenzelm@27334
    96
fun dest_all (Const ("all", _) $ Abs (abs as (_, T, _))) =
wenzelm@27334
    97
      let val (x, b) = Term.dest_abs abs  (*potentially slow*)
wenzelm@27334
    98
      in ((x, T), b) end
wenzelm@18762
    99
  | dest_all t = raise TERM ("dest_all", [t]);
wenzelm@18762
   100
wenzelm@18762
   101
clasohm@0
   102
(** equality **)
clasohm@0
   103
wenzelm@27334
   104
fun mk_equals (t, u) =
wenzelm@27334
   105
  let val T = Term.fastype_of t
wenzelm@27334
   106
  in Const ("==", T --> T --> propT) $ t $ u end;
clasohm@0
   107
wenzelm@20883
   108
fun dest_equals (Const ("==", _) $ t $ u) = (t, u)
wenzelm@20883
   109
  | dest_equals t = raise TERM ("dest_equals", [t]);
wenzelm@637
   110
wenzelm@637
   111
clasohm@0
   112
(** implies **)
clasohm@0
   113
wenzelm@27334
   114
val implies = Const ("==>", propT --> propT --> propT);
wenzelm@27334
   115
wenzelm@20883
   116
fun mk_implies (A, B) = implies $ A $ B;
clasohm@0
   117
wenzelm@20883
   118
fun dest_implies (Const ("==>", _) $ A $ B) = (A, B)
wenzelm@20883
   119
  | dest_implies A = raise TERM ("dest_implies", [A]);
nipkow@5041
   120
wenzelm@4822
   121
clasohm@0
   122
(** nested implications **)
clasohm@0
   123
clasohm@0
   124
(* [A1,...,An], B  goes to  A1==>...An==>B  *)
wenzelm@18181
   125
fun list_implies ([], B) = B
wenzelm@18181
   126
  | list_implies (A::As, B) = implies $ A $ list_implies(As,B);
clasohm@0
   127
clasohm@0
   128
(* A1==>...An==>B  goes to  [A1,...,An], where B is not an implication *)
clasohm@0
   129
fun strip_imp_prems (Const("==>", _) $ A $ B) = A :: strip_imp_prems B
clasohm@0
   130
  | strip_imp_prems _ = [];
clasohm@0
   131
clasohm@0
   132
(* A1==>...An==>B  goes to B, where B is not an implication *)
clasohm@0
   133
fun strip_imp_concl (Const("==>", _) $ A $ B) = strip_imp_concl B
clasohm@0
   134
  | strip_imp_concl A = A : term;
clasohm@0
   135
clasohm@0
   136
(*Strip and return premises: (i, [], A1==>...Ai==>B)
wenzelm@9460
   137
    goes to   ([Ai, A(i-1),...,A1] , B)         (REVERSED)
clasohm@0
   138
  if  i<0 or else i too big then raises  TERM*)
wenzelm@9460
   139
fun strip_prems (0, As, B) = (As, B)
wenzelm@9460
   140
  | strip_prems (i, As, Const("==>", _) $ A $ B) =
wenzelm@9460
   141
        strip_prems (i-1, A::As, B)
clasohm@0
   142
  | strip_prems (_, As, A) = raise TERM("strip_prems", A::As);
clasohm@0
   143
wenzelm@16130
   144
(*Count premises -- quicker than (length o strip_prems) *)
wenzelm@21576
   145
fun count_prems (Const ("==>", _) $ _ $ B) = 1 + count_prems B
wenzelm@21576
   146
  | count_prems _ = 0;
clasohm@0
   147
wenzelm@16130
   148
(*Select Ai from A1 ==>...Ai==>B*)
wenzelm@16130
   149
fun nth_prem (1, Const ("==>", _) $ A $ _) = A
wenzelm@16130
   150
  | nth_prem (i, Const ("==>", _) $ _ $ B) = nth_prem (i - 1, B)
wenzelm@16130
   151
  | nth_prem (_, A) = raise TERM ("nth_prem", [A]);
wenzelm@16130
   152
berghofe@13659
   153
(*strip a proof state (Horn clause):
berghofe@13659
   154
  B1 ==> ... Bn ==> C   goes to   ([B1, ..., Bn], C)    *)
berghofe@13659
   155
fun strip_horn A = (strip_imp_prems A, strip_imp_concl A);
berghofe@13659
   156
wenzelm@4822
   157
wenzelm@23418
   158
wenzelm@12137
   159
(** conjunction **)
wenzelm@12137
   160
wenzelm@23418
   161
val true_prop = Term.all propT $ Abs ("dummy", propT, mk_implies (Bound 0, Bound 0));
wenzelm@26424
   162
val conjunction = Const ("Pure.conjunction", propT --> propT --> propT);
wenzelm@19125
   163
wenzelm@23418
   164
wenzelm@28856
   165
(*A &&& B*)
wenzelm@19125
   166
fun mk_conjunction (A, B) = conjunction $ A $ B;
wenzelm@12137
   167
wenzelm@28856
   168
(*A &&& B &&& C -- improper*)
wenzelm@23418
   169
fun mk_conjunction_list [] = true_prop
wenzelm@12757
   170
  | mk_conjunction_list ts = foldr1 mk_conjunction ts;
wenzelm@12137
   171
wenzelm@28856
   172
(*(A &&& B) &&& (C &&& D) -- balanced*)
wenzelm@23418
   173
fun mk_conjunction_balanced [] = true_prop
wenzelm@32765
   174
  | mk_conjunction_balanced ts = Balanced_Tree.make mk_conjunction ts;
wenzelm@23418
   175
wenzelm@18499
   176
wenzelm@28856
   177
(*A &&& B*)
wenzelm@26424
   178
fun dest_conjunction (Const ("Pure.conjunction", _) $ A $ B) = (A, B)
wenzelm@18469
   179
  | dest_conjunction t = raise TERM ("dest_conjunction", [t]);
wenzelm@18469
   180
wenzelm@28856
   181
(*A &&& B &&& C -- improper*)
wenzelm@19425
   182
fun dest_conjunction_list t =
wenzelm@19425
   183
  (case try dest_conjunction t of
wenzelm@19425
   184
    NONE => [t]
wenzelm@19425
   185
  | SOME (A, B) => A :: dest_conjunction_list B);
wenzelm@19425
   186
wenzelm@28856
   187
(*(A &&& B) &&& (C &&& D) -- balanced*)
wenzelm@23418
   188
fun dest_conjunction_balanced 0 _ = []
wenzelm@32765
   189
  | dest_conjunction_balanced n t = Balanced_Tree.dest dest_conjunction n t;
wenzelm@23418
   190
wenzelm@28856
   191
(*((A &&& B) &&& C) &&& D &&& E -- flat*)
wenzelm@18469
   192
fun dest_conjunctions t =
wenzelm@18469
   193
  (case try dest_conjunction t of
wenzelm@18469
   194
    NONE => [t]
wenzelm@18469
   195
  | SOME (A, B) => dest_conjunctions A @ dest_conjunctions B);
wenzelm@18469
   196
wenzelm@18469
   197
wenzelm@12137
   198
wenzelm@398
   199
(** types as terms **)
wenzelm@398
   200
wenzelm@19391
   201
fun mk_type ty = Const ("TYPE", Term.itselfT ty);
wenzelm@398
   202
wenzelm@398
   203
fun dest_type (Const ("TYPE", Type ("itself", [ty]))) = ty
wenzelm@398
   204
  | dest_type t = raise TERM ("dest_type", [t]);
wenzelm@398
   205
wenzelm@21520
   206
fun type_map f = dest_type o f o mk_type;
wenzelm@21520
   207
wenzelm@4822
   208
wenzelm@19406
   209
wenzelm@19406
   210
(** type classes **)
wenzelm@19406
   211
wenzelm@19406
   212
(* const names *)
wenzelm@398
   213
wenzelm@18938
   214
val classN = "_class";
wenzelm@18938
   215
wenzelm@18938
   216
val const_of_class = suffix classN;
wenzelm@19406
   217
wenzelm@18938
   218
fun class_of_const c = unsuffix classN c
wenzelm@18938
   219
  handle Fail _ => raise TERM ("class_of_const: bad name " ^ quote c, []);
wenzelm@18938
   220
wenzelm@19406
   221
wenzelm@35854
   222
(* class/sort membership *)
wenzelm@19406
   223
wenzelm@31943
   224
fun mk_of_class (ty, c) =
wenzelm@19391
   225
  Const (const_of_class c, Term.itselfT ty --> propT) $ mk_type ty;
wenzelm@398
   226
wenzelm@32784
   227
fun dest_of_class (Const (c_class, _) $ ty) = (dest_type ty, class_of_const c_class)
wenzelm@31943
   228
  | dest_of_class t = raise TERM ("dest_of_class", [t]);
wenzelm@398
   229
wenzelm@35854
   230
fun mk_of_sort (ty, S) = map (fn c => mk_of_class (ty, c)) S;
wenzelm@35854
   231
clasohm@0
   232
wenzelm@19406
   233
(* class relations *)
wenzelm@19406
   234
wenzelm@20630
   235
fun name_classrel (c1, c2) =
wenzelm@30364
   236
  Long_Name.base_name c1 ^ "_" ^ Long_Name.base_name c2;
wenzelm@20630
   237
wenzelm@31943
   238
fun mk_classrel (c1, c2) = mk_of_class (Term.aT [c1], c2);
wenzelm@19406
   239
wenzelm@19406
   240
fun dest_classrel tm =
wenzelm@31943
   241
  (case dest_of_class tm of
wenzelm@19406
   242
    (TVar (_, [c1]), c2) => (c1, c2)
wenzelm@19406
   243
  | _ => raise TERM ("dest_classrel", [tm]));
wenzelm@19406
   244
wenzelm@19406
   245
wenzelm@19406
   246
(* type arities *)
wenzelm@19406
   247
wenzelm@20630
   248
fun name_arities (t, _, S) =
wenzelm@30364
   249
  let val b = Long_Name.base_name t
wenzelm@30364
   250
  in S |> map (fn c => Long_Name.base_name c ^ "_" ^ b) end;
wenzelm@20630
   251
wenzelm@20630
   252
fun name_arity (t, dom, c) = hd (name_arities (t, dom, [c]));
wenzelm@20630
   253
wenzelm@19406
   254
fun mk_arities (t, Ss, S) =
wenzelm@24848
   255
  let val T = Type (t, ListPair.map TFree (Name.invents Name.context Name.aT (length Ss), Ss))
wenzelm@31943
   256
  in map (fn c => mk_of_class (T, c)) S end;
wenzelm@19406
   257
wenzelm@19406
   258
fun dest_arity tm =
wenzelm@19406
   259
  let
wenzelm@19406
   260
    fun err () = raise TERM ("dest_arity", [tm]);
wenzelm@19406
   261
wenzelm@31943
   262
    val (ty, c) = dest_of_class tm;
wenzelm@19406
   263
    val (t, tvars) =
wenzelm@19406
   264
      (case ty of
wenzelm@19406
   265
        Type (t, tys) => (t, map dest_TVar tys handle TYPE _ => err ())
wenzelm@19406
   266
      | _ => err ());
wenzelm@19406
   267
    val Ss =
wenzelm@19406
   268
      if has_duplicates (eq_fst (op =)) tvars then err ()
wenzelm@19406
   269
      else map snd tvars;
wenzelm@19406
   270
  in (t, Ss, c) end;
wenzelm@19406
   271
wenzelm@19406
   272
wenzelm@36767
   273
(* internalized sort constraints *)
wenzelm@36767
   274
wenzelm@36767
   275
fun unconstrain_allTs shyps prop =
wenzelm@36767
   276
  let
wenzelm@36767
   277
    val present = rev ((fold_types o fold_atyps_sorts) (insert (eq_fst op =)) prop []);
wenzelm@36767
   278
    val extra = fold (Sorts.remove_sort o #2) present shyps;
wenzelm@36767
   279
wenzelm@36767
   280
    val n = length present;
wenzelm@36767
   281
    val (names1, names2) = Name.invents Name.context Name.aT (n + length extra) |> chop n;
wenzelm@36767
   282
wenzelm@36767
   283
    val present_map =
wenzelm@36767
   284
      map2 (fn (T, S) => fn a => (T, TVar ((a, 0), S))) present names1;
wenzelm@36767
   285
    val constraints_map =
wenzelm@36767
   286
      map2 (fn (_, S) => fn a => (S, TVar ((a, 0), S))) present names1 @
wenzelm@36767
   287
      map2 (fn S => fn a => (S, TVar ((a, 0), S))) extra names2;
wenzelm@36767
   288
wenzelm@36767
   289
    fun atyp_map T =
wenzelm@36767
   290
      (case AList.lookup (op =) present_map T of
wenzelm@36767
   291
        SOME U => U
wenzelm@36767
   292
      | NONE =>
wenzelm@36767
   293
          (case AList.lookup (op =) constraints_map (Type.sort_of_atyp T) of
wenzelm@36767
   294
            SOME U => U
wenzelm@36767
   295
          | NONE => raise TYPE ("Dangling type variable", [T], [])));
wenzelm@36767
   296
wenzelm@36767
   297
    val constraints =
wenzelm@36767
   298
      maps (fn (_, T as TVar (ai, S)) =>
wenzelm@36767
   299
        map (fn c => ((T, c), mk_of_class (TVar (ai, []), c))) S)
wenzelm@36767
   300
        constraints_map;
wenzelm@36767
   301
wenzelm@36767
   302
    val prop' =
wenzelm@36767
   303
      prop
wenzelm@36767
   304
      |> (Term.map_types o Term.map_atyps) (Type.strip_sorts o atyp_map)
wenzelm@36767
   305
      |> curry list_implies (map snd constraints);
wenzelm@36767
   306
  in ((atyp_map, constraints), prop') end;
wenzelm@36767
   307
wenzelm@36767
   308
wenzelm@18938
   309
wenzelm@28448
   310
(** protected propositions and embedded terms **)
wenzelm@9460
   311
wenzelm@18029
   312
val protectC = Const ("prop", propT --> propT);
wenzelm@18029
   313
fun protect t = protectC $ t;
wenzelm@9460
   314
wenzelm@18029
   315
fun unprotect (Const ("prop", _) $ t) = t
wenzelm@18029
   316
  | unprotect t = raise TERM ("unprotect", [t]);
wenzelm@9460
   317
wenzelm@9460
   318
wenzelm@26424
   319
fun mk_term t = Const ("Pure.term", Term.fastype_of t --> propT) $ t;
wenzelm@19775
   320
wenzelm@26424
   321
fun dest_term (Const ("Pure.term", _) $ t) = t
wenzelm@19775
   322
  | dest_term t = raise TERM ("dest_term", [t]);
wenzelm@19775
   323
wenzelm@19775
   324
wenzelm@18181
   325
clasohm@0
   326
(*** Low-level term operations ***)
clasohm@0
   327
clasohm@0
   328
(*Does t occur in u?  Or is alpha-convertible to u?
clasohm@0
   329
  The term t must contain no loose bound variables*)
wenzelm@16846
   330
fun occs (t, u) = exists_subterm (fn s => t aconv s) u;
clasohm@0
   331
clasohm@0
   332
(*Close up a formula over all free variables by quantification*)
clasohm@0
   333
fun close_form A =
wenzelm@19425
   334
  Term.list_all_free (rev (Term.add_frees A []), A);
clasohm@0
   335
clasohm@0
   336
wenzelm@18938
   337
clasohm@0
   338
(*** Specialized operations for resolution... ***)
clasohm@0
   339
wenzelm@18938
   340
(*computes t(Bound(n+k-1),...,Bound(n))  *)
wenzelm@18938
   341
fun combound (t, n, k) =
wenzelm@18938
   342
    if  k>0  then  combound (t,n+1,k-1) $ (Bound n)  else  t;
wenzelm@18938
   343
wenzelm@18938
   344
(* ([xn,...,x1], t)   ======>   (x1,...,xn)t *)
wenzelm@18938
   345
fun rlist_abs ([], body) = body
wenzelm@18938
   346
  | rlist_abs ((a,T)::pairs, body) = rlist_abs(pairs, Abs(a, T, body));
wenzelm@18938
   347
wenzelm@32026
   348
fun incr_tvar_same 0 = Same.same
wenzelm@32026
   349
  | incr_tvar_same k = Term_Subst.map_atypsT_same
wenzelm@32026
   350
      (fn TVar ((a, i), S) => TVar ((a, i + k), S)
wenzelm@32026
   351
        | _ => raise Same.SAME);
wenzelm@16879
   352
wenzelm@32026
   353
fun incr_tvar k T = incr_tvar_same k T handle Same.SAME => T;
wenzelm@32023
   354
clasohm@0
   355
(*For all variables in the term, increment indexnames and lift over the Us
clasohm@0
   356
    result is ?Gidx(B.(lev+n-1),...,B.lev) where lev is abstraction level *)
wenzelm@32026
   357
fun incr_indexes_same ([], 0) = Same.same
wenzelm@32026
   358
  | incr_indexes_same (Ts, k) =
wenzelm@32020
   359
      let
wenzelm@32020
   360
        val n = length Ts;
wenzelm@32026
   361
        val incrT = incr_tvar_same k;
wenzelm@16879
   362
wenzelm@32020
   363
        fun incr lev (Var ((x, i), T)) =
wenzelm@32020
   364
              combound (Var ((x, i + k), Ts ---> Same.commit incrT T), lev, n)
wenzelm@32020
   365
          | incr lev (Abs (x, T, body)) =
wenzelm@32020
   366
              (Abs (x, incrT T, incr (lev + 1) body handle Same.SAME => body)
wenzelm@32020
   367
                handle Same.SAME => Abs (x, T, incr (lev + 1) body))
wenzelm@32020
   368
          | incr lev (t $ u) =
wenzelm@32020
   369
              (incr lev t $ (incr lev u handle Same.SAME => u)
wenzelm@32020
   370
                handle Same.SAME => t $ incr lev u)
wenzelm@32020
   371
          | incr _ (Const (c, T)) = Const (c, incrT T)
wenzelm@32020
   372
          | incr _ (Free (x, T)) = Free (x, incrT T)
wenzelm@32026
   373
          | incr _ (Bound _) = raise Same.SAME;
wenzelm@32026
   374
      in incr 0 end;
wenzelm@32026
   375
wenzelm@32026
   376
fun incr_indexes arg t = incr_indexes_same arg t handle Same.SAME => t;
wenzelm@16879
   377
clasohm@0
   378
wenzelm@18248
   379
(* Lifting functions from subgoal and increment:
wenzelm@18029
   380
    lift_abs operates on terms
wenzelm@18029
   381
    lift_all operates on propositions *)
wenzelm@18029
   382
wenzelm@18029
   383
fun lift_abs inc =
wenzelm@18029
   384
  let
wenzelm@18029
   385
    fun lift Ts (Const ("==>", _) $ _ $ B) t = lift Ts B t
wenzelm@18248
   386
      | lift Ts (Const ("all", _) $ Abs (a, T, B)) t = Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   387
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   388
  in lift [] end;
wenzelm@18029
   389
wenzelm@18029
   390
fun lift_all inc =
wenzelm@18029
   391
  let
wenzelm@18029
   392
    fun lift Ts ((c as Const ("==>", _)) $ A $ B) t = c $ A $ lift Ts B t
wenzelm@18248
   393
      | lift Ts ((c as Const ("all", _)) $ Abs (a, T, B)) t = c $ Abs (a, T, lift (T :: Ts) B t)
wenzelm@18029
   394
      | lift Ts _ t = incr_indexes (rev Ts, inc) t;
wenzelm@18029
   395
  in lift [] end;
clasohm@0
   396
clasohm@0
   397
(*Strips assumptions in goal, yielding list of hypotheses.   *)
berghofe@21016
   398
fun strip_assums_hyp B =
berghofe@21016
   399
  let
berghofe@21016
   400
    fun strip Hs (Const ("==>", _) $ H $ B) = strip (H :: Hs) B
berghofe@21016
   401
      | strip Hs (Const ("all", _) $ Abs (a, T, t)) =
berghofe@21016
   402
          strip (map (incr_boundvars 1) Hs) t
berghofe@21016
   403
      | strip Hs B = rev Hs
berghofe@21016
   404
  in strip [] B end;
clasohm@0
   405
clasohm@0
   406
(*Strips assumptions in goal, yielding conclusion.   *)
clasohm@0
   407
fun strip_assums_concl (Const("==>", _) $ H $ B) = strip_assums_concl B
clasohm@0
   408
  | strip_assums_concl (Const("all",_)$Abs(a,T,t)) = strip_assums_concl t
clasohm@0
   409
  | strip_assums_concl B = B;
clasohm@0
   410
clasohm@0
   411
(*Make a list of all the parameters in a subgoal, even if nested*)
clasohm@0
   412
fun strip_params (Const("==>", _) $ H $ B) = strip_params B
clasohm@0
   413
  | strip_params (Const("all",_)$Abs(a,T,t)) = (a,T) :: strip_params t
clasohm@0
   414
  | strip_params B = [];
clasohm@0
   415
wenzelm@23597
   416
(*test for nested meta connectives in prems*)
wenzelm@23597
   417
val has_meta_prems =
wenzelm@9667
   418
  let
wenzelm@23597
   419
    fun is_meta (Const ("==", _) $ _ $ _) = true
wenzelm@23597
   420
      | is_meta (Const ("==>", _) $ _ $ _) = true
wenzelm@9667
   421
      | is_meta (Const ("all", _) $ _) = true
wenzelm@9667
   422
      | is_meta _ = false;
wenzelm@23597
   423
    fun ex_meta (Const ("==>", _) $ A $ B) = is_meta A orelse ex_meta B
wenzelm@23597
   424
      | ex_meta (Const ("all", _) $ Abs (_, _, B)) = ex_meta B
wenzelm@23597
   425
      | ex_meta _ = false;
wenzelm@23597
   426
  in ex_meta end;
wenzelm@9483
   427
clasohm@0
   428
(*Removes the parameters from a subgoal and renumber bvars in hypotheses,
wenzelm@9460
   429
    where j is the total number of parameters (precomputed)
clasohm@0
   430
  If n>0 then deletes assumption n. *)
wenzelm@9460
   431
fun remove_params j n A =
clasohm@0
   432
    if j=0 andalso n<=0 then A  (*nothing left to do...*)
clasohm@0
   433
    else case A of
wenzelm@9460
   434
        Const("==>", _) $ H $ B =>
wenzelm@9460
   435
          if n=1 then                           (remove_params j (n-1) B)
wenzelm@9460
   436
          else implies $ (incr_boundvars j H) $ (remove_params j (n-1) B)
clasohm@0
   437
      | Const("all",_)$Abs(a,T,t) => remove_params (j-1) n t
clasohm@0
   438
      | _ => if n>0 then raise TERM("remove_params", [A])
clasohm@0
   439
             else A;
clasohm@0
   440
clasohm@0
   441
(*Move all parameters to the front of the subgoal, renaming them apart;
clasohm@0
   442
  if n>0 then deletes assumption n. *)
clasohm@0
   443
fun flatten_params n A =
clasohm@0
   444
    let val params = strip_params A;
berghofe@25939
   445
        val vars = ListPair.zip (Name.variant_list [] (map #1 params),
berghofe@25939
   446
                                 map #2 params)
clasohm@0
   447
    in  list_all (vars, remove_params (length vars) n A)
clasohm@0
   448
    end;
clasohm@0
   449
clasohm@0
   450
(*Makes parameters in a goal have the names supplied by the list cs.*)
clasohm@0
   451
fun list_rename_params (cs, Const("==>", _) $ A $ B) =
clasohm@0
   452
      implies $ A $ list_rename_params (cs, B)
wenzelm@27334
   453
  | list_rename_params (c::cs, (a as Const("all",_)) $ Abs(_,T,t)) =
wenzelm@27334
   454
      a $ Abs(c, T, list_rename_params (cs, t))
clasohm@0
   455
  | list_rename_params (cs, B) = B;
clasohm@0
   456
wenzelm@32008
   457
wenzelm@32008
   458
wenzelm@19806
   459
(*** Treatment of "assume", "erule", etc. ***)
clasohm@0
   460
wenzelm@16879
   461
(*Strips assumptions in goal yielding
paulson@15451
   462
   HS = [Hn,...,H1],   params = [xm,...,x1], and B,
wenzelm@16879
   463
  where x1...xm are the parameters. This version (21.1.2005) REQUIRES
wenzelm@16879
   464
  the the parameters to be flattened, but it allows erule to work on
paulson@15451
   465
  assumptions of the form !!x. phi. Any !! after the outermost string
paulson@15451
   466
  will be regarded as belonging to the conclusion, and left untouched.
paulson@15454
   467
  Used ONLY by assum_pairs.
paulson@15454
   468
      Unless nasms<0, it can terminate the recursion early; that allows
paulson@15454
   469
  erule to work on assumptions of the form P==>Q.*)
paulson@15454
   470
fun strip_assums_imp (0, Hs, B) = (Hs, B)  (*recursion terminated by nasms*)
wenzelm@16879
   471
  | strip_assums_imp (nasms, Hs, Const("==>", _) $ H $ B) =
paulson@15454
   472
      strip_assums_imp (nasms-1, H::Hs, B)
paulson@15454
   473
  | strip_assums_imp (_, Hs, B) = (Hs, B); (*recursion terminated by B*)
paulson@15454
   474
wenzelm@32008
   475
(*Strips OUTER parameters only.*)
paulson@15451
   476
fun strip_assums_all (params, Const("all",_)$Abs(a,T,t)) =
paulson@15451
   477
      strip_assums_all ((a,T)::params, t)
paulson@15451
   478
  | strip_assums_all (params, B) = (params, B);
clasohm@0
   479
clasohm@0
   480
(*Produces disagreement pairs, one for each assumption proof, in order.
clasohm@0
   481
  A is the first premise of the lifted rule, and thus has the form
paulson@15454
   482
    H1 ==> ... Hk ==> B   and the pairs are (H1,B),...,(Hk,B).
paulson@15454
   483
  nasms is the number of assumptions in the original subgoal, needed when B
paulson@15454
   484
    has the form B1 ==> B2: it stops B1 from being taken as an assumption. *)
paulson@15454
   485
fun assum_pairs(nasms,A) =
paulson@15451
   486
  let val (params, A') = strip_assums_all ([],A)
paulson@15454
   487
      val (Hs,B) = strip_assums_imp (nasms,[],A')
wenzelm@18938
   488
      fun abspar t = rlist_abs(params, t)
paulson@15451
   489
      val D = abspar B
paulson@15451
   490
      fun pairrev ([], pairs) = pairs
paulson@15451
   491
        | pairrev (H::Hs, pairs) = pairrev(Hs,  (abspar H, D) :: pairs)
paulson@15451
   492
  in  pairrev (Hs,[])
clasohm@0
   493
  end;
clasohm@0
   494
wenzelm@30554
   495
fun assum_problems (nasms, A) =
wenzelm@30554
   496
  let
wenzelm@30554
   497
    val (params, A') = strip_assums_all ([], A)
wenzelm@30554
   498
    val (Hs, B) = strip_assums_imp (nasms, [], A')
wenzelm@30554
   499
    fun abspar t = rlist_abs (params, t)
wenzelm@30554
   500
  in (abspar, rev Hs, B) end;
wenzelm@30554
   501
wenzelm@19806
   502
wenzelm@19806
   503
(* global schematic variables *)
wenzelm@19806
   504
wenzelm@19806
   505
fun bad_schematic xi = "Illegal schematic variable: " ^ quote (Term.string_of_vname xi);
wenzelm@19806
   506
fun bad_fixed x = "Illegal fixed variable: " ^ quote x;
wenzelm@19806
   507
wenzelm@35845
   508
fun varifyT_global_same ty = ty
wenzelm@32020
   509
  |> Term_Subst.map_atypsT_same
wenzelm@32020
   510
    (fn TFree (a, S) => TVar ((a, 0), S)
wenzelm@31981
   511
      | TVar (ai, _) => raise TYPE (bad_schematic ai, [ty], []));
wenzelm@19806
   512
wenzelm@35845
   513
fun unvarifyT_global_same ty = ty
wenzelm@32020
   514
  |> Term_Subst.map_atypsT_same
wenzelm@32020
   515
    (fn TVar ((a, 0), S) => TFree (a, S)
wenzelm@31981
   516
      | TVar (ai, _) => raise TYPE (bad_schematic ai, [ty], [])
wenzelm@31981
   517
      | TFree (a, _) => raise TYPE (bad_fixed a, [ty], []));
clasohm@0
   518
wenzelm@35845
   519
val varifyT_global = Same.commit varifyT_global_same;
wenzelm@35845
   520
val unvarifyT_global = Same.commit unvarifyT_global_same;
wenzelm@31981
   521
wenzelm@35845
   522
fun varify_global tm = tm
wenzelm@32020
   523
  |> Same.commit (Term_Subst.map_aterms_same
wenzelm@32020
   524
    (fn Free (x, T) => Var ((x, 0), T)
wenzelm@19806
   525
      | Var (xi, _) => raise TERM (bad_schematic xi, [tm])
wenzelm@32020
   526
      | _ => raise Same.SAME))
wenzelm@35845
   527
  |> Same.commit (Term_Subst.map_types_same varifyT_global_same)
wenzelm@19879
   528
  handle TYPE (msg, _, _) => raise TERM (msg, [tm]);
wenzelm@19806
   529
wenzelm@35845
   530
fun unvarify_global tm = tm
wenzelm@32020
   531
  |> Same.commit (Term_Subst.map_aterms_same
wenzelm@32020
   532
    (fn Var ((x, 0), T) => Free (x, T)
wenzelm@19806
   533
      | Var (xi, _) => raise TERM (bad_schematic xi, [tm])
wenzelm@19806
   534
      | Free (x, _) => raise TERM (bad_fixed x, [tm])
wenzelm@32020
   535
      | _ => raise Same.SAME))
wenzelm@35845
   536
  |> Same.commit (Term_Subst.map_types_same unvarifyT_global_same)
wenzelm@19879
   537
  handle TYPE (msg, _, _) => raise TERM (msg, [tm]);
wenzelm@19806
   538
berghofe@13799
   539
wenzelm@16862
   540
(* goal states *)
wenzelm@16862
   541
wenzelm@16862
   542
fun get_goal st i = nth_prem (i, st)
wenzelm@16862
   543
  handle TERM _ => error "Goal number out of range";
berghofe@13799
   544
berghofe@13799
   545
(*reverses parameters for substitution*)
berghofe@13799
   546
fun goal_params st i =
berghofe@13799
   547
  let val gi = get_goal st i
wenzelm@29276
   548
      val rfrees = map Free (Term.rename_wrt_term gi (strip_params gi))
berghofe@13799
   549
  in (gi, rfrees) end;
berghofe@13799
   550
berghofe@13799
   551
fun concl_of_goal st i =
berghofe@13799
   552
  let val (gi, rfrees) = goal_params st i
berghofe@13799
   553
      val B = strip_assums_concl gi
berghofe@13799
   554
  in subst_bounds (rfrees, B) end;
berghofe@13799
   555
berghofe@13799
   556
fun prems_of_goal st i =
berghofe@13799
   557
  let val (gi, rfrees) = goal_params st i
berghofe@13799
   558
      val As = strip_assums_hyp gi
berghofe@13799
   559
  in map (fn A => subst_bounds (rfrees, A)) As end;
berghofe@13799
   560
clasohm@0
   561
end;