src/Pure/zterm.ML
author wenzelm
Tue, 04 Jun 2024 15:13:26 +0200
changeset 80262 d49f3a1c06a6
parent 79478 5c1451900bec
child 80264 71c1cf9e7413
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/zterm.ML
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
     3
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
     4
Tight representation of types / terms / proof terms, notably for proof recording.
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
     5
*)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
     6
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
     7
(*** global ***)
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
     8
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
     9
(* types and terms *)
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    10
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    11
datatype ztyp =
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    12
    ZTVar of indexname * sort      (*free: index ~1*)
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    13
  | ZFun of ztyp * ztyp
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    14
  | ZProp
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    15
  | ZType0 of string               (*type constant*)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    16
  | ZType1 of string * ztyp        (*type constructor: 1 argument*)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    17
  | ZType of string * ztyp list    (*type constructor: >= 2 arguments*)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    18
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    19
datatype zterm =
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    20
    ZVar of indexname * ztyp       (*free: index ~1*)
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    21
  | ZBound of int
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    22
  | ZConst0 of string              (*monomorphic constant*)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    23
  | ZConst1 of string * ztyp       (*polymorphic constant: 1 type argument*)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    24
  | ZConst of string * ztyp list   (*polymorphic constant: >= 2 type arguments*)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    25
  | ZAbs of string * ztyp * zterm
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    26
  | ZApp of zterm * zterm
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    27
  | ZClass of ztyp * class         (*OFCLASS proposition*)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    28
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
    29
structure ZTerm =
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    30
struct
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    31
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    32
(* fold *)
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    33
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    34
fun fold_tvars f (ZTVar v) = f v
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    35
  | fold_tvars f (ZFun (T, U)) = fold_tvars f T #> fold_tvars f U
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    36
  | fold_tvars f (ZType1 (_, T)) = fold_tvars f T
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    37
  | fold_tvars f (ZType (_, Ts)) = fold (fold_tvars f) Ts
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    38
  | fold_tvars _ _ = I;
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    39
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    40
fun fold_aterms f (ZApp (t, u)) = fold_aterms f t #> fold_aterms f u
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    41
  | fold_aterms f (ZAbs (_, _, t)) = fold_aterms f t
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    42
  | fold_aterms f a = f a;
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    43
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    44
fun fold_types f (ZVar (_, T)) = f T
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    45
  | fold_types f (ZConst1 (_, T)) = f T
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    46
  | fold_types f (ZConst (_, As)) = fold f As
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    47
  | fold_types f (ZAbs (_, T, b)) = f T #> fold_types f b
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    48
  | fold_types f (ZApp (t, u)) = fold_types f t #> fold_types f u
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    49
  | fold_types f (ZClass (T, _)) = f T
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    50
  | fold_types _ _ = I;
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    51
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    52
79264
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    53
(* type ordering *)
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    54
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    55
local
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    56
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    57
fun cons_nr (ZTVar _) = 0
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    58
  | cons_nr (ZFun _) = 1
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    59
  | cons_nr ZProp = 2
79420
4c3346b4b100 clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
wenzelm
parents: 79419
diff changeset
    60
  | cons_nr (ZType0 _) = 3
4c3346b4b100 clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
wenzelm
parents: 79419
diff changeset
    61
  | cons_nr (ZType1 _) = 4
4c3346b4b100 clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
wenzelm
parents: 79419
diff changeset
    62
  | cons_nr (ZType _) = 5;
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    63
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    64
val fast_indexname_ord = Term_Ord.fast_indexname_ord;
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    65
val sort_ord = Term_Ord.sort_ord;
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    66
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    67
in
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    68
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    69
fun ztyp_ord TU =
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    70
  if pointer_eq TU then EQUAL
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    71
  else
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    72
    (case TU of
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    73
      (ZTVar (a, A), ZTVar (b, B)) =>
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
    74
        (case fast_indexname_ord (a, b) of EQUAL => sort_ord (A, B) | ord => ord)
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    75
    | (ZFun (T, T'), ZFun (U, U')) =>
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    76
        (case ztyp_ord (T, U) of EQUAL => ztyp_ord (T', U') | ord => ord)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    77
    | (ZProp, ZProp) => EQUAL
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    78
    | (ZType0 a, ZType0 b) => fast_string_ord (a, b)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    79
    | (ZType1 (a, T), ZType1 (b, U)) =>
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    80
        (case fast_string_ord (a, b) of EQUAL => ztyp_ord (T, U) | ord => ord)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    81
    | (ZType (a, Ts), ZType (b, Us)) =>
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    82
        (case fast_string_ord (a, b) of EQUAL => dict_ord ztyp_ord (Ts, Us) | ord => ord)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    83
    | (T, U) => int_ord (cons_nr T, cons_nr U));
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    84
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    85
end;
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
    86
79264
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    87
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    88
(* term ordering and alpha-conversion *)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    89
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    90
local
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    91
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    92
fun cons_nr (ZVar _) = 0
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    93
  | cons_nr (ZBound _) = 1
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    94
  | cons_nr (ZConst0 _) = 2
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    95
  | cons_nr (ZConst1 _) = 3
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    96
  | cons_nr (ZConst _) = 4
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    97
  | cons_nr (ZAbs _) = 5
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    98
  | cons_nr (ZApp _) = 6
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
    99
  | cons_nr (ZClass _) = 7;
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   100
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   101
fun struct_ord tu =
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   102
  if pointer_eq tu then EQUAL
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   103
  else
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   104
    (case tu of
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   105
      (ZAbs (_, _, t), ZAbs (_, _, u)) => struct_ord (t, u)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   106
    | (ZApp (t1, t2), ZApp (u1, u2)) =>
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   107
        (case struct_ord (t1, u1) of EQUAL => struct_ord (t2, u2) | ord => ord)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   108
    | (t, u) => int_ord (cons_nr t, cons_nr u));
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   109
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   110
fun atoms_ord tu =
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   111
  if pointer_eq tu then EQUAL
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   112
  else
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   113
    (case tu of
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   114
      (ZAbs (_, _, t), ZAbs (_, _, u)) => atoms_ord (t, u)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   115
    | (ZApp (t1, t2), ZApp (u1, u2)) =>
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   116
        (case atoms_ord (t1, u1) of EQUAL => atoms_ord (t2, u2) | ord => ord)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   117
    | (ZConst0 a, ZConst0 b) => fast_string_ord (a, b)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   118
    | (ZConst1 (a, _), ZConst1 (b, _)) => fast_string_ord (a, b)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   119
    | (ZConst (a, _), ZConst (b, _)) => fast_string_ord (a, b)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   120
    | (ZVar (xi, _), ZVar (yj, _)) => Term_Ord.fast_indexname_ord (xi, yj)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   121
    | (ZBound i, ZBound j) => int_ord (i, j)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   122
    | (ZClass (_, a), ZClass (_, b)) => fast_string_ord (a, b)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   123
    | _ => EQUAL);
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   124
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   125
fun types_ord tu =
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   126
  if pointer_eq tu then EQUAL
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   127
  else
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   128
    (case tu of
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   129
      (ZAbs (_, T, t), ZAbs (_, U, u)) =>
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   130
        (case ztyp_ord (T, U) of EQUAL => types_ord (t, u) | ord => ord)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   131
    | (ZApp (t1, t2), ZApp (u1, u2)) =>
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   132
        (case types_ord (t1, u1) of EQUAL => types_ord (t2, u2) | ord => ord)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   133
    | (ZConst1 (_, T), ZConst1 (_, U)) => ztyp_ord (T, U)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   134
    | (ZConst (_, Ts), ZConst (_, Us)) => dict_ord ztyp_ord (Ts, Us)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   135
    | (ZVar (_, T), ZVar (_, U)) => ztyp_ord (T, U)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   136
    | (ZClass (T, _), ZClass (U, _)) => ztyp_ord (T, U)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   137
    | _ => EQUAL);
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   138
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   139
in
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   140
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   141
val fast_zterm_ord = struct_ord ||| atoms_ord ||| types_ord;
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   142
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   143
end;
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   144
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   145
fun aconv_zterm (tm1, tm2) =
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   146
  pointer_eq (tm1, tm2) orelse
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   147
    (case (tm1, tm2) of
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   148
      (ZApp (t1, u1), ZApp (t2, u2)) => aconv_zterm (t1, t2) andalso aconv_zterm (u1, u2)
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   149
    | (ZAbs (_, T1, t1), ZAbs (_, T2, t2)) => aconv_zterm (t1, t2) andalso T1 = T2
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   150
    | (a1, a2) => a1 = a2);
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   151
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   152
end;
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   153
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   154
79264
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   155
(* tables and term items *)
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   156
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
   157
structure ZTypes = Table(type key = ztyp val ord = ZTerm.ztyp_ord);
79264
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   158
structure ZTerms = Table(type key = zterm val ord = ZTerm.fast_zterm_ord);
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
   159
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   160
structure ZTVars:
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   161
sig
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   162
  include TERM_ITEMS
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   163
  val add_tvarsT: ztyp -> set -> set
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   164
  val add_tvars: zterm -> set -> set
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   165
end =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   166
struct
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   167
  open TVars;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   168
  val add_tvarsT = ZTerm.fold_tvars add_set;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   169
  val add_tvars = ZTerm.fold_types add_tvarsT;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   170
end;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   171
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   172
structure ZVars:
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   173
sig
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   174
  include TERM_ITEMS
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   175
  val add_vars: zterm -> set -> set
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   176
end =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   177
struct
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   178
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   179
structure Term_Items = Term_Items
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   180
(
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   181
  type key = indexname * ztyp;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   182
  val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord ZTerm.ztyp_ord);
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   183
);
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   184
open Term_Items;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   185
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   186
val add_vars = ZTerm.fold_aterms (fn ZVar v => add_set v | _ => I);
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   187
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   188
end;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   189
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   190
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   191
(* proofs *)
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   192
79126
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
   193
datatype zproof_name =
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
   194
    ZAxiom of string
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
   195
  | ZOracle of string
79313
3b03af5125de use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents: 79312
diff changeset
   196
  | ZBox of serial
79361
c28d4d1986f1 tuned signature;
wenzelm
parents: 79360
diff changeset
   197
  | ZThm of {theory_name: string, thm_name: Thm_Name.T * Position.T, serial: int};
79126
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
   198
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   199
datatype zproof =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   200
    ZDummy                         (*dummy proof*)
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   201
  | ZConstp of (zproof_name * zterm) * (ztyp ZTVars.table * zterm ZVars.table)
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   202
  | ZBoundp of int
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   203
  | ZAbst of string * ztyp * zproof
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   204
  | ZAbsp of string * zterm * zproof
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   205
  | ZAppt of zproof * zterm
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   206
  | ZAppp of zproof * zproof
79476
wenzelm
parents: 79475
diff changeset
   207
  | ZHyp of zterm
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   208
  | ZClassp of ztyp * class;       (*OFCLASS proof from sorts algebra*)
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   209
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   210
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   211
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   212
(*** local ***)
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   213
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   214
signature ZTERM =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   215
sig
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   216
  datatype ztyp = datatype ztyp
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   217
  datatype zterm = datatype zterm
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   218
  datatype zproof = datatype zproof
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   219
  exception ZTERM of string * ztyp list * zterm list * zproof list
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   220
  type zproof_const = (zproof_name * zterm) * (ztyp ZTVars.table * zterm ZVars.table)
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   221
  val fold_tvars: (indexname * sort -> 'a -> 'a) -> ztyp -> 'a -> 'a
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   222
  val fold_aterms: (zterm -> 'a -> 'a) -> zterm -> 'a -> 'a
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   223
  val fold_types: (ztyp -> 'a -> 'a) -> zterm -> 'a -> 'a
