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