79264
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   224
  val ztyp_ord: ztyp ord
07b93dee105f more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents: 79263
diff changeset
   225
  val fast_zterm_ord: zterm ord
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   226
  val aconv_zterm: zterm * zterm -> bool
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   227
  val ZAbsts: (string * ztyp) list -> zproof -> zproof
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   228
  val ZAbsps: zterm list -> zproof -> zproof
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   229
  val ZAppts: zproof * zterm list -> zproof
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   230
  val ZAppps: zproof * zproof list -> zproof
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   231
  val incr_bv_same: int -> int -> zterm Same.operation
79283
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   232
  val incr_proof_bv_same: int -> int -> int -> int -> zproof Same.operation
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   233
  val incr_bv: int -> int -> zterm -> zterm
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   234
  val incr_boundvars: int -> zterm -> zterm
79283
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   235
  val incr_proof_bv: int -> int -> int -> int -> zproof -> zproof
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   236
  val incr_proof_boundvars: int -> int -> zproof -> zproof
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   237
  val subst_term_bound_same: zterm -> int -> zterm Same.operation
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   238
  val subst_proof_bound_same: zterm -> int -> zproof Same.operation
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   239
  val subst_proof_boundp_same: zproof -> int -> int -> zproof Same.operation
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   240
  val subst_term_bound: zterm -> zterm -> zterm
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   241
  val subst_proof_bound: zterm -> zproof -> zproof
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   242
  val subst_proof_boundp: zproof -> zproof -> zproof
79388
e6a12ea61f83 more operations;
wenzelm
parents: 79361
diff changeset
   243
  val subst_type_same: (indexname * sort, ztyp) Same.function -> ztyp Same.operation
e6a12ea61f83 more operations;
wenzelm
parents: 79361
diff changeset
   244
  val subst_term_same:
e6a12ea61f83 more operations;
wenzelm
parents: 79361
diff changeset
   245
    ztyp Same.operation -> (indexname * ztyp, zterm) Same.function -> zterm Same.operation
79318
74e245625a67 more informative exception;
wenzelm
parents: 79315
diff changeset
   246
  exception BAD_INST of ((indexname * ztyp) * zterm) list
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
   247
  val map_proof: {hyps: bool} -> ztyp Same.operation -> zterm Same.operation -> zproof -> zproof
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
   248
  val map_proof_types: {hyps: bool} -> ztyp Same.operation -> zproof -> zproof
79414
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   249
  val map_class_proof: (ztyp * class, zproof) Same.function -> zproof -> zproof
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   250
  val ztyp_of: typ -> ztyp
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   251
  val zterm_cache: theory -> {zterm: term -> zterm, ztyp: typ -> ztyp}
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   252
  val zterm_of: theory -> term -> zterm
79477
4c719b31a0c2 minor performance tuning;
wenzelm
parents: 79476
diff changeset
   253
  val typ_of_tvar: indexname * sort -> typ
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   254
  val typ_of: ztyp -> typ
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   255
  val term_of: theory -> zterm -> term
79298
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   256
  val beta_norm_term_same: zterm Same.operation
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   257
  val beta_norm_proof_same: zproof Same.operation
79302
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   258
  val beta_norm_prooft_same: zproof -> zproof
79298
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   259
  val beta_norm_term: zterm -> zterm
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   260
  val beta_norm_proof: zproof -> zproof
79302
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   261
  val beta_norm_prooft: zproof -> zproof
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   262
  val norm_type: Type.tyenv -> ztyp -> ztyp
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   263
  val norm_term: theory -> Envir.env -> zterm -> zterm
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   264
  val norm_proof: theory -> Envir.env -> term list -> zproof -> zproof
79311
e4a9a861856b omit pointless future: proof terms are built sequentially;
wenzelm
parents: 79303
diff changeset
   265
  type zbox = serial * (zterm * zproof)
79279
d9a7ee1bd070 minor performance tuning: more concise union;
wenzelm
parents: 79277
diff changeset
   266
  val zbox_ord: zbox ord
d9a7ee1bd070 minor performance tuning: more concise union;
wenzelm
parents: 79277
diff changeset
   267
  type zboxes = zbox Ord_List.T
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   268
  val union_zboxes: zboxes -> zboxes -> zboxes
79279
d9a7ee1bd070 minor performance tuning: more concise union;
wenzelm
parents: 79277
diff changeset
   269
  val unions_zboxes: zboxes list -> zboxes
79425
0875c87b4a4b clarified signature;
wenzelm
parents: 79422
diff changeset
   270
  val add_box_proof: {unconstrain: bool} -> theory ->
0875c87b4a4b clarified signature;
wenzelm
parents: 79422
diff changeset
   271
    term list -> term -> zproof -> zboxes -> zproof * zboxes
79329
992c494bda25 proper thm_name for stored zproof;
wenzelm
parents: 79326
diff changeset
   272
  val thm_proof: theory -> Thm_Name.T * Position.T -> term list -> term -> zproof -> zbox * zproof
79126
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
   273
  val axiom_proof:  theory -> string -> term -> zproof
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
   274
  val oracle_proof:  theory -> string -> term -> zproof
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   275
  val assume_proof: theory -> term -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   276
  val trivial_proof: theory -> term -> zproof
79414
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   277
  val implies_intr_proof': zterm -> zproof -> zproof
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   278
  val implies_intr_proof: theory -> term -> zproof -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   279
  val forall_intr_proof: theory -> typ -> string * term -> zproof -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   280
  val forall_elim_proof: theory -> term -> zproof -> zproof
79128
b6f5d4392388 more zproofs;
wenzelm
parents: 79126
diff changeset
   281
  val of_class_proof: typ * class -> zproof
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   282
  val reflexive_proof: theory -> typ -> term -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   283
  val symmetric_proof: theory -> typ -> term -> term -> zproof -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   284
  val transitive_proof: theory -> typ -> term -> term -> term -> zproof -> zproof -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   285
  val equal_intr_proof: theory -> term -> term -> zproof -> zproof -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   286
  val equal_elim_proof: theory -> term -> term -> zproof -> zproof -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   287
  val abstract_rule_proof: theory -> typ -> typ -> string * term -> term -> term -> zproof -> zproof
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   288
  val combination_proof: theory -> typ -> typ -> term -> term -> term -> term ->
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   289
    zproof -> zproof -> zproof
79133
cfe995369655 more zproofs;
wenzelm
parents: 79132
diff changeset
   290
  val generalize_proof: Names.set * Names.set -> int -> zproof -> zproof
79149
810679c5ed3c more zproofs;
wenzelm
parents: 79148
diff changeset
   291
  val instantiate_proof: theory ->
810679c5ed3c more zproofs;
wenzelm
parents: 79148
diff changeset
   292
    ((indexname * sort) * typ) list * ((indexname * typ) * term) list -> zproof -> zproof
79135
db2dc7634d62 more zproofs;
wenzelm
parents: 79133
diff changeset
   293
  val varifyT_proof: ((string * sort) * (indexname * sort)) list -> zproof -> zproof
79152
4189e10f1524 more zproofs;
wenzelm
parents: 79150
diff changeset
   294
  val legacy_freezeT_proof: term -> zproof -> zproof
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
   295
  val rotate_proof: theory -> term list -> term -> (string * typ) list ->
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
   296
    term list -> int -> zproof -> zproof
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
   297
  val permute_prems_proof: theory -> term list -> int -> int -> zproof -> zproof
79234
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
   298
  val incr_indexes_proof: int -> zproof -> zproof
79237
f97fe7ad62a7 proper ZTerm.lift_proof (amending 4a1a25bdf81d);
wenzelm
parents: 79234
diff changeset
   299
  val lift_proof: theory -> term -> int -> term list -> zproof -> zproof
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   300
  val assumption_proof: theory -> Envir.env -> term list -> term -> int -> term list ->
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   301
    zproof -> zproof
79261
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
   302
  val bicompose_proof: theory -> Envir.env -> int -> bool -> term list -> term list ->
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   303
    term option -> term list -> int -> int -> term list -> zproof -> zproof -> zproof
79425
0875c87b4a4b clarified signature;
wenzelm
parents: 79422
diff changeset
   304
  type sorts_proof = (class * class -> zproof) * (string * class list list * class -> zproof)
0875c87b4a4b clarified signature;
wenzelm
parents: 79422
diff changeset
   305
  val of_sort_proof: Sorts.algebra -> sorts_proof -> (typ * class -> zproof) ->
79405
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
   306
    typ * sort -> zproof list
79425
0875c87b4a4b clarified signature;
wenzelm
parents: 79422
diff changeset
   307
  val unconstrainT_proof: theory -> sorts_proof -> Logic.unconstrain_context -> zproof -> zproof
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   308
end;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   309
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   310
structure ZTerm: ZTERM =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   311
struct
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   312
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   313
datatype ztyp = datatype ztyp;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   314
datatype zterm = datatype zterm;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   315
datatype zproof = datatype zproof;
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   316
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   317
exception ZTERM of string * ztyp list * zterm list * zproof list;
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   318
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   319
open ZTerm;
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   320
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   321
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
   322
(* derived operations *)
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
   323
79234
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
   324
val ZFuns = Library.foldr ZFun;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
   325
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   326
val ZAbsts = fold_rev (fn (x, T) => fn prf => ZAbst (x, T, prf));
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   327
val ZAbsps = fold_rev (fn t => fn prf => ZAbsp ("H", t, prf));
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
   328
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   329
val ZAppts = Library.foldl ZAppt;
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   330
val ZAppps = Library.foldl ZAppp;
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
   331
79234
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
   332
fun combound (t, n, k) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
   333
  if k > 0 then ZApp (combound (t, n + 1, k - 1), ZBound n) else t;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
   334
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
   335
79283
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   336
(* increment bound variables *)
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   337
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   338
fun incr_bv_same inc =
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   339
  if inc = 0 then fn _ => Same.same
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   340
  else
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   341
    let
79285
3c542c1087f1 tuned whitespace;
wenzelm
parents: 79283
diff changeset
   342
      fun term lev (ZBound i) =
3c542c1087f1 tuned whitespace;
wenzelm
parents: 79283
diff changeset
   343
            if i >= lev then ZBound (i + inc) else raise Same.SAME
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   344
        | term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   345
        | term lev (ZApp (t, u)) =
79285
3c542c1087f1 tuned whitespace;
wenzelm
parents: 79283
diff changeset
   346
            (ZApp (term lev t, Same.commit (term lev) u)
3c542c1087f1 tuned whitespace;
wenzelm
parents: 79283
diff changeset
   347
              handle Same.SAME => ZApp (t, term lev u))
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   348
        | term _ _ = raise Same.SAME;
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   349
    in term end;
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   350
79283
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   351
fun incr_proof_bv_same incp inct =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   352
  if incp = 0 andalso inct = 0 then fn _ => fn _ => Same.same
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   353
  else
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   354
    let
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   355
      val term = incr_bv_same inct;
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   356
79283
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   357
      fun proof plev _ (ZBoundp i) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   358
            if i >= plev then ZBoundp (i + incp) else raise Same.SAME
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   359
        | proof plev tlev (ZAbsp (a, t, p)) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   360
            (ZAbsp (a, term tlev t, Same.commit (proof (plev + 1) tlev) p)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   361
              handle Same.SAME => ZAbsp (a, t, proof (plev + 1) tlev p))
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   362
        | proof plev tlev (ZAbst (a, T, p)) = ZAbst (a, T, proof plev (tlev + 1) p)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   363
        | proof plev tlev (ZAppp (p, q)) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   364
            (ZAppp (proof plev tlev p, Same.commit (proof plev tlev) q)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   365
              handle Same.SAME => ZAppp (p, proof plev tlev q))
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   366
        | proof plev tlev (ZAppt (p, t)) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   367
            (ZAppt (proof plev tlev p, Same.commit (term tlev) t)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   368
              handle Same.SAME => ZAppt (p, term tlev t))
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   369
        | proof _ _ _ = raise Same.SAME;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   370
    in proof end;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   371
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   372
fun incr_bv inc lev = Same.commit (incr_bv_same inc lev);
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   373
fun incr_boundvars inc = incr_bv inc 0;
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   374
79283
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   375
fun incr_proof_bv incp inct plev tlev = Same.commit (incr_proof_bv_same incp inct plev tlev);
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   376
fun incr_proof_boundvars incp inct = incr_proof_bv incp inct 0 0;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   377
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   378
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   379
(* substitution of bound variables *)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   380
79281
28342f38da5c clarified signature, following Term.subst_bounds_same;
wenzelm
parents: 79279
diff changeset
   381
fun subst_term_bound_same arg =
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   382
  let
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   383
    fun term lev (ZBound i) =
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   384
          if i < lev then raise Same.SAME   (*var is locally bound*)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   385
          else if i = lev then incr_boundvars lev arg
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   386
          else ZBound (i - 1)   (*loose: change it*)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   387
      | term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   388
      | term lev (ZApp (t, u)) =
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   389
          (ZApp (term lev t, Same.commit (term lev) u)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   390
            handle Same.SAME => ZApp (t, term lev u))
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   391
      | term _ _ = raise Same.SAME;
79281
28342f38da5c clarified signature, following Term.subst_bounds_same;
wenzelm
parents: 79279
diff changeset
   392
  in term end;
28342f38da5c clarified signature, following Term.subst_bounds_same;
wenzelm
parents: 79279
diff changeset
   393
79283
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   394
fun subst_proof_bound_same arg =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   395
  let
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   396
    val term = subst_term_bound_same arg;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   397
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   398
    fun proof lev (ZAbsp (a, t, p)) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   399
          (ZAbsp (a, term lev t, Same.commit (proof lev) p)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   400
            handle Same.SAME => ZAbsp (a, t, proof lev p))
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   401
      | proof lev (ZAbst (a, T, p)) = ZAbst (a, T, proof (lev + 1) p)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   402
      | proof lev (ZAppp (p, q)) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   403
          (ZAppp (proof lev p, Same.commit (proof lev) q)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   404
            handle Same.SAME => ZAppp (p, proof lev q))
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   405
      | proof lev (ZAppt (p, t)) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   406
          (ZAppt (proof lev p, Same.commit (term lev) t)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   407
            handle Same.SAME => ZAppt (p, term lev t))
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   408
      | proof _ _ = raise Same.SAME;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   409
  in proof end;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   410
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   411
fun subst_proof_boundp_same arg =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   412
  let
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   413
    fun proof plev tlev (ZBoundp i) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   414
          if i < plev then raise Same.SAME   (*var is locally bound*)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   415
          else if i = plev then incr_proof_boundvars plev tlev arg
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   416
          else ZBoundp (i - 1)   (*loose: change it*)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   417
      | proof plev tlev (ZAbsp (a, t, p)) = ZAbsp (a, t, proof (plev + 1) tlev p)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   418
      | proof plev tlev (ZAbst (a, T, p)) = ZAbst (a, T, proof plev (tlev + 1) p)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   419
      | proof plev tlev (ZAppp (p, q)) =
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   420
          (ZAppp (proof plev tlev p, Same.commit (proof plev tlev) q)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   421
            handle Same.SAME => ZAppp (p, proof plev tlev q))
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   422
      | proof plev tlev (ZAppt (p, t)) = ZAppt (proof plev tlev p, t)
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   423
      | proof _ _ _ = raise Same.SAME;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   424
  in proof end;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   425
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   426
fun subst_term_bound arg body = Same.commit (subst_term_bound_same arg 0) body;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   427
fun subst_proof_bound arg body = Same.commit (subst_proof_bound_same arg 0) body;
2c5d4b4ea3a2 more operations, following proofterm.ML;
wenzelm
parents: 79281
diff changeset
   428
fun subst_proof_boundp arg body = Same.commit (subst_proof_boundp_same arg 0 0) body;
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   429
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   430
79268
154166613b40 tuned comments;
wenzelm
parents: 79266
diff changeset
   431
(* substitution of free or schematic variables *)
79132
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   432
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   433
fun subst_type_same tvar =
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   434
  let
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   435
    fun typ (ZTVar x) = tvar x
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   436
      | typ (ZFun (T, U)) =
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   437
          (ZFun (typ T, Same.commit typ U)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   438
            handle Same.SAME => ZFun (T, typ U))
79132
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   439
      | typ ZProp = raise Same.SAME
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   440
      | typ (ZType0 _) = raise Same.SAME
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   441
      | typ (ZType1 (a, T)) = ZType1 (a, typ T)
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   442
      | typ (ZType (a, Ts)) = ZType (a, Same.map typ Ts);
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   443
  in typ end;
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   444
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   445
fun subst_term_same typ var =
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   446
  let
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   447
    fun term (ZVar (x, T)) =
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   448
          let val (T', same) = Same.commit_id typ T in
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   449
            (case Same.catch var (x, T') of
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   450
              NONE => if same then raise Same.SAME else ZVar (x, T')
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   451
            | SOME t' => t')
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   452
          end
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   453
      | term (ZBound _) = raise Same.SAME
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   454
      | term (ZConst0 _) = raise Same.SAME
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   455
      | term (ZConst1 (a, T)) = ZConst1 (a, typ T)
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   456
      | term (ZConst (a, Ts)) = ZConst (a, Same.map typ Ts)
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   457
      | term (ZAbs (a, T, t)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   458
          (ZAbs (a, typ T, Same.commit term t)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   459
            handle Same.SAME => ZAbs (a, T, term t))
79132
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   460
      | term (ZApp (t, u)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   461
          (ZApp (term t, Same.commit term u)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   462
            handle Same.SAME => ZApp (t, term u))
79132
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   463
      | term (ZClass (T, c)) = ZClass (typ T, c);
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   464
  in term end;
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   465
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   466
fun instantiate_type_same instT =
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   467
  if ZTVars.is_empty instT then Same.same
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   468
  else ZTypes.unsynchronized_cache (subst_type_same (Same.function (ZTVars.lookup instT)));
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   469
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   470
fun instantiate_term_same typ inst =
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   471
  subst_term_same typ (Same.function (ZVars.lookup inst));
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   472
79268
154166613b40 tuned comments;
wenzelm
parents: 79266
diff changeset
   473
79475
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   474
(* fold types/terms within proof *)
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   475
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   476
fun fold_proof {hyps} typ term =
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   477
  let
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   478
    fun proof ZDummy = I
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   479
      | proof (ZConstp (_, (instT, inst))) =
79475
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   480
          ZTVars.fold (typ o #2) instT #> ZVars.fold (term o #2) inst
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   481
      | proof (ZBoundp _) = I
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   482
      | proof (ZAbst (_, T, p)) = typ T #> proof p
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   483
      | proof (ZAbsp (_, t, p)) = term t #> proof p
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   484
      | proof (ZAppt (p, t)) = proof p #> term t
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   485
      | proof (ZAppp (p, q)) = proof p #> proof q
79476
wenzelm
parents: 79475
diff changeset
   486
      | proof (ZHyp h) = hyps ? term h
79475
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   487
      | proof (ZClassp (T, _)) = hyps ? typ T;
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   488
  in proof end;
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   489
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   490
fun fold_proof_types hyps typ =
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   491
  fold_proof hyps typ (fold_types typ);
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   492
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   493
79268
154166613b40 tuned comments;
wenzelm
parents: 79266
diff changeset
   494
(* map types/terms within proof *)
154166613b40 tuned comments;
wenzelm
parents: 79266
diff changeset
   495
79318
74e245625a67 more informative exception;
wenzelm
parents: 79315
diff changeset
   496
exception BAD_INST of ((indexname * ztyp) * zterm) list
74e245625a67 more informative exception;
wenzelm
parents: 79315
diff changeset
   497
74e245625a67 more informative exception;
wenzelm
parents: 79315
diff changeset
   498
fun make_inst inst =
79320
bbac3e8a5070 more permissive: allow collapse of term variables for equal results, e.g. relevant for metis (line 1882 of "~~/src/HOL/List.thy");
wenzelm
parents: 79318
diff changeset
   499
  ZVars.build (inst |> fold (ZVars.insert (op aconv_zterm)))
bbac3e8a5070 more permissive: allow collapse of term variables for equal results, e.g. relevant for metis (line 1882 of "~~/src/HOL/List.thy");
wenzelm
parents: 79318
diff changeset
   500
    handle ZVars.DUP _ => raise BAD_INST inst;
79318
74e245625a67 more informative exception;
wenzelm
parents: 79315
diff changeset
   501
79145
a9c55fef42b0 tuned signature;
wenzelm
parents: 79144
diff changeset
   502
fun map_insts_same typ term (instT, inst) =
a9c55fef42b0 tuned signature;
wenzelm
parents: 79144
diff changeset
   503
  let
a9c55fef42b0 tuned signature;
wenzelm
parents: 79144
diff changeset
   504
    val changed = Unsynchronized.ref false;
79146
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   505
    fun apply f x =
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   506
      (case Same.catch f x of
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   507
        NONE => NONE
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   508
      | some => (changed := true; some));
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   509
79145
a9c55fef42b0 tuned signature;
wenzelm
parents: 79144
diff changeset
   510
    val instT' =
a9c55fef42b0 tuned signature;
wenzelm
parents: 79144
diff changeset
   511
      (instT, instT) |-> ZTVars.fold (fn (v, T) =>
79146
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   512
        (case apply typ T of
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   513
          NONE => I
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   514
        | SOME T' => ZTVars.update (v, T')));
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   515
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   516
    val vars' =
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   517
      (inst, ZVars.empty) |-> ZVars.fold (fn ((v, T), _) =>
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   518
        (case apply typ T of
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   519
          NONE => I
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   520
        | SOME T' => ZVars.add ((v, T), (v, T'))));
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   521
79145
a9c55fef42b0 tuned signature;
wenzelm
parents: 79144
diff changeset
   522
    val inst' =
79146
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   523
      if ZVars.is_empty vars' then
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   524
        (inst, inst) |-> ZVars.fold (fn (v, t) =>
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   525
          (case apply term t of
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   526
            NONE => I
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   527
          | SOME t' => ZVars.update (v, t')))
feb94ac5df41 more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents: 79145
diff changeset
   528
      else
79325
wenzelm
parents: 79320
diff changeset
   529
        build (inst |> ZVars.fold_rev (fn (v, t) =>
wenzelm
parents: 79320
diff changeset
   530
          cons (the_default v (ZVars.lookup vars' v), the_default t (apply term t))))
79318
74e245625a67 more informative exception;
wenzelm
parents: 79315
diff changeset
   531
        |> make_inst;
79145
a9c55fef42b0 tuned signature;
wenzelm
parents: 79144
diff changeset
   532
  in if ! changed then (instT', inst') else raise Same.SAME end;
a9c55fef42b0 tuned signature;
wenzelm
parents: 79144
diff changeset
   533
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
   534
fun map_proof_same {hyps} typ term =
79132
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   535
  let
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   536
    fun proof ZDummy = raise Same.SAME
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   537
      | proof (ZConstp (a, (instT, inst))) =
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   538
          ZConstp (a, map_insts_same typ term (instT, inst))
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   539
      | proof (ZBoundp _) = raise Same.SAME
79132
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   540
      | proof (ZAbst (a, T, p)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   541
          (ZAbst (a, typ T, Same.commit proof p)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   542
            handle Same.SAME => ZAbst (a, T, proof p))
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   543
      | proof (ZAbsp (a, t, p)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   544
          (ZAbsp (a, term t, Same.commit proof p)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   545
            handle Same.SAME => ZAbsp (a, t, proof p))
79132
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   546
      | proof (ZAppt (p, t)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   547
          (ZAppt (proof p, Same.commit term t)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   548
            handle Same.SAME => ZAppt (p, term t))
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   549
      | proof (ZAppp (p, q)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   550
          (ZAppp (proof p, Same.commit proof q)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   551
            handle Same.SAME => ZAppp (p, proof q))
79476
wenzelm
parents: 79475
diff changeset
   552
      | proof (ZHyp h) = if hyps then ZHyp (term h) else raise Same.SAME
79474
c39aed404ffc more uniform treatment of "hyps" within zproof;
wenzelm
parents: 79473
diff changeset
   553
      | proof (ZClassp (T, c)) = if hyps then ZClassp (typ T, c) else raise Same.SAME;
79132
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   554
  in proof end;
6d3322477cfd more operations;
wenzelm
parents: 79128
diff changeset
   555
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
   556
fun map_proof hyps typ term = Same.commit (map_proof_same hyps typ term);
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
   557
fun map_proof_types hyps typ = map_proof hyps typ (subst_term_same typ Same.same);
79144
wenzelm
parents: 79136
diff changeset
   558
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   559
79414
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   560
(* map class proofs *)
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   561
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   562
fun map_class_proof class =
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   563
  let
79415
740ec03b0b71 tuned names;
wenzelm
parents: 79414
diff changeset
   564
    fun proof (ZClassp C) = class C
79414
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   565
      | proof (ZAbst (a, T, p)) = ZAbst (a, T, proof p)
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   566
      | proof (ZAbsp (a, t, p)) = ZAbsp (a, t, proof p)
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   567
      | proof (ZAppt (p, t)) = ZAppt (proof p, t)
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   568
      | proof (ZAppp (p, q)) =
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   569
          (ZAppp (proof p, Same.commit proof q)
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   570
            handle Same.SAME => ZAppp (p, proof q))
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   571
      | proof _ = raise Same.SAME;
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   572
  in Same.commit proof end;
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   573
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   574
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   575
(* convert ztyp / zterm vs. regular typ / term *)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   576
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   577
fun ztyp_of (TFree (a, S)) = ZTVar ((a, ~1), S)
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   578
  | ztyp_of (TVar v) = ZTVar v
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   579
  | ztyp_of (Type ("fun", [T, U])) = ZFun (ztyp_of T, ztyp_of U)
79421
wenzelm
parents: 79420
diff changeset
   580
  | ztyp_of (Type ("prop", [])) = ZProp
wenzelm
parents: 79420
diff changeset
   581
  | ztyp_of (Type (c, [])) = ZType0 c
79420
4c3346b4b100 clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
wenzelm
parents: 79419
diff changeset
   582
  | ztyp_of (Type (c, [T])) = ZType1 (c, ztyp_of T)
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   583
  | ztyp_of (Type (c, ts)) = ZType (c, map ztyp_of ts);
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   584
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
   585
fun ztyp_cache () = Typtab.unsynchronized_cache ztyp_of;
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
   586
79226
0b87e04d0b68 tuned signature;
wenzelm
parents: 79214
diff changeset
   587
fun zterm_cache thy =
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   588
  let
79226
0b87e04d0b68 tuned signature;
wenzelm
parents: 79214
diff changeset
   589
    val typargs = Consts.typargs (Sign.consts_of thy);
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   590
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
   591
    val ztyp = ztyp_cache ();
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   592
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   593
    fun zterm (Free (x, T)) = ZVar ((x, ~1), ztyp T)
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   594
      | zterm (Var (xi, T)) = ZVar (xi, ztyp T)
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   595
      | zterm (Bound i) = ZBound i
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   596
      | zterm (Const (c, T)) =
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   597
          (case typargs (c, T) of
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   598
            [] => ZConst0 c
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   599
          | [T] => ZConst1 (c, ztyp T)
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   600
          | Ts => ZConst (c, map ztyp Ts))
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   601
      | zterm (Abs (a, T, b)) = ZAbs (a, ztyp T, zterm b)
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   602
      | zterm ((t as Const (c, _)) $ (u as Const ("Pure.type", _))) =
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   603
          if String.isSuffix Logic.class_suffix c then
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   604
            ZClass (ztyp (Logic.dest_type u), Logic.class_of_const c)
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   605
          else ZApp (zterm t, zterm u)
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   606
      | zterm (t $ u) = ZApp (zterm t, zterm u);
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   607
  in {ztyp = ztyp, zterm = zterm} end;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   608
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   609
val zterm_of = #zterm o zterm_cache;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   610
79477
4c719b31a0c2 minor performance tuning;
wenzelm
parents: 79476
diff changeset
   611
fun typ_of_tvar ((a, ~1), S) = TFree (a, S)
4c719b31a0c2 minor performance tuning;
wenzelm
parents: 79476
diff changeset
   612
  | typ_of_tvar v = TVar v;
4c719b31a0c2 minor performance tuning;
wenzelm
parents: 79476
diff changeset
   613
4c719b31a0c2 minor performance tuning;
wenzelm
parents: 79476
diff changeset
   614
fun typ_of (ZTVar v) = typ_of_tvar v
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   615
  | typ_of (ZFun (T, U)) = typ_of T --> typ_of U
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   616
  | typ_of ZProp = propT
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   617
  | typ_of (ZType0 c) = Type (c, [])
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   618
  | typ_of (ZType1 (c, T)) = Type (c, [typ_of T])
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   619
  | typ_of (ZType (c, Ts)) = Type (c, map typ_of Ts);
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   620
79201
wenzelm
parents: 79200
diff changeset
   621
fun typ_cache () = ZTypes.unsynchronized_cache typ_of;
wenzelm
parents: 79200
diff changeset
   622
79226
0b87e04d0b68 tuned signature;
wenzelm
parents: 79214
diff changeset
   623
fun term_of thy =
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   624
  let
79226
0b87e04d0b68 tuned signature;
wenzelm
parents: 79214
diff changeset
   625
    val instance = Consts.instance (Sign.consts_of thy);
0b87e04d0b68 tuned signature;
wenzelm
parents: 79214
diff changeset
   626
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   627
    fun const (c, Ts) = Const (c, instance (c, Ts));
79226
0b87e04d0b68 tuned signature;
wenzelm
parents: 79214
diff changeset
   628
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   629
    fun term (ZVar ((x, ~1), T)) = Free (x, typ_of T)
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   630
      | term (ZVar (xi, T)) = Var (xi, typ_of T)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   631
      | term (ZBound i) = Bound i
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   632
      | term (ZConst0 c) = const (c, [])
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   633
      | term (ZConst1 (c, T)) = const (c, [typ_of T])
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   634
      | term (ZConst (c, Ts)) = const (c, map typ_of Ts)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   635
      | term (ZAbs (a, T, b)) = Abs (a, typ_of T, term b)
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   636
      | term (ZApp (t, u)) = term t $ term u
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   637
      | term (ZClass (T, c)) = Logic.mk_of_class (typ_of T, c);
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   638
  in term end;
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
   639
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   640
79298
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   641
(* beta contraction *)
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   642
79286
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   643
val beta_norm_term_same =
79271
b14b289caaf6 minor performance tuning: more direct beta_norm;
wenzelm
parents: 79270
diff changeset
   644
  let
79299
74a90157ee89 tuned, following Envir.norm_term;
wenzelm
parents: 79298
diff changeset
   645
    fun norm (ZAbs (a, T, t)) = ZAbs (a, T, Same.commit norm t)
74a90157ee89 tuned, following Envir.norm_term;
wenzelm
parents: 79298
diff changeset
   646
      | norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body)
79271
b14b289caaf6 minor performance tuning: more direct beta_norm;
wenzelm
parents: 79270
diff changeset
   647
      | norm (ZApp (f, t)) =
b14b289caaf6 minor performance tuning: more direct beta_norm;
wenzelm
parents: 79270
diff changeset
   648
          ((case norm f of
79286
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   649
             ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body)
79271
b14b289caaf6 minor performance tuning: more direct beta_norm;
wenzelm
parents: 79270
diff changeset
   650
           | nf => ZApp (nf, Same.commit norm t))
b14b289caaf6 minor performance tuning: more direct beta_norm;
wenzelm
parents: 79270
diff changeset
   651
          handle Same.SAME => ZApp (f, norm t))
b14b289caaf6 minor performance tuning: more direct beta_norm;
wenzelm
parents: 79270
diff changeset
   652
      | norm _ = raise Same.SAME;
b14b289caaf6 minor performance tuning: more direct beta_norm;
wenzelm
parents: 79270
diff changeset
   653
  in norm end;
b14b289caaf6 minor performance tuning: more direct beta_norm;
wenzelm
parents: 79270
diff changeset
   654
79302
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   655
val beta_norm_prooft_same =
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   656
  let
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   657
    val term = beta_norm_term_same;
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   658
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   659
    fun norm (ZAbst (a, T, p)) = ZAbst (a, T, norm p)
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   660
      | norm (ZAppt (ZAbst (_, _, body), t)) = Same.commit norm (subst_proof_bound t body)
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   661
      | norm (ZAppt (f, t)) =
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   662
          ((case norm f of
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   663
             ZAbst (_, _, body) => Same.commit norm (subst_proof_bound t body)
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   664
           | nf => ZAppt (nf, Same.commit term t))
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   665
          handle Same.SAME => ZAppt (f, term t))
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   666
      | norm _ = raise Same.SAME;
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   667
  in norm end;
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   668
79286
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   669
val beta_norm_proof_same =
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   670
  let
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   671
    val term = beta_norm_term_same;
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   672
79300
236fa4b32afb more thorough beta contraction, following Envir.norm_term;
wenzelm
parents: 79299
diff changeset
   673
    fun norm (ZAbst (a, T, p)) = ZAbst (a, T, norm p)
236fa4b32afb more thorough beta contraction, following Envir.norm_term;
wenzelm
parents: 79299
diff changeset
   674
      | norm (ZAbsp (a, t, p)) =
236fa4b32afb more thorough beta contraction, following Envir.norm_term;
wenzelm
parents: 79299
diff changeset
   675
          (ZAbsp (a, term t, Same.commit norm p)
236fa4b32afb more thorough beta contraction, following Envir.norm_term;
wenzelm
parents: 79299
diff changeset
   676
            handle Same.SAME => ZAbsp (a, t, norm p))
236fa4b32afb more thorough beta contraction, following Envir.norm_term;
wenzelm
parents: 79299
diff changeset
   677
      | norm (ZAppt (ZAbst (_, _, body), t)) = Same.commit norm (subst_proof_bound t body)
79301
wenzelm
parents: 79300
diff changeset
   678
      | norm (ZAppp (ZAbsp (_, _, body), p)) = Same.commit norm (subst_proof_boundp p body)
79286
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   679
      | norm (ZAppt (f, t)) =
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   680
          ((case norm f of
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   681
             ZAbst (_, _, body) => Same.commit norm (subst_proof_bound t body)
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   682
           | nf => ZAppt (nf, Same.commit term t))
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   683
          handle Same.SAME => ZAppt (f, term t))
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   684
      | norm (ZAppp (f, p)) =
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   685
          ((case norm f of
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   686
             ZAbsp (_, _, body) => Same.commit norm (subst_proof_boundp p body)
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   687
           | nf => ZAppp (nf, Same.commit norm p))
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   688
          handle Same.SAME => ZAppp (f, norm p))
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   689
      | norm _ = raise Same.SAME;
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   690
  in norm end;
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   691
79298
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   692
val beta_norm_term = Same.commit beta_norm_term_same;
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   693
val beta_norm_proof = Same.commit beta_norm_proof_same;
79302
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   694
val beta_norm_prooft = Same.commit beta_norm_prooft_same;
79298
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   695
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   696
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   697
(* normalization wrt. environment and beta contraction *)
77e4e69fd0e1 more operations;
wenzelm
parents: 79286
diff changeset
   698
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   699
fun norm_type_same ztyp tyenv =
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   700
  if Vartab.is_empty tyenv then Same.same
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   701
  else
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   702
    let
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   703
      fun norm (ZTVar v) =
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   704
            (case Type.lookup tyenv v of
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   705
              SOME U => Same.commit norm (ztyp U)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   706
            | NONE => raise Same.SAME)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   707
        | norm (ZFun (T, U)) =
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   708
            (ZFun (norm T, Same.commit norm U)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   709
              handle Same.SAME => ZFun (T, norm U))
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   710
        | norm ZProp = raise Same.SAME
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   711
        | norm (ZType0 _) = raise Same.SAME
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   712
        | norm (ZType1 (a, T)) = ZType1 (a, norm T)
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   713
        | norm (ZType (a, Ts)) = ZType (a, Same.map norm Ts);
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   714
    in norm end;
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   715
79269
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   716
fun norm_term_same {ztyp, zterm, typ} (envir as Envir.Envir {tyenv, tenv, ...}) =
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   717
  let
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   718
    val lookup =
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   719
      if Vartab.is_empty tenv then K NONE
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   720
      else ZVars.unsynchronized_cache (apsnd typ #> Envir.lookup envir #> Option.map zterm);
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   721
79269
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   722
    val normT = norm_type_same ztyp tyenv;
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   723
79269
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   724
    fun norm (ZVar (xi, T)) =
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   725
          (case lookup (xi, T) of
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   726
            SOME u => Same.commit norm u
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   727
          | NONE => ZVar (xi, normT T))
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   728
      | norm (ZBound _) = raise Same.SAME
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   729
      | norm (ZConst0 _) = raise Same.SAME
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   730
      | norm (ZConst1 (a, T)) = ZConst1 (a, normT T)
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   731
      | norm (ZConst (a, Ts)) = ZConst (a, Same.map normT Ts)
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   732
      | norm (ZAbs (a, T, t)) =
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   733
          (ZAbs (a, normT T, Same.commit norm t)
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   734
            handle Same.SAME => ZAbs (a, T, norm t))
79281
28342f38da5c clarified signature, following Term.subst_bounds_same;
wenzelm
parents: 79279
diff changeset
   735
      | norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body)
79269
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   736
      | norm (ZApp (f, t)) =
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   737
          ((case norm f of
79281
28342f38da5c clarified signature, following Term.subst_bounds_same;
wenzelm
parents: 79279
diff changeset
   738
             ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body)
79269
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   739
           | nf => ZApp (nf, Same.commit norm t))
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   740
          handle Same.SAME => ZApp (f, norm t))
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   741
      | norm _ = raise Same.SAME;
79286
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   742
  in fn t => if Envir.is_empty envir then beta_norm_term_same t else norm t end;
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   743
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   744
fun norm_proof_cache (cache as {ztyp, ...}) (envir as Envir.Envir {tyenv, ...}) =
79269
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   745
  let
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   746
    val norm_tvar = ZTVar #> Same.commit (norm_type_same ztyp tyenv);
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   747
    val norm_var = ZVar #> Same.commit (norm_term_same cache envir);
79269
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   748
  in
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   749
    fn visible => fn prf =>
79302
fed9791928bf less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents: 79301
diff changeset
   750
      if Envir.is_empty envir orelse null visible then beta_norm_prooft prf
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   751
      else
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   752
        let
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   753
          fun add_tvar v tab =
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   754
            if ZTVars.defined tab v then tab else ZTVars.update (v, norm_tvar v) tab;
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   755
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   756
          val inst_typ =
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   757
            if Vartab.is_empty tyenv then Same.same
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   758
            else
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   759
              ZTVars.build (visible |> (fold o Term.fold_types o Term.fold_atyps)
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   760
                (fn TVar v => add_tvar v | _ => I))
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   761
              |> instantiate_type_same;
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   762
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   763
          fun add_var (a, T) tab =
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   764
            let val v = (a, Same.commit inst_typ (ztyp T))
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   765
            in if ZVars.defined tab v then tab else ZVars.update (v, norm_var v) tab end;
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   766
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   767
          val inst_term =
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   768
            ZVars.build (visible |> (fold o Term.fold_aterms) (fn Var v => add_var v | _ => I))
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   769
            |> instantiate_term_same inst_typ;
79272
899f37f6d218 proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents: 79271
diff changeset
   770
79286
366a5ad2f2b3 more thorough beta contraction;
wenzelm
parents: 79285
diff changeset
   771
          val norm_term = Same.compose beta_norm_term_same inst_term;
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
   772
        in beta_norm_prooft (map_proof {hyps = false} inst_typ norm_term prf) end
79269
2c65d3356ef9 proper scope of cache (amending 61af3e917597);
wenzelm
parents: 79268
diff changeset
   773
  end;
79210
5ed6f16a5f7c clarified signature: support shared cache;
wenzelm
parents: 79205
diff changeset
   774
5ed6f16a5f7c clarified signature: support shared cache;
wenzelm
parents: 79205
diff changeset
   775
fun norm_cache thy =
79205
a159cb82afe7 more operations;
wenzelm
parents: 79204
diff changeset
   776
  let
79226
0b87e04d0b68 tuned signature;
wenzelm
parents: 79214
diff changeset
   777
    val {ztyp, zterm} = zterm_cache thy;
79205
a159cb82afe7 more operations;
wenzelm
parents: 79204
diff changeset
   778
    val typ = typ_cache ();
a159cb82afe7 more operations;
wenzelm
parents: 79204
diff changeset
   779
  in {ztyp = ztyp, zterm = zterm, typ = typ} end;
a159cb82afe7 more operations;
wenzelm
parents: 79204
diff changeset
   780
79214
61af3e917597 more operations;
wenzelm
parents: 79212
diff changeset
   781
fun norm_type tyenv = Same.commit (norm_type_same (ztyp_cache ()) tyenv);
61af3e917597 more operations;
wenzelm
parents: 79212
diff changeset
   782
fun norm_term thy envir = Same.commit (norm_term_same (norm_cache thy) envir);
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
   783
fun norm_proof thy envir = norm_proof_cache (norm_cache thy) envir;
79200
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   784
f6bbe80f5f41 more operations;
wenzelm
parents: 79163
diff changeset
   785
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   786
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   787
(** proof construction **)
79113
5109e4b2a292 pro-forma support for optional zproof: no proper content yet;
wenzelm
parents: 79098
diff changeset
   788
79161
3f532c76d0ad tuned structure;
wenzelm
parents: 79160
diff changeset
   789
(* constants *)
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   790
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   791
type zproof_const = (zproof_name * zterm) * (ztyp ZTVars.table * zterm ZVars.table);
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   792
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   793
fun zproof_const (a, A) : zproof_const =
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   794
  let
79154
e47db1e15a22 minor performance tuning;
wenzelm
parents: 79153
diff changeset
   795
    val instT =
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   796
      ZTVars.build (A |> (fold_types o fold_tvars) (fn v => fn tab =>
79154
e47db1e15a22 minor performance tuning;
wenzelm
parents: 79153
diff changeset
   797
        if ZTVars.defined tab v then tab else ZTVars.update (v, ZTVar v) tab));
e47db1e15a22 minor performance tuning;
wenzelm
parents: 79153
diff changeset
   798
    val inst =
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   799
      ZVars.build (A |> fold_aterms (fn t => fn tab =>
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   800
        (case t of
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   801
          ZVar v => if ZVars.defined tab v then tab else ZVars.update (v, t) tab
79154
e47db1e15a22 minor performance tuning;
wenzelm
parents: 79153
diff changeset
   802
        | _ => tab)));
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   803
  in ((a, A), (instT, inst)) end;
79126
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
   804
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   805
fun make_const_proof (f, g) (a, insts) =
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   806
  let
79417
a4eae462f224 proper support for complex types, not just type variables (amending 623789141e39);
wenzelm
parents: 79416
diff changeset
   807
    val typ = subst_type_same (Same.function (fn ((x, _), _) => try f x));
79416
623789141e39 proper instantiation for make_const_proof, notably change of types for term variables;
wenzelm
parents: 79415
diff changeset
   808
    val term = Same.function (fn ZVar ((x, _), _) => try g x | _ => NONE);
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   809
  in ZConstp (a, Same.commit (map_insts_same typ term) insts) end;
79161
3f532c76d0ad tuned structure;
wenzelm
parents: 79160
diff changeset
   810
3f532c76d0ad tuned structure;
wenzelm
parents: 79160
diff changeset
   811
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   812
(* closed proof boxes *)
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   813
79311
e4a9a861856b omit pointless future: proof terms are built sequentially;
wenzelm
parents: 79303
diff changeset
   814
type zbox = serial * (zterm * zproof);
79279
d9a7ee1bd070 minor performance tuning: more concise union;
wenzelm
parents: 79277
diff changeset
   815
val zbox_ord: zbox ord = fn ((i, _), (j, _)) => int_ord (j, i);
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   816
79279
d9a7ee1bd070 minor performance tuning: more concise union;
wenzelm
parents: 79277
diff changeset
   817
type zboxes = zbox Ord_List.T;
d9a7ee1bd070 minor performance tuning: more concise union;
wenzelm
parents: 79277
diff changeset
   818
val union_zboxes = Ord_List.union zbox_ord;
d9a7ee1bd070 minor performance tuning: more concise union;
wenzelm
parents: 79277
diff changeset
   819
val unions_zboxes = Ord_List.unions zbox_ord;
d9a7ee1bd070 minor performance tuning: more concise union;
wenzelm
parents: 79277
diff changeset
   820
val add_zboxes = Ord_List.insert zbox_ord;
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   821
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   822
local
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   823
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   824
fun close_prop prems prop =
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   825
  fold_rev (fn A => fn B => ZApp (ZApp (ZConst0 "Pure.imp", A), B)) prems prop;
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   826
79428
4cd892d1a676 omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents: 79427
diff changeset
   827
fun close_proof prems prf =
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   828
  let
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   829
    val m = length prems - 1;
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   830
    val bounds = ZTerms.build (prems |> fold_index (fn (i, h) => ZTerms.update (h, m - i)));
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   831
    fun get_bound lev t = ZTerms.lookup bounds t |> Option.map (fn i => ZBoundp (lev + i));
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   832
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   833
    fun proof lev (ZHyp t) =
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   834
          (case get_bound lev t of
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   835
            SOME p => p
79428
4cd892d1a676 omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents: 79427
diff changeset
   836
          | NONE => raise ZTERM ("Loose bound in proof term", [], [t], [prf]))
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   837
      | proof lev (ZClassp C) =
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   838
          (case get_bound lev (ZClass C) of
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   839
            SOME p => p
79428
4cd892d1a676 omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents: 79427
diff changeset
   840
          | NONE => raise Same.SAME)
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   841
      | proof lev (ZAbst (x, T, p)) = ZAbst (x, T, proof lev p)
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   842
      | proof lev (ZAbsp (x, t, p)) = ZAbsp (x, t, proof (lev + 1) p)
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   843
      | proof lev (ZAppt (p, t)) = ZAppt (proof lev p, t)
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   844
      | proof lev (ZAppp (p, q)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   845
          (ZAppp (proof lev p, Same.commit (proof lev) q)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   846
            handle Same.SAME => ZAppp (p, proof lev q))
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   847
      | proof _ _ = raise Same.SAME;
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   848
  in ZAbsps prems (Same.commit (proof 0) prf) end;
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   849
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   850
fun box_proof {unconstrain} zproof_name thy hyps concl prf =
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   851
  let
79428
4cd892d1a676 omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents: 79427
diff changeset
   852
    val {zterm, ...} = zterm_cache thy;
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   853
79477
4c719b31a0c2 minor performance tuning;
wenzelm
parents: 79476
diff changeset
   854
    val present_set_prf =
4c719b31a0c2 minor performance tuning;
wenzelm
parents: 79476
diff changeset
   855
      ZTVars.build ((fold_proof_types {hyps = true} o fold_tvars) ZTVars.add_set prf);
79475
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   856
    val present_set =
9eb9882f1845 more thorough treatment of hidden type variables within zproof;
wenzelm
parents: 79474
diff changeset
   857
      Types.build (Types.add_atyps concl #> fold Types.add_atyps hyps #>
79478
5c1451900bec clarified order, disregard structure of proof;
wenzelm
parents: 79477
diff changeset
   858
        ZTVars.fold (Types.add_set o typ_of_tvar o #1) present_set_prf);
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   859
    val ucontext as {constraints, outer_constraints, ...} =
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   860
      Logic.unconstrain_context [] present_set;
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   861
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   862
    val typ_operation = #typ_operation ucontext {strip_sorts = true};
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   863
    val unconstrain_typ = Same.commit typ_operation;
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   864
    val unconstrain_ztyp =
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   865
      ZTypes.unsynchronized_cache (Same.function_eq (op =) (typ_of #> unconstrain_typ #> ztyp_of));
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   866
    val unconstrain_zterm = zterm o Term.map_types typ_operation;
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   867
    val unconstrain_proof = Same.commit (map_proof_types {hyps = true} unconstrain_ztyp);
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   868
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   869
    val constrain_instT =
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   870
      if unconstrain then ZTVars.empty
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   871
      else
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   872
        ZTVars.build (present_set |> Types.fold (fn (T, _) =>
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   873
          let
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   874
            val ZTVar v = ztyp_of (unconstrain_typ T) and U = ztyp_of T;
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   875
            val equal = (case U of ZTVar u => u = v | _ => false);
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   876
          in not equal ? ZTVars.add (v, U) end));
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   877
    val constrain_proof =
79426
b5ba5b767444 minor performance tuning;
wenzelm
parents: 79425
diff changeset
   878
      if ZTVars.is_empty constrain_instT then I
b5ba5b767444 minor performance tuning;
wenzelm
parents: 79425
diff changeset
   879
      else
b5ba5b767444 minor performance tuning;
wenzelm
parents: 79425
diff changeset
   880
        map_proof_types {hyps = true}
b5ba5b767444 minor performance tuning;
wenzelm
parents: 79425
diff changeset
   881
          (subst_type_same (Same.function (ZTVars.lookup constrain_instT)));
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   882
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   883
    val hyps' = map unconstrain_zterm hyps;
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   884
    val prems = map (zterm o #2) constraints @ hyps';
79419
93f26d333fb5 clarified box_proof: use sort constraints within the logic;
wenzelm
parents: 79418
diff changeset
   885
    val prop' = beta_norm_term (close_prop prems (unconstrain_zterm concl));
79428
4cd892d1a676 omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents: 79427
diff changeset
   886
    val prf' = beta_norm_prooft (close_proof prems (unconstrain_proof prf));
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   887
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   888
    val i = serial ();
79312
8db60cadad86 clarified signature;
wenzelm
parents: 79311
diff changeset
   889
    val zbox: zbox = (i, (prop', prf'));
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   890
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   891
    val const = constrain_proof (ZConstp (zproof_const (zproof_name i, prop')));
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   892
    val args1 =
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   893
      outer_constraints |> map (fn (T, c) =>
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   894
        ZClassp (ztyp_of (if unconstrain then unconstrain_typ T else T), c));
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   895
    val args2 = if unconstrain then map ZHyp hyps' else map (ZHyp o zterm) hyps;
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   896
  in (zbox, ZAppps (ZAppps (const, args1), args2)) end;
79312
8db60cadad86 clarified signature;
wenzelm
parents: 79311
diff changeset
   897
79313
3b03af5125de use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents: 79312
diff changeset
   898
in
3b03af5125de use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents: 79312
diff changeset
   899
79361
c28d4d1986f1 tuned signature;
wenzelm
parents: 79360
diff changeset
   900
fun thm_proof thy thm_name =
79329
992c494bda25 proper thm_name for stored zproof;
wenzelm
parents: 79326
diff changeset
   901
  let
992c494bda25 proper thm_name for stored zproof;
wenzelm
parents: 79326
diff changeset
   902
    val thy_name = Context.theory_long_name thy;
79361
c28d4d1986f1 tuned signature;
wenzelm
parents: 79360
diff changeset
   903
    fun zproof_name i = ZThm {theory_name = thy_name, thm_name = thm_name, serial = i};
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   904
  in box_proof {unconstrain = false} zproof_name thy end;
79313
3b03af5125de use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents: 79312
diff changeset
   905
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   906
fun add_box_proof unconstrain thy hyps concl prf zboxes =
79422
371ee5494d15 tuned signature: canonical argument order;
wenzelm
parents: 79421
diff changeset
   907
  let
79429
637d7d896929 more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents: 79428
diff changeset
   908
    val (zbox, prf') = box_proof unconstrain ZBox thy hyps concl prf;
79422
371ee5494d15 tuned signature: canonical argument order;
wenzelm
parents: 79421
diff changeset
   909
    val zboxes' = add_zboxes zbox zboxes;
371ee5494d15 tuned signature: canonical argument order;
wenzelm
parents: 79421
diff changeset
   910
  in (prf', zboxes') end;
79265
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   911
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   912
end;
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   913
3c194f50beef more zproofs;
wenzelm
parents: 79264
diff changeset
   914
79161
3f532c76d0ad tuned structure;
wenzelm
parents: 79160
diff changeset
   915
(* basic logic *)
3f532c76d0ad tuned structure;
wenzelm
parents: 79160
diff changeset
   916
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   917
fun axiom_proof thy name A =
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   918
  ZConstp (zproof_const (ZAxiom name, zterm_of thy A));
79161
3f532c76d0ad tuned structure;
wenzelm
parents: 79160
diff changeset
   919
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   920
fun oracle_proof thy name A =
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   921
  ZConstp (zproof_const (ZOracle name, zterm_of thy A));
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   922
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   923
fun assume_proof thy A =
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   924
  ZHyp (zterm_of thy A);
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   925
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   926
fun trivial_proof thy A =
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   927
  ZAbsp ("H", zterm_of thy A, ZBoundp 0);
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   928
79414
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   929
fun implies_intr_proof' h prf =
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   930
  let
79277
7c415c73ebf7 tuned, following close_proof;
wenzelm
parents: 79276
diff changeset
   931
    fun proof lev (ZHyp t) = if aconv_zterm (h, t) then ZBoundp lev else raise Same.SAME
7c415c73ebf7 tuned, following close_proof;
wenzelm
parents: 79276
diff changeset
   932
      | proof lev (ZAbst (x, T, p)) = ZAbst (x, T, proof lev p)
7c415c73ebf7 tuned, following close_proof;
wenzelm
parents: 79276
diff changeset
   933
      | proof lev (ZAbsp (x, t, p)) = ZAbsp (x, t, proof (lev + 1) p)
7c415c73ebf7 tuned, following close_proof;
wenzelm
parents: 79276
diff changeset
   934
      | proof lev (ZAppt (p, t)) = ZAppt (proof lev p, t)
7c415c73ebf7 tuned, following close_proof;
wenzelm
parents: 79276
diff changeset
   935
      | proof lev (ZAppp (p, q)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   936
          (ZAppp (proof lev p, Same.commit (proof lev) q)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   937
            handle Same.SAME => ZAppp (p, proof lev q))
79157
00962876301c tuned names;
wenzelm
parents: 79156
diff changeset
   938
      | proof _ _ = raise Same.SAME;
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   939
  in ZAbsp ("H", h, Same.commit (proof 0) prf) end;
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   940
79414
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   941
fun implies_intr_proof thy = implies_intr_proof' o zterm_of thy;
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
   942
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   943
fun forall_intr_proof thy T (a, x) prf =
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   944
  let
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   945
    val {ztyp, zterm} = zterm_cache thy;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   946
    val Z = ztyp T;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   947
    val z = zterm x;
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   948
79157
00962876301c tuned names;
wenzelm
parents: 79156
diff changeset
   949
    fun term i b =
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   950
      if aconv_zterm (b, z) then ZBound i
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   951
      else
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   952
        (case b of
79157
00962876301c tuned names;
wenzelm
parents: 79156
diff changeset
   953
          ZAbs (x, T, t) => ZAbs (x, T, term (i + 1) t)
79156
3b272da1d165 minor performance tuning;
wenzelm
parents: 79155
diff changeset
   954
        | ZApp (t, u) =>
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   955
            (ZApp (term i t, Same.commit (term i) u)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   956
              handle Same.SAME => ZApp (t, term i u))
79156
3b272da1d165 minor performance tuning;
wenzelm
parents: 79155
diff changeset
   957
        | _ => raise Same.SAME);
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   958
79157
00962876301c tuned names;
wenzelm
parents: 79156
diff changeset
   959
    fun proof i (ZAbst (x, T, prf)) = ZAbst (x, T, proof (i + 1) prf)
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   960
      | proof i (ZAbsp (x, t, prf)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   961
          (ZAbsp (x, term i t, Same.commit (proof i) prf)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   962
            handle Same.SAME => ZAbsp (x, t, proof i prf))
79157
00962876301c tuned names;
wenzelm
parents: 79156
diff changeset
   963
      | proof i (ZAppt (p, t)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   964
          (ZAppt (proof i p, Same.commit (term i) t)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   965
            handle Same.SAME => ZAppt (p, term i t))
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   966
      | proof i (ZAppp (p, q)) =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   967
          (ZAppp (proof i p, Same.commit (proof i) q)
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
   968
            handle Same.SAME => ZAppp (p, proof i q))
79157
00962876301c tuned names;
wenzelm
parents: 79156
diff changeset
   969
      | proof _ _ = raise Same.SAME;
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   970
79157
00962876301c tuned names;
wenzelm
parents: 79156
diff changeset
   971
  in ZAbst (a, Z, Same.commit (proof 0) prf) end;
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   972
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
   973
fun forall_elim_proof thy t p = ZAppt (p, zterm_of thy t);
79119
cf29db6c95e1 more zterm operations;
wenzelm
parents: 79114
diff changeset
   974
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
   975
fun of_class_proof (T, c) = ZClassp (ztyp_of T, c);
79128
b6f5d4392388 more zproofs;
wenzelm
parents: 79126
diff changeset
   976
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   977
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   978
(* equality *)
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   979
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   980
local
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   981
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   982
val thy0 =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   983
  Context.the_global_context ()
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   984
  |> Sign.add_types_global [(Binding.name "fun", 2, NoSyn), (Binding.name "prop", 0, NoSyn)]
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   985
  |> Sign.local_path
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   986
  |> Sign.add_consts
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   987
   [(Binding.name "all", (Term.aT [] --> propT) --> propT, NoSyn),
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   988
    (Binding.name "imp", propT --> propT --> propT, NoSyn),
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   989
    (Binding.name "eq", Term.aT [] --> Term.aT [] --> propT, NoSyn)];
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   990
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   991
val [reflexive_axiom, symmetric_axiom, transitive_axiom, equal_intr_axiom, equal_elim_axiom,
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   992
  abstract_rule_axiom, combination_axiom] =
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   993
    Theory.equality_axioms |> map (fn (b, t) =>
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
   994
      let val ZConstp c = axiom_proof thy0 (Sign.full_name thy0 b) t in c end);
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   995
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   996
in
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   997
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
   998
val is_reflexive_proof =
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
   999
  fn ZConstp ((ZAxiom "Pure.reflexive", _), _) => true | _ => false;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1000
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1001
fun reflexive_proof thy T t =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1002
  let
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1003
    val {ztyp, zterm} = zterm_cache thy;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1004
    val A = ztyp T;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1005
    val x = zterm t;
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
  1006
  in make_const_proof (fn "'a" => A, fn "x" => x) reflexive_axiom end;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1007
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1008
fun symmetric_proof thy T t u prf =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1009
  if is_reflexive_proof prf then prf
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1010
  else
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1011
    let
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1012
      val {ztyp, zterm} = zterm_cache thy;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1013
      val A = ztyp T;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1014
      val x = zterm t;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1015
      val y = zterm u;
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
  1016
      val ax = make_const_proof (fn "'a" => A, fn "x" => x | "y" => y) symmetric_axiom;
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1017
    in ZAppp (ax, prf) end;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1018
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1019
fun transitive_proof thy T t u v prf1 prf2 =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1020
  if is_reflexive_proof prf1 then prf2
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1021
  else if is_reflexive_proof prf2 then prf1
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1022
  else
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1023
    let
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1024
      val {ztyp, zterm} = zterm_cache thy;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1025
      val A = ztyp T;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1026
      val x = zterm t;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1027
      val y = zterm u;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1028
      val z = zterm v;
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
  1029
      val ax = make_const_proof (fn "'a" => A, fn "x" => x | "y" => y | "z" => z) transitive_axiom;
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1030
    in ZAppp (ZAppp (ax, prf1), prf2) end;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1031
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1032
fun equal_intr_proof thy t u prf1 prf2 =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1033
  let
79160
wenzelm
parents: 79158
diff changeset
  1034
    val {zterm, ...} = zterm_cache thy;
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1035
    val A = zterm t;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1036
    val B = zterm u;
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
  1037
    val ax = make_const_proof (undefined, fn "A" => A | "B" => B) equal_intr_axiom;
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1038
  in ZAppp (ZAppp (ax, prf1), prf2) end;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1039
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1040
fun equal_elim_proof thy t u prf1 prf2 =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1041
  let
79160
wenzelm
parents: 79158
diff changeset
  1042
    val {zterm, ...} = zterm_cache thy;
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1043
    val A = zterm t;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1044
    val B = zterm u;
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
  1045
    val ax = make_const_proof (undefined, fn "A" => A | "B" => B) equal_elim_axiom;
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1046
  in ZAppp (ZAppp (ax, prf1), prf2) end;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1047
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1048
fun abstract_rule_proof thy T U x t u prf =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1049
  let
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1050
    val {ztyp, zterm} = zterm_cache thy;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1051
    val A = ztyp T;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1052
    val B = ztyp U;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1053
    val f = zterm t;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1054
    val g = zterm u;
79126
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
  1055
    val ax =
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
  1056
      make_const_proof (fn "'a" => A | "'b" => B, fn "f" => f | "g" => g)
79126
bdb33a2d4167 clarified const_proof vs. zproof_name;
wenzelm
parents: 79124
diff changeset
  1057
        abstract_rule_axiom;
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1058
  in ZAppp (ax, forall_intr_proof thy T x prf) end;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1059
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1060
fun combination_proof thy T U f g t u prf1 prf2 =
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1061
  let
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1062
    val {ztyp, zterm} = zterm_cache thy;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1063
    val A = ztyp T;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1064
    val B = ztyp U;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1065
    val f' = zterm f;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1066
    val g' = zterm g;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1067
    val x = zterm t;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1068
    val y = zterm u;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1069
    val ax =
79263
bf2e724ff57e clarified signature;
wenzelm
parents: 79261
diff changeset
  1070
      make_const_proof (fn "'a" => A | "'b" => B, fn "f" => f' | "g" => g' | "x" => x | "y" => y)
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1071
        combination_axiom;
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1072
  in ZAppp (ZAppp (ax, prf1), prf2) end;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1073
79098
d8940e5bbb25 tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff changeset
  1074
end;
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1075
79133
cfe995369655 more zproofs;
wenzelm
parents: 79132
diff changeset
  1076
cfe995369655 more zproofs;
wenzelm
parents: 79132
diff changeset
  1077
(* substitution *)
cfe995369655 more zproofs;
wenzelm
parents: 79132
diff changeset
  1078
cfe995369655 more zproofs;
wenzelm
parents: 79132
diff changeset
  1079
fun generalize_proof (tfrees, frees) idx prf =
cfe995369655 more zproofs;
wenzelm
parents: 79132
diff changeset
  1080
  let
cfe995369655 more zproofs;
wenzelm
parents: 79132
diff changeset
  1081
    val typ =
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
  1082
      if Names.is_empty tfrees then Same.same
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
  1083
      else
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
  1084
        ZTypes.unsynchronized_cache
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
  1085
          (subst_type_same (fn ((a, i), S) =>
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
  1086
            if i = ~1 andalso Names.defined tfrees a then ZTVar ((a, idx), S)
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
  1087
            else raise Same.SAME));
79136
wenzelm
parents: 79135
diff changeset
  1088
    val term =
79147
bfe5c20074e4 proper substitution of types within term;
wenzelm
parents: 79146
diff changeset
  1089
      subst_term_same typ (fn ((x, i), T) =>
bfe5c20074e4 proper substitution of types within term;
wenzelm
parents: 79146
diff changeset
  1090
        if i = ~1 andalso Names.defined frees x then ZVar ((x, idx), T)
bfe5c20074e4 proper substitution of types within term;
wenzelm
parents: 79146
diff changeset
  1091
        else raise Same.SAME);
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
  1092
  in map_proof {hyps = false} typ term prf end;
79133
cfe995369655 more zproofs;
wenzelm
parents: 79132
diff changeset
  1093
79149
810679c5ed3c more zproofs;
wenzelm
parents: 79148
diff changeset
  1094
fun instantiate_proof thy (Ts, ts) prf =
810679c5ed3c more zproofs;
wenzelm
parents: 79148
diff changeset
  1095
  let
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1096
    val {ztyp, zterm} = zterm_cache thy;
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1097
    val instT = ZTVars.build (Ts |> fold (fn (v, T) => ZTVars.add (v, ztyp T)));
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1098
    val inst = ZVars.build (ts |> fold (fn ((v, T), t) => ZVars.add ((v, ztyp T), zterm t)));
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
  1099
    val typ = instantiate_type_same instT;
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
  1100
    val term = instantiate_term_same typ inst;
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
  1101
  in map_proof {hyps = false} typ term prf end;
79149
810679c5ed3c more zproofs;
wenzelm
parents: 79148
diff changeset
  1102
79135
db2dc7634d62 more zproofs;
wenzelm
parents: 79133
diff changeset
  1103
fun varifyT_proof names prf =
db2dc7634d62 more zproofs;
wenzelm
parents: 79133
diff changeset
  1104
  if null names then prf
db2dc7634d62 more zproofs;
wenzelm
parents: 79133
diff changeset
  1105
  else
db2dc7634d62 more zproofs;
wenzelm
parents: 79133
diff changeset
  1106
    let
db2dc7634d62 more zproofs;
wenzelm
parents: 79133
diff changeset
  1107
      val tab = ZTVars.build (names |> fold (fn ((a, S), b) => ZTVars.add (((a, ~1), S), b)));
79136
wenzelm
parents: 79135
diff changeset
  1108
      val typ =
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
  1109
        ZTypes.unsynchronized_cache (subst_type_same (fn v =>
79136
wenzelm
parents: 79135
diff changeset
  1110
          (case ZTVars.lookup tab v of
wenzelm
parents: 79135
diff changeset
  1111
            NONE => raise Same.SAME
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
  1112
          | SOME w => ZTVar w)));
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
  1113
    in map_proof_types {hyps = false} typ prf end;
79135
db2dc7634d62 more zproofs;
wenzelm
parents: 79133
diff changeset
  1114
79152
4189e10f1524 more zproofs;
wenzelm
parents: 79150
diff changeset
  1115
fun legacy_freezeT_proof t prf =
4189e10f1524 more zproofs;
wenzelm
parents: 79150
diff changeset
  1116
  (case Type.legacy_freezeT t of
4189e10f1524 more zproofs;
wenzelm
parents: 79150
diff changeset
  1117
    NONE => prf
4189e10f1524 more zproofs;
wenzelm
parents: 79150
diff changeset
  1118
  | SOME f =>
4189e10f1524 more zproofs;
wenzelm
parents: 79150
diff changeset
  1119
      let
4189e10f1524 more zproofs;
wenzelm
parents: 79150
diff changeset
  1120
        val tvar = ztyp_of o Same.function f;
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79161
diff changeset
  1121
        val typ = ZTypes.unsynchronized_cache (subst_type_same tvar);
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
  1122
      in map_proof_types {hyps = false} typ prf end);
79152
4189e10f1524 more zproofs;
wenzelm
parents: 79150
diff changeset
  1123
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1124
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1125
(* permutations *)
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1126
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1127
fun rotate_proof thy Bs Bi' params asms m prf =
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1128
  let
79158
3c7ab17380a8 performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents: 79157
diff changeset
  1129
    val {ztyp, zterm} = zterm_cache thy;
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1130
    val i = length asms;
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1131
    val j = length Bs;
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1132
  in
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1133
    ZAbsps (map zterm Bs @ [zterm Bi']) (ZAppps (prf, map ZBoundp
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1134
      (j downto 1) @ [ZAbsts (map (apsnd ztyp) params) (ZAbsps (map zterm asms)
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1135
        (ZAppps (ZAppts (ZBoundp i, map ZBound ((length params - 1) downto 0)),
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
  1136
          map ZBoundp (((i - m - 1) downto 0) @ ((i - 1) downto (i - m))))))]))
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1137
  end;
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1138
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1139
fun permute_prems_proof thy prems' j k prf =
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1140
  let
79160
wenzelm
parents: 79158
diff changeset
  1141
    val {zterm, ...} = zterm_cache thy;
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1142
    val n = length prems';
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1143
  in
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1144
    ZAbsps (map zterm prems')
79326
8a2921053511 tuned whitespace;
wenzelm
parents: 79325
diff changeset
  1145
      (ZAppps (prf, map ZBoundp ((n - 1 downto n - j) @ (k - 1 downto 0) @ (n - j - 1 downto k))))
79155
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1146
  end;
53288743c2f0 more zproofs;
wenzelm
parents: 79154
diff changeset
  1147
79211
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1148
79234
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1149
(* lifting *)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1150
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1151
fun incr_tvar_same inc =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1152
  if inc = 0 then Same.same
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1153
  else
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1154
    let fun tvar ((a, i), S) = if i >= 0 then ZTVar ((a, i + inc), S) else raise Same.SAME
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1155
    in ZTypes.unsynchronized_cache (subst_type_same tvar) end;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1156
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1157
fun incr_indexes_proof inc prf =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1158
  let
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1159
    val typ = incr_tvar_same inc;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1160
    fun var ((x, i), T) = if i >= 0 then ZVar ((x, i + inc), T) else raise Same.SAME;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1161
    val term = subst_term_same typ var;
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
  1162
  in map_proof {hyps = false} typ term prf end;
79234
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1163
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1164
fun lift_proof thy gprop inc prems prf =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1165
  let
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1166
    val {ztyp, zterm} = zterm_cache thy;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1167
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1168
    val typ = incr_tvar_same inc;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1169
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1170
    fun term Ts lev =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1171
      if null Ts andalso inc = 0 then Same.same
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1172
      else
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1173
        let
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1174
          val n = length Ts;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1175
          fun incr lev (ZVar ((x, i), T)) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1176
                if i >= 0 then combound (ZVar ((x, i + inc), ZFuns (Ts, Same.commit typ T)), lev, n)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1177
                else raise Same.SAME
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1178
            | incr _ (ZBound _) = raise Same.SAME
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1179
            | incr _ (ZConst0 _) = raise Same.SAME
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1180
            | incr _ (ZConst1 (c, T)) = ZConst1 (c, typ T)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1181
            | incr _ (ZConst (c, Ts)) = ZConst (c, Same.map typ Ts)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1182
            | incr lev (ZAbs (x, T, t)) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1183
                (ZAbs (x, typ T, Same.commit (incr (lev + 1)) t)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1184
                  handle Same.SAME => ZAbs (x, T, incr (lev + 1) t))
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1185
            | incr lev (ZApp (t, u)) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1186
                (ZApp (incr lev t, Same.commit (incr lev) u)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1187
                  handle Same.SAME => ZApp (t, incr lev u))
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1188
            | incr _ (ZClass (T, c)) = ZClass (typ T, c);
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1189
        in incr lev end;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1190
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1191
    fun proof Ts lev (ZAbst (a, T, p)) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1192
          (ZAbst (a, typ T, Same.commit (proof Ts (lev + 1)) p)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1193
            handle Same.SAME => ZAbst (a, T, proof Ts (lev + 1) p))
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1194
      | proof Ts lev (ZAbsp (a, t, p)) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1195
          (ZAbsp (a, term Ts lev t, Same.commit (proof Ts lev) p)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1196
            handle Same.SAME => ZAbsp (a, t, proof Ts lev p))
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1197
      | proof Ts lev (ZAppt (p, t)) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1198
          (ZAppt (proof Ts lev p, Same.commit (term Ts lev) t)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1199
            handle Same.SAME => ZAppt (p, term Ts lev t))
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1200
      | proof Ts lev (ZAppp (p, q)) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1201
          (ZAppp (proof Ts lev p, Same.commit (proof Ts lev) q)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1202
            handle Same.SAME => ZAppp (p, proof Ts lev q))
80262
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
  1203
      | proof Ts lev (ZConstp (a, insts)) =
d49f3a1c06a6 tuned signature;
wenzelm
parents: 79478
diff changeset
  1204
          ZConstp (a, map_insts_same typ (term Ts lev) insts)
79234
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1205
      | proof _ _ (ZClassp (T, c)) = ZClassp (typ T, c)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1206
      | proof _ _ _ = raise Same.SAME;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1207
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1208
    val k = length prems;
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1209
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1210
    fun mk_app b (i, j, p) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1211
      if b then (i - 1, j, ZAppp (p, ZBoundp i)) else (i, j - 1, ZAppt (p, ZBound j));
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1212
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1213
    fun lift Ts bs i j (Const ("Pure.imp", _) $ A $ B) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1214
          ZAbsp ("H", Same.commit (term Ts 0) (zterm A), lift Ts (true :: bs) (i + 1) j B)
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1215
      | lift Ts bs i j (Const ("Pure.all", _) $ Abs (a, T, t)) =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1216
          let val T' = ztyp T
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1217
          in ZAbst (a, T', lift (T' :: Ts) (false :: bs) i (j + 1) t) end
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1218
      | lift Ts bs i j _ =
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1219
          ZAppps (Same.commit (proof (rev Ts) 0) prf,
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1220
            map (fn k => (#3 (fold_rev mk_app bs (i - 1, j - 1, ZBoundp k)))) (i + k - 1 downto i));
79237
f97fe7ad62a7 proper ZTerm.lift_proof (amending 4a1a25bdf81d);
wenzelm
parents: 79234
diff changeset
  1221
  in ZAbsps (map zterm prems) (lift [] [] 0 0 gprop) end;
79234
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1222
4a1a25bdf81d more zproofs;
wenzelm
parents: 79226
diff changeset
  1223
79211
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1224
(* higher-order resolution *)
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1225
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1226
fun mk_asm_prf C i m =
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1227
  let
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1228
    fun imp _ i 0 = ZBoundp i
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1229
      | imp (ZApp (ZApp (ZConst0 "Pure.imp", A), B)) i m = ZAbsp ("H", A, imp B (i + 1) (m - 1))
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1230
      | imp _ i _ = ZBoundp i;
79211
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1231
    fun all (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t))) = ZAbst (a, T, all t)
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1232
      | all t = imp t (~ i) m
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1233
  in all C end;
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1234
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
  1235
fun assumption_proof thy envir Bs Bi n visible prf =
79211
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1236
  let
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1237
    val cache as {zterm, ...} = norm_cache thy;
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1238
    val normt = zterm #> Same.commit (norm_term_same cache envir);
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1239
  in
79212
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1240
    ZAbsps (map normt Bs)
601aa36071ba clarified signature;
wenzelm
parents: 79211
diff changeset
  1241
      (ZAppps (prf, map ZBoundp (length Bs - 1 downto 0) @ [mk_asm_prf (normt Bi) n ~1]))
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
  1242
    |> norm_proof_cache cache envir visible
79211
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1243
  end;
35ead2206eb1 more zproofs;
wenzelm
parents: 79210
diff changeset
  1244
79261
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1245
fun flatten_params_proof i j n (ZApp (ZApp (ZConst0 "Pure.imp", A), B), k) =
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1246
      ZAbsp ("H", A, flatten_params_proof (i + 1) j n (B, k))
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1247
  | flatten_params_proof i j n (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t)), k) =
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1248
      ZAbst (a, T, flatten_params_proof i (j + 1) n (t, k))
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1249
  | flatten_params_proof i j n (_, k) =
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1250
      ZAppps (ZAppts (ZBoundp (k + i),
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1251
        map ZBound (j - 1 downto 0)), map ZBoundp (remove (op =) (i - n) (i - 1 downto 0)));
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1252
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
  1253
fun bicompose_proof thy env smax flatten Bs As A oldAs n m visible rprf sprf =
79261
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1254
  let
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1255
    val cache as {zterm, ...} = norm_cache thy;
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1256
    val normt = zterm #> Same.commit (norm_term_same cache env);
79270
90c5aadcc4b2 more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents: 79269
diff changeset
  1257
    val normp = norm_proof_cache cache env visible;
79261
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1258
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1259
    val lb = length Bs;
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1260
    val la = length As;
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1261
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1262
    fun proof p q =
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1263
      ZAbsps (map normt (Bs @ As))
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1264
        (ZAppp (ZAppps (q, map ZBoundp (lb + la - 1 downto la)),
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1265
          ZAppps (p, (if n > 0 then [mk_asm_prf (normt (the A)) n m] else []) @
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1266
            map (if flatten then flatten_params_proof 0 0 n else ZBoundp o snd)
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1267
              (map normt oldAs ~~ (la - 1 downto 0)))));
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1268
  in
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1269
    if Envir.is_empty env then proof rprf sprf
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1270
    else proof (normp rprf) (if Envir.above env smax then sprf else normp sprf)
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1271
  end;
2e6fcc331f10 more zproofs;
wenzelm
parents: 79259
diff changeset
  1272
79405
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1273
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1274
(* sort constraints within the logic *)
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1275
79425
0875c87b4a4b clarified signature;
wenzelm
parents: 79422
diff changeset
  1276
type sorts_proof = (class * class -> zproof) * (string * class list list * class -> zproof);
79405
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1277
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1278
fun of_sort_proof algebra (classrel_proof, arity_proof) hyps =
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1279
  Sorts.of_sort_derivation algebra
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1280
   {class_relation = fn _ => fn _ => fn (prf, c1) => fn c2 =>
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1281
      if c1 = c2 then prf else ZAppp (classrel_proof (c1, c2), prf),
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1282
    type_constructor = fn (a, _) => fn dom => fn c =>
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1283
      let val Ss = map (map snd) dom and prfs = maps (map fst) dom
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1284
      in ZAppps (arity_proof (a, Ss, c), prfs) end,
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1285
    type_variable = fn typ => map (fn c => (hyps (typ, c), c)) (Type.sort_of_atyp typ)};
f4fdf5eebcac pro-forma support for ZTerm.sorts_zproof;
wenzelm
parents: 79388
diff changeset
  1286
79414
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1287
fun unconstrainT_proof thy sorts_proof (ucontext: Logic.unconstrain_context) =
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1288
  let
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1289
    val cache = norm_cache thy;
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1290
    val algebra = Sign.classes_of thy;
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1291
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1292
    val typ =
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1293
      ZTypes.unsynchronized_cache
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1294
        (typ_of #> #typ_operation ucontext {strip_sorts = true} #> ztyp_of);
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1295
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1296
    val typ_sort = #typ_operation ucontext {strip_sorts = false};
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1297
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1298
    fun hyps T =
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1299
      (case AList.lookup (op =) (#constraints ucontext) T of
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1300
        SOME t => ZHyp (#zterm cache t)
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1301
      | NONE => raise Fail "unconstrainT_proof: missing constraint");
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1302
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1303
    fun class (T, c) =
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1304
      let val T' = Same.commit typ_sort (#typ cache T)
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1305
      in the_single (of_sort_proof algebra sorts_proof hyps (T', [c])) end;
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1306
  in
79418
3a66bcb208b8 more operations (see also 8368160d3c65);
wenzelm
parents: 79417
diff changeset
  1307
    map_proof_types {hyps = false} typ #> map_class_proof class #> beta_norm_prooft
79414
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1308
    #> fold_rev (implies_intr_proof' o #zterm cache o #2) (#constraints ucontext)
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1309
  end;
6cacfbce33ba more operations;
wenzelm
parents: 79405
diff changeset
  1310
79124
89d4a8f52738 more zproofs;
wenzelm
parents: 79119
diff changeset
  1311
end;