src/HOL/Tools/Quotient/quotient_term.ML
author haftmann
Wed May 05 18:25:34 2010 +0200 (2010-05-05)
changeset 36692 54b64d4ad524
parent 35990 5ceedb86aa9d
child 37135 636e6d8645d6
permissions -rw-r--r--
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
wenzelm@35788
     1
(*  Title:      HOL/Tools/Quotient/quotient_term.thy
kaliszyk@35222
     2
    Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@35222
     3
wenzelm@35788
     4
Constructs terms corresponding to goals from lifting theorems to
wenzelm@35788
     5
quotient types.
kaliszyk@35222
     6
*)
kaliszyk@35222
     7
kaliszyk@35222
     8
signature QUOTIENT_TERM =
kaliszyk@35222
     9
sig
kaliszyk@35222
    10
  exception LIFT_MATCH of string
kaliszyk@35222
    11
kaliszyk@35222
    12
  datatype flag = AbsF | RepF
kaliszyk@35222
    13
kaliszyk@35222
    14
  val absrep_fun: flag -> Proof.context -> typ * typ -> term
kaliszyk@35222
    15
  val absrep_fun_chk: flag -> Proof.context -> typ * typ -> term
kaliszyk@35222
    16
kaliszyk@35222
    17
  (* Allows Nitpick to represent quotient types as single elements from raw type *)
kaliszyk@35222
    18
  val absrep_const_chk: flag -> Proof.context -> string -> term
kaliszyk@35222
    19
kaliszyk@35222
    20
  val equiv_relation: Proof.context -> typ * typ -> term
kaliszyk@35222
    21
  val equiv_relation_chk: Proof.context -> typ * typ -> term
kaliszyk@35222
    22
kaliszyk@35222
    23
  val regularize_trm: Proof.context -> term * term -> term
kaliszyk@35222
    24
  val regularize_trm_chk: Proof.context -> term * term -> term
kaliszyk@35222
    25
kaliszyk@35222
    26
  val inj_repabs_trm: Proof.context -> term * term -> term
kaliszyk@35222
    27
  val inj_repabs_trm_chk: Proof.context -> term * term -> term
kaliszyk@35222
    28
kaliszyk@35990
    29
  val quotient_lift_const: typ list -> string * term -> local_theory -> term
kaliszyk@35990
    30
  val quotient_lift_all: typ list -> Proof.context -> term -> term
kaliszyk@35222
    31
end;
kaliszyk@35222
    32
kaliszyk@35222
    33
structure Quotient_Term: QUOTIENT_TERM =
kaliszyk@35222
    34
struct
kaliszyk@35222
    35
kaliszyk@35222
    36
open Quotient_Info;
kaliszyk@35222
    37
kaliszyk@35222
    38
exception LIFT_MATCH of string
kaliszyk@35222
    39
kaliszyk@35222
    40
kaliszyk@35222
    41
kaliszyk@35222
    42
(*** Aggregate Rep/Abs Function ***)
kaliszyk@35222
    43
kaliszyk@35222
    44
kaliszyk@35222
    45
(* The flag RepF is for types in negative position; AbsF is for types
kaliszyk@35222
    46
   in positive position. Because of this, function types need to be
kaliszyk@35222
    47
   treated specially, since there the polarity changes.
kaliszyk@35222
    48
*)
kaliszyk@35222
    49
kaliszyk@35222
    50
datatype flag = AbsF | RepF
kaliszyk@35222
    51
kaliszyk@35222
    52
fun negF AbsF = RepF
kaliszyk@35222
    53
  | negF RepF = AbsF
kaliszyk@35222
    54
kaliszyk@35222
    55
fun is_identity (Const (@{const_name "id"}, _)) = true
kaliszyk@35222
    56
  | is_identity _ = false
kaliszyk@35222
    57
kaliszyk@35222
    58
fun mk_identity ty = Const (@{const_name "id"}, ty --> ty)
kaliszyk@35222
    59
kaliszyk@35222
    60
fun mk_fun_compose flag (trm1, trm2) =
kaliszyk@35222
    61
  case flag of
kaliszyk@35222
    62
    AbsF => Const (@{const_name "comp"}, dummyT) $ trm1 $ trm2
kaliszyk@35222
    63
  | RepF => Const (@{const_name "comp"}, dummyT) $ trm2 $ trm1
kaliszyk@35222
    64
kaliszyk@35222
    65
fun get_mapfun ctxt s =
kaliszyk@35222
    66
let
kaliszyk@35222
    67
  val thy = ProofContext.theory_of ctxt
kaliszyk@35222
    68
  val exn = LIFT_MATCH ("No map function for type " ^ quote s ^ " found.")
kaliszyk@35222
    69
  val mapfun = #mapfun (maps_lookup thy s) handle Quotient_Info.NotFound => raise exn
kaliszyk@35222
    70
in
kaliszyk@35222
    71
  Const (mapfun, dummyT)
kaliszyk@35222
    72
end
kaliszyk@35222
    73
kaliszyk@35222
    74
(* makes a Free out of a TVar *)
kaliszyk@35222
    75
fun mk_Free (TVar ((x, i), _)) = Free (unprefix "'" x ^ string_of_int i, dummyT)
kaliszyk@35222
    76
kaliszyk@35222
    77
(* produces an aggregate map function for the
kaliszyk@35222
    78
   rty-part of a quotient definition; abstracts
kaliszyk@35222
    79
   over all variables listed in vs (these variables
kaliszyk@35222
    80
   correspond to the type variables in rty)
kaliszyk@35222
    81
kaliszyk@35222
    82
   for example for: (?'a list * ?'b)
kaliszyk@35222
    83
   it produces:     %a b. prod_map (map a) b
kaliszyk@35222
    84
*)
kaliszyk@35222
    85
fun mk_mapfun ctxt vs rty =
kaliszyk@35222
    86
let
kaliszyk@35222
    87
  val vs' = map (mk_Free) vs
kaliszyk@35222
    88
kaliszyk@35222
    89
  fun mk_mapfun_aux rty =
kaliszyk@35222
    90
    case rty of
kaliszyk@35222
    91
      TVar _ => mk_Free rty
kaliszyk@35222
    92
    | Type (_, []) => mk_identity rty
kaliszyk@35222
    93
    | Type (s, tys) => list_comb (get_mapfun ctxt s, map mk_mapfun_aux tys)
kaliszyk@35222
    94
    | _ => raise LIFT_MATCH "mk_mapfun (default)"
kaliszyk@35222
    95
in
kaliszyk@35222
    96
  fold_rev Term.lambda vs' (mk_mapfun_aux rty)
kaliszyk@35222
    97
end
kaliszyk@35222
    98
kaliszyk@35222
    99
(* looks up the (varified) rty and qty for
kaliszyk@35222
   100
   a quotient definition
kaliszyk@35222
   101
*)
kaliszyk@35222
   102
fun get_rty_qty ctxt s =
kaliszyk@35222
   103
let
kaliszyk@35222
   104
  val thy = ProofContext.theory_of ctxt
kaliszyk@35222
   105
  val exn = LIFT_MATCH ("No quotient type " ^ quote s ^ " found.")
kaliszyk@35222
   106
  val qdata = (quotdata_lookup thy s) handle Quotient_Info.NotFound => raise exn
kaliszyk@35222
   107
in
kaliszyk@35222
   108
  (#rtyp qdata, #qtyp qdata)
kaliszyk@35222
   109
end
kaliszyk@35222
   110
kaliszyk@35222
   111
(* takes two type-environments and looks
kaliszyk@35222
   112
   up in both of them the variable v, which
kaliszyk@35222
   113
   must be listed in the environment
kaliszyk@35222
   114
*)
kaliszyk@35222
   115
fun double_lookup rtyenv qtyenv v =
kaliszyk@35222
   116
let
kaliszyk@35222
   117
  val v' = fst (dest_TVar v)
kaliszyk@35222
   118
in
kaliszyk@35222
   119
  (snd (the (Vartab.lookup rtyenv v')), snd (the (Vartab.lookup qtyenv v')))
kaliszyk@35222
   120
end
kaliszyk@35222
   121
kaliszyk@35222
   122
(* matches a type pattern with a type *)
kaliszyk@35222
   123
fun match ctxt err ty_pat ty =
kaliszyk@35222
   124
let
kaliszyk@35222
   125
  val thy = ProofContext.theory_of ctxt
kaliszyk@35222
   126
in
kaliszyk@35222
   127
  Sign.typ_match thy (ty_pat, ty) Vartab.empty
kaliszyk@35222
   128
  handle MATCH_TYPE => err ctxt ty_pat ty
kaliszyk@35222
   129
end
kaliszyk@35222
   130
kaliszyk@35222
   131
(* produces the rep or abs constant for a qty *)
kaliszyk@35222
   132
fun absrep_const flag ctxt qty_str =
kaliszyk@35222
   133
let
kaliszyk@35222
   134
  val qty_name = Long_Name.base_name qty_str
kaliszyk@35843
   135
  val qualifier = Long_Name.qualifier qty_str
kaliszyk@35222
   136
in
kaliszyk@35222
   137
  case flag of
kaliszyk@35843
   138
    AbsF => Const (Long_Name.qualify qualifier ("abs_" ^ qty_name), dummyT)
kaliszyk@35843
   139
  | RepF => Const (Long_Name.qualify qualifier ("rep_" ^ qty_name), dummyT)
kaliszyk@35222
   140
end
kaliszyk@35222
   141
kaliszyk@35222
   142
(* Lets Nitpick represent elements of quotient types as elements of the raw type *)
kaliszyk@35222
   143
fun absrep_const_chk flag ctxt qty_str =
kaliszyk@35222
   144
  Syntax.check_term ctxt (absrep_const flag ctxt qty_str)
kaliszyk@35222
   145
kaliszyk@35222
   146
fun absrep_match_err ctxt ty_pat ty =
kaliszyk@35222
   147
let
kaliszyk@35222
   148
  val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
kaliszyk@35222
   149
  val ty_str = Syntax.string_of_typ ctxt ty
kaliszyk@35222
   150
in
kaliszyk@35222
   151
  raise LIFT_MATCH (space_implode " "
kaliszyk@35222
   152
    ["absrep_fun (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
kaliszyk@35222
   153
end
kaliszyk@35222
   154
kaliszyk@35222
   155
kaliszyk@35222
   156
(** generation of an aggregate absrep function **)
kaliszyk@35222
   157
kaliszyk@35222
   158
(* - In case of equal types we just return the identity.
kaliszyk@35222
   159
kaliszyk@35222
   160
   - In case of TFrees we also return the identity.
kaliszyk@35222
   161
kaliszyk@35222
   162
   - In case of function types we recurse taking
kaliszyk@35222
   163
     the polarity change into account.
kaliszyk@35222
   164
kaliszyk@35222
   165
   - If the type constructors are equal, we recurse for the
kaliszyk@35222
   166
     arguments and build the appropriate map function.
kaliszyk@35222
   167
kaliszyk@35222
   168
   - If the type constructors are unequal, there must be an
kaliszyk@35222
   169
     instance of quotient types:
kaliszyk@35222
   170
kaliszyk@35222
   171
       - we first look up the corresponding rty_pat and qty_pat
kaliszyk@35222
   172
         from the quotient definition; the arguments of qty_pat
kaliszyk@35222
   173
         must be some distinct TVars
kaliszyk@35222
   174
       - we then match the rty_pat with rty and qty_pat with qty;
kaliszyk@35222
   175
         if matching fails the types do not correspond -> error
kaliszyk@35222
   176
       - the matching produces two environments; we look up the
kaliszyk@35222
   177
         assignments for the qty_pat variables and recurse on the
kaliszyk@35222
   178
         assignments
kaliszyk@35222
   179
       - we prefix the aggregate map function for the rty_pat,
kaliszyk@35222
   180
         which is an abstraction over all type variables
kaliszyk@35222
   181
       - finally we compose the result with the appropriate
kaliszyk@35222
   182
         absrep function in case at least one argument produced
kaliszyk@35222
   183
         a non-identity function /
kaliszyk@35222
   184
         otherwise we just return the appropriate absrep
kaliszyk@35222
   185
         function
kaliszyk@35222
   186
kaliszyk@35222
   187
     The composition is necessary for types like
kaliszyk@35222
   188
kaliszyk@35222
   189
        ('a list) list / ('a foo) foo
kaliszyk@35222
   190
kaliszyk@35222
   191
     The matching is necessary for types like
kaliszyk@35222
   192
kaliszyk@35222
   193
        ('a * 'a) list / 'a bar
kaliszyk@35222
   194
kaliszyk@35222
   195
     The test is necessary in order to eliminate superfluous
kaliszyk@35222
   196
     identity maps.
kaliszyk@35222
   197
*)
kaliszyk@35222
   198
kaliszyk@35222
   199
fun absrep_fun flag ctxt (rty, qty) =
kaliszyk@35222
   200
  if rty = qty
kaliszyk@35222
   201
  then mk_identity rty
kaliszyk@35222
   202
  else
kaliszyk@35222
   203
    case (rty, qty) of
kaliszyk@35222
   204
      (Type ("fun", [ty1, ty2]), Type ("fun", [ty1', ty2'])) =>
kaliszyk@35222
   205
        let
kaliszyk@35222
   206
          val arg1 = absrep_fun (negF flag) ctxt (ty1, ty1')
kaliszyk@35222
   207
          val arg2 = absrep_fun flag ctxt (ty2, ty2')
kaliszyk@35222
   208
        in
kaliszyk@35222
   209
          list_comb (get_mapfun ctxt "fun", [arg1, arg2])
kaliszyk@35222
   210
        end
kaliszyk@35222
   211
    | (Type (s, tys), Type (s', tys')) =>
kaliszyk@35222
   212
        if s = s'
kaliszyk@35222
   213
        then
kaliszyk@35222
   214
           let
kaliszyk@35222
   215
             val args = map (absrep_fun flag ctxt) (tys ~~ tys')
kaliszyk@35222
   216
           in
kaliszyk@35222
   217
             list_comb (get_mapfun ctxt s, args)
kaliszyk@35222
   218
           end
kaliszyk@35222
   219
        else
kaliszyk@35222
   220
           let
kaliszyk@35222
   221
             val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty ctxt s'
kaliszyk@35222
   222
             val rtyenv = match ctxt absrep_match_err rty_pat rty
kaliszyk@35222
   223
             val qtyenv = match ctxt absrep_match_err qty_pat qty
kaliszyk@35222
   224
             val args_aux = map (double_lookup rtyenv qtyenv) vs
kaliszyk@35222
   225
             val args = map (absrep_fun flag ctxt) args_aux
kaliszyk@35222
   226
             val map_fun = mk_mapfun ctxt vs rty_pat
kaliszyk@35222
   227
             val result = list_comb (map_fun, args)
kaliszyk@35222
   228
           in
kaliszyk@35222
   229
             if forall is_identity args
kaliszyk@35222
   230
             then absrep_const flag ctxt s'
kaliszyk@35222
   231
             else mk_fun_compose flag (absrep_const flag ctxt s', result)
kaliszyk@35222
   232
           end
kaliszyk@35222
   233
    | (TFree x, TFree x') =>
kaliszyk@35222
   234
        if x = x'
kaliszyk@35222
   235
        then mk_identity rty
kaliszyk@35222
   236
        else raise (LIFT_MATCH "absrep_fun (frees)")
kaliszyk@35222
   237
    | (TVar _, TVar _) => raise (LIFT_MATCH "absrep_fun (vars)")
kaliszyk@35222
   238
    | _ => raise (LIFT_MATCH "absrep_fun (default)")
kaliszyk@35222
   239
kaliszyk@35222
   240
fun absrep_fun_chk flag ctxt (rty, qty) =
kaliszyk@35222
   241
  absrep_fun flag ctxt (rty, qty)
kaliszyk@35222
   242
  |> Syntax.check_term ctxt
kaliszyk@35222
   243
kaliszyk@35222
   244
kaliszyk@35222
   245
kaliszyk@35222
   246
kaliszyk@35222
   247
(*** Aggregate Equivalence Relation ***)
kaliszyk@35222
   248
kaliszyk@35222
   249
kaliszyk@35222
   250
(* works very similar to the absrep generation,
kaliszyk@35222
   251
   except there is no need for polarities
kaliszyk@35222
   252
*)
kaliszyk@35222
   253
kaliszyk@35222
   254
(* instantiates TVars so that the term is of type ty *)
kaliszyk@35222
   255
fun force_typ ctxt trm ty =
kaliszyk@35222
   256
let
kaliszyk@35222
   257
  val thy = ProofContext.theory_of ctxt
kaliszyk@35222
   258
  val trm_ty = fastype_of trm
kaliszyk@35222
   259
  val ty_inst = Sign.typ_match thy (trm_ty, ty) Vartab.empty
kaliszyk@35222
   260
in
kaliszyk@35222
   261
  map_types (Envir.subst_type ty_inst) trm
kaliszyk@35222
   262
end
kaliszyk@35222
   263
kaliszyk@35222
   264
fun is_eq (Const (@{const_name "op ="}, _)) = true
kaliszyk@35222
   265
  | is_eq _ = false
kaliszyk@35222
   266
kaliszyk@35222
   267
fun mk_rel_compose (trm1, trm2) =
wenzelm@35402
   268
  Const (@{const_abbrev "rel_conj"}, dummyT) $ trm1 $ trm2
kaliszyk@35222
   269
kaliszyk@35222
   270
fun get_relmap ctxt s =
kaliszyk@35222
   271
let
kaliszyk@35222
   272
  val thy = ProofContext.theory_of ctxt
kaliszyk@35222
   273
  val exn = LIFT_MATCH ("get_relmap (no relation map function found for type " ^ s ^ ")")
kaliszyk@35222
   274
  val relmap = #relmap (maps_lookup thy s) handle Quotient_Info.NotFound => raise exn
kaliszyk@35222
   275
in
kaliszyk@35222
   276
  Const (relmap, dummyT)
kaliszyk@35222
   277
end
kaliszyk@35222
   278
kaliszyk@35222
   279
fun mk_relmap ctxt vs rty =
kaliszyk@35222
   280
let
kaliszyk@35222
   281
  val vs' = map (mk_Free) vs
kaliszyk@35222
   282
kaliszyk@35222
   283
  fun mk_relmap_aux rty =
kaliszyk@35222
   284
    case rty of
kaliszyk@35222
   285
      TVar _ => mk_Free rty
kaliszyk@35222
   286
    | Type (_, []) => HOLogic.eq_const rty
kaliszyk@35222
   287
    | Type (s, tys) => list_comb (get_relmap ctxt s, map mk_relmap_aux tys)
kaliszyk@35222
   288
    | _ => raise LIFT_MATCH ("mk_relmap (default)")
kaliszyk@35222
   289
in
kaliszyk@35222
   290
  fold_rev Term.lambda vs' (mk_relmap_aux rty)
kaliszyk@35222
   291
end
kaliszyk@35222
   292
kaliszyk@35222
   293
fun get_equiv_rel ctxt s =
kaliszyk@35222
   294
let
kaliszyk@35222
   295
  val thy = ProofContext.theory_of ctxt
kaliszyk@35222
   296
  val exn = LIFT_MATCH ("get_quotdata (no quotient found for type " ^ s ^ ")")
kaliszyk@35222
   297
in
kaliszyk@35222
   298
  #equiv_rel (quotdata_lookup thy s) handle Quotient_Info.NotFound => raise exn
kaliszyk@35222
   299
end
kaliszyk@35222
   300
kaliszyk@35222
   301
fun equiv_match_err ctxt ty_pat ty =
kaliszyk@35222
   302
let
kaliszyk@35222
   303
  val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
kaliszyk@35222
   304
  val ty_str = Syntax.string_of_typ ctxt ty
kaliszyk@35222
   305
in
kaliszyk@35222
   306
  raise LIFT_MATCH (space_implode " "
kaliszyk@35222
   307
    ["equiv_relation (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
kaliszyk@35222
   308
end
kaliszyk@35222
   309
kaliszyk@35222
   310
(* builds the aggregate equivalence relation
kaliszyk@35222
   311
   that will be the argument of Respects
kaliszyk@35222
   312
*)
kaliszyk@35222
   313
fun equiv_relation ctxt (rty, qty) =
kaliszyk@35222
   314
  if rty = qty
kaliszyk@35222
   315
  then HOLogic.eq_const rty
kaliszyk@35222
   316
  else
kaliszyk@35222
   317
    case (rty, qty) of
kaliszyk@35222
   318
      (Type (s, tys), Type (s', tys')) =>
kaliszyk@35222
   319
       if s = s'
kaliszyk@35222
   320
       then
kaliszyk@35222
   321
         let
kaliszyk@35222
   322
           val args = map (equiv_relation ctxt) (tys ~~ tys')
kaliszyk@35222
   323
         in
kaliszyk@35222
   324
           list_comb (get_relmap ctxt s, args)
kaliszyk@35222
   325
         end
kaliszyk@35222
   326
       else
kaliszyk@35222
   327
         let
kaliszyk@35222
   328
           val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty ctxt s'
kaliszyk@35222
   329
           val rtyenv = match ctxt equiv_match_err rty_pat rty
kaliszyk@35222
   330
           val qtyenv = match ctxt equiv_match_err qty_pat qty
kaliszyk@35222
   331
           val args_aux = map (double_lookup rtyenv qtyenv) vs
kaliszyk@35222
   332
           val args = map (equiv_relation ctxt) args_aux
kaliszyk@35222
   333
           val rel_map = mk_relmap ctxt vs rty_pat
kaliszyk@35222
   334
           val result = list_comb (rel_map, args)
kaliszyk@35222
   335
           val eqv_rel = get_equiv_rel ctxt s'
kaliszyk@35222
   336
           val eqv_rel' = force_typ ctxt eqv_rel ([rty, rty] ---> @{typ bool})
kaliszyk@35222
   337
         in
kaliszyk@35222
   338
           if forall is_eq args
kaliszyk@35222
   339
           then eqv_rel'
kaliszyk@35222
   340
           else mk_rel_compose (result, eqv_rel')
kaliszyk@35222
   341
         end
kaliszyk@35222
   342
      | _ => HOLogic.eq_const rty
kaliszyk@35222
   343
kaliszyk@35222
   344
fun equiv_relation_chk ctxt (rty, qty) =
kaliszyk@35222
   345
  equiv_relation ctxt (rty, qty)
kaliszyk@35222
   346
  |> Syntax.check_term ctxt
kaliszyk@35222
   347
kaliszyk@35222
   348
kaliszyk@35222
   349
kaliszyk@35222
   350
(*** Regularization ***)
kaliszyk@35222
   351
kaliszyk@35222
   352
(* Regularizing an rtrm means:
kaliszyk@35222
   353
kaliszyk@35222
   354
 - Quantifiers over types that need lifting are replaced
kaliszyk@35222
   355
   by bounded quantifiers, for example:
kaliszyk@35222
   356
kaliszyk@35222
   357
      All P  ----> All (Respects R) P
kaliszyk@35222
   358
kaliszyk@35222
   359
   where the aggregate relation R is given by the rty and qty;
kaliszyk@35222
   360
kaliszyk@35222
   361
 - Abstractions over types that need lifting are replaced
kaliszyk@35222
   362
   by bounded abstractions, for example:
kaliszyk@35222
   363
kaliszyk@35222
   364
      %x. P  ----> Ball (Respects R) %x. P
kaliszyk@35222
   365
kaliszyk@35222
   366
 - Equalities over types that need lifting are replaced by
kaliszyk@35222
   367
   corresponding equivalence relations, for example:
kaliszyk@35222
   368
kaliszyk@35222
   369
      A = B  ----> R A B
kaliszyk@35222
   370
kaliszyk@35222
   371
   or
kaliszyk@35222
   372
kaliszyk@35222
   373
      A = B  ----> (R ===> R) A B
kaliszyk@35222
   374
kaliszyk@35222
   375
   for more complicated types of A and B
kaliszyk@35222
   376
kaliszyk@35222
   377
kaliszyk@35222
   378
 The regularize_trm accepts raw theorems in which equalities
kaliszyk@35222
   379
 and quantifiers match exactly the ones in the lifted theorem
kaliszyk@35222
   380
 but also accepts partially regularized terms.
kaliszyk@35222
   381
kaliszyk@35222
   382
 This means that the raw theorems can have:
kaliszyk@35222
   383
   Ball (Respects R),  Bex (Respects R), Bex1_rel (Respects R), Babs, R
kaliszyk@35222
   384
 in the places where:
kaliszyk@35222
   385
   All, Ex, Ex1, %, (op =)
kaliszyk@35222
   386
 is required the lifted theorem.
kaliszyk@35222
   387
kaliszyk@35222
   388
*)
kaliszyk@35222
   389
kaliszyk@35222
   390
val mk_babs = Const (@{const_name Babs}, dummyT)
kaliszyk@35222
   391
val mk_ball = Const (@{const_name Ball}, dummyT)
kaliszyk@35222
   392
val mk_bex  = Const (@{const_name Bex}, dummyT)
kaliszyk@35222
   393
val mk_bex1_rel = Const (@{const_name Bex1_rel}, dummyT)
kaliszyk@35222
   394
val mk_resp = Const (@{const_name Respects}, dummyT)
kaliszyk@35222
   395
kaliszyk@35222
   396
(* - applies f to the subterm of an abstraction,
kaliszyk@35222
   397
     otherwise to the given term,
kaliszyk@35222
   398
   - used by regularize, therefore abstracted
kaliszyk@35222
   399
     variables do not have to be treated specially
kaliszyk@35222
   400
*)
kaliszyk@35222
   401
fun apply_subt f (trm1, trm2) =
kaliszyk@35222
   402
  case (trm1, trm2) of
kaliszyk@35222
   403
    (Abs (x, T, t), Abs (_ , _, t')) => Abs (x, T, f (t, t'))
kaliszyk@35222
   404
  | _ => f (trm1, trm2)
kaliszyk@35222
   405
kaliszyk@35222
   406
fun term_mismatch str ctxt t1 t2 =
kaliszyk@35222
   407
let
kaliszyk@35222
   408
  val t1_str = Syntax.string_of_term ctxt t1
kaliszyk@35222
   409
  val t2_str = Syntax.string_of_term ctxt t2
kaliszyk@35222
   410
  val t1_ty_str = Syntax.string_of_typ ctxt (fastype_of t1)
kaliszyk@35222
   411
  val t2_ty_str = Syntax.string_of_typ ctxt (fastype_of t2)
kaliszyk@35222
   412
in
kaliszyk@35222
   413
  raise LIFT_MATCH (cat_lines [str, t1_str ^ "::" ^ t1_ty_str, t2_str ^ "::" ^ t2_ty_str])
kaliszyk@35222
   414
end
kaliszyk@35222
   415
kaliszyk@35222
   416
(* the major type of All and Ex quantifiers *)
kaliszyk@35222
   417
fun qnt_typ ty = domain_type (domain_type ty)
kaliszyk@35222
   418
kaliszyk@35222
   419
(* Checks that two types match, for example:
kaliszyk@35222
   420
     rty -> rty   matches   qty -> qty *)
kaliszyk@35222
   421
fun matches_typ thy rT qT =
kaliszyk@35222
   422
  if rT = qT then true else
kaliszyk@35222
   423
  case (rT, qT) of
kaliszyk@35222
   424
    (Type (rs, rtys), Type (qs, qtys)) =>
kaliszyk@35222
   425
      if rs = qs then
kaliszyk@35222
   426
        if length rtys <> length qtys then false else
kaliszyk@35222
   427
        forall (fn x => x = true) (map2 (matches_typ thy) rtys qtys)
kaliszyk@35222
   428
      else
kaliszyk@35222
   429
        (case Quotient_Info.quotdata_lookup_raw thy qs of
kaliszyk@35222
   430
          SOME quotinfo => Sign.typ_instance thy (rT, #rtyp quotinfo)
kaliszyk@35222
   431
        | NONE => false)
kaliszyk@35222
   432
  | _ => false
kaliszyk@35222
   433
kaliszyk@35222
   434
kaliszyk@35222
   435
(* produces a regularized version of rtrm
kaliszyk@35222
   436
kaliszyk@35222
   437
   - the result might contain dummyTs
kaliszyk@35222
   438
kaliszyk@35222
   439
   - for regularisation we do not need any
kaliszyk@35222
   440
     special treatment of bound variables
kaliszyk@35222
   441
*)
kaliszyk@35222
   442
fun regularize_trm ctxt (rtrm, qtrm) =
kaliszyk@35222
   443
  case (rtrm, qtrm) of
kaliszyk@35222
   444
    (Abs (x, ty, t), Abs (_, ty', t')) =>
kaliszyk@35222
   445
       let
kaliszyk@35222
   446
         val subtrm = Abs(x, ty, regularize_trm ctxt (t, t'))
kaliszyk@35222
   447
       in
kaliszyk@35222
   448
         if ty = ty' then subtrm
kaliszyk@35222
   449
         else mk_babs $ (mk_resp $ equiv_relation ctxt (ty, ty')) $ subtrm
kaliszyk@35222
   450
       end
kaliszyk@35222
   451
  | (Const (@{const_name "Babs"}, T) $ resrel $ (t as (Abs (_, ty, _))), t' as (Abs (_, ty', _))) =>
kaliszyk@35222
   452
       let
kaliszyk@35222
   453
         val subtrm = regularize_trm ctxt (t, t')
kaliszyk@35222
   454
         val needres = mk_resp $ equiv_relation_chk ctxt (ty, ty')
kaliszyk@35222
   455
       in
kaliszyk@35222
   456
         if resrel <> needres
kaliszyk@35222
   457
         then term_mismatch "regularize (Babs)" ctxt resrel needres
kaliszyk@35222
   458
         else mk_babs $ resrel $ subtrm
kaliszyk@35222
   459
       end
kaliszyk@35222
   460
kaliszyk@35222
   461
  | (Const (@{const_name "All"}, ty) $ t, Const (@{const_name "All"}, ty') $ t') =>
kaliszyk@35222
   462
       let
kaliszyk@35222
   463
         val subtrm = apply_subt (regularize_trm ctxt) (t, t')
kaliszyk@35222
   464
       in
kaliszyk@35222
   465
         if ty = ty' then Const (@{const_name "All"}, ty) $ subtrm
kaliszyk@35222
   466
         else mk_ball $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
kaliszyk@35222
   467
       end
kaliszyk@35222
   468
kaliszyk@35222
   469
  | (Const (@{const_name "Ex"}, ty) $ t, Const (@{const_name "Ex"}, ty') $ t') =>
kaliszyk@35222
   470
       let
kaliszyk@35222
   471
         val subtrm = apply_subt (regularize_trm ctxt) (t, t')
kaliszyk@35222
   472
       in
kaliszyk@35222
   473
         if ty = ty' then Const (@{const_name "Ex"}, ty) $ subtrm
kaliszyk@35222
   474
         else mk_bex $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
kaliszyk@35222
   475
       end
kaliszyk@35222
   476
kaliszyk@35222
   477
  | (Const (@{const_name "Ex1"}, ty) $ (Abs (_, _,
kaliszyk@35222
   478
      (Const (@{const_name "op &"}, _) $ (Const (@{const_name "op :"}, _) $ _ $
kaliszyk@35222
   479
        (Const (@{const_name "Respects"}, _) $ resrel)) $ (t $ _)))),
kaliszyk@35222
   480
     Const (@{const_name "Ex1"}, ty') $ t') =>
kaliszyk@35222
   481
       let
kaliszyk@35222
   482
         val t_ = incr_boundvars (~1) t
kaliszyk@35222
   483
         val subtrm = apply_subt (regularize_trm ctxt) (t_, t')
kaliszyk@35222
   484
         val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
kaliszyk@35222
   485
       in
kaliszyk@35222
   486
         if resrel <> needrel
kaliszyk@35222
   487
         then term_mismatch "regularize (Bex1)" ctxt resrel needrel
kaliszyk@35222
   488
         else mk_bex1_rel $ resrel $ subtrm
kaliszyk@35222
   489
       end
kaliszyk@35222
   490
kaliszyk@35222
   491
  | (Const (@{const_name "Ex1"}, ty) $ t, Const (@{const_name "Ex1"}, ty') $ t') =>
kaliszyk@35222
   492
       let
kaliszyk@35222
   493
         val subtrm = apply_subt (regularize_trm ctxt) (t, t')
kaliszyk@35222
   494
       in
kaliszyk@35222
   495
         if ty = ty' then Const (@{const_name "Ex1"}, ty) $ subtrm
kaliszyk@35222
   496
         else mk_bex1_rel $ (equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
kaliszyk@35222
   497
       end
kaliszyk@35222
   498
kaliszyk@35222
   499
  | (Const (@{const_name "Ball"}, ty) $ (Const (@{const_name "Respects"}, _) $ resrel) $ t,
kaliszyk@35222
   500
     Const (@{const_name "All"}, ty') $ t') =>
kaliszyk@35222
   501
       let
kaliszyk@35222
   502
         val subtrm = apply_subt (regularize_trm ctxt) (t, t')
kaliszyk@35222
   503
         val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
kaliszyk@35222
   504
       in
kaliszyk@35222
   505
         if resrel <> needrel
kaliszyk@35222
   506
         then term_mismatch "regularize (Ball)" ctxt resrel needrel
kaliszyk@35222
   507
         else mk_ball $ (mk_resp $ resrel) $ subtrm
kaliszyk@35222
   508
       end
kaliszyk@35222
   509
kaliszyk@35222
   510
  | (Const (@{const_name "Bex"}, ty) $ (Const (@{const_name "Respects"}, _) $ resrel) $ t,
kaliszyk@35222
   511
     Const (@{const_name "Ex"}, ty') $ t') =>
kaliszyk@35222
   512
       let
kaliszyk@35222
   513
         val subtrm = apply_subt (regularize_trm ctxt) (t, t')
kaliszyk@35222
   514
         val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
kaliszyk@35222
   515
       in
kaliszyk@35222
   516
         if resrel <> needrel
kaliszyk@35222
   517
         then term_mismatch "regularize (Bex)" ctxt resrel needrel
kaliszyk@35222
   518
         else mk_bex $ (mk_resp $ resrel) $ subtrm
kaliszyk@35222
   519
       end
kaliszyk@35222
   520
kaliszyk@35222
   521
  | (Const (@{const_name "Bex1_rel"}, ty) $ resrel $ t, Const (@{const_name "Ex1"}, ty') $ t') =>
kaliszyk@35222
   522
       let
kaliszyk@35222
   523
         val subtrm = apply_subt (regularize_trm ctxt) (t, t')
kaliszyk@35222
   524
         val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
kaliszyk@35222
   525
       in
kaliszyk@35222
   526
         if resrel <> needrel
kaliszyk@35222
   527
         then term_mismatch "regularize (Bex1_res)" ctxt resrel needrel
kaliszyk@35222
   528
         else mk_bex1_rel $ resrel $ subtrm
kaliszyk@35222
   529
       end
kaliszyk@35222
   530
kaliszyk@35222
   531
  | (* equalities need to be replaced by appropriate equivalence relations *)
kaliszyk@35222
   532
    (Const (@{const_name "op ="}, ty), Const (@{const_name "op ="}, ty')) =>
kaliszyk@35222
   533
         if ty = ty' then rtrm
kaliszyk@35222
   534
         else equiv_relation ctxt (domain_type ty, domain_type ty')
kaliszyk@35222
   535
kaliszyk@35222
   536
  | (* in this case we just check whether the given equivalence relation is correct *)
kaliszyk@35222
   537
    (rel, Const (@{const_name "op ="}, ty')) =>
kaliszyk@35222
   538
       let
kaliszyk@35222
   539
         val rel_ty = fastype_of rel
kaliszyk@35222
   540
         val rel' = equiv_relation_chk ctxt (domain_type rel_ty, domain_type ty')
kaliszyk@35222
   541
       in
kaliszyk@35222
   542
         if rel' aconv rel then rtrm
kaliszyk@35222
   543
         else term_mismatch "regularise (relation mismatch)" ctxt rel rel'
kaliszyk@35222
   544
       end
kaliszyk@35222
   545
kaliszyk@35222
   546
  | (_, Const _) =>
kaliszyk@35222
   547
       let
kaliszyk@35222
   548
         val thy = ProofContext.theory_of ctxt
kaliszyk@35222
   549
         fun same_const (Const (s, T)) (Const (s', T')) = (s = s') andalso matches_typ thy T T'
kaliszyk@35222
   550
           | same_const _ _ = false
kaliszyk@35222
   551
       in
kaliszyk@35222
   552
         if same_const rtrm qtrm then rtrm
kaliszyk@35222
   553
         else
kaliszyk@35222
   554
           let
kaliszyk@35222
   555
             val rtrm' = #rconst (qconsts_lookup thy qtrm)
kaliszyk@35222
   556
               handle Quotient_Info.NotFound => term_mismatch "regularize(constant notfound)" ctxt rtrm qtrm
kaliszyk@35222
   557
           in
kaliszyk@35222
   558
             if Pattern.matches thy (rtrm', rtrm)
kaliszyk@35222
   559
             then rtrm else term_mismatch "regularize(constant mismatch)" ctxt rtrm qtrm
kaliszyk@35222
   560
           end
kaliszyk@35222
   561
       end
kaliszyk@35222
   562
kaliszyk@35222
   563
  | (((t1 as Const (@{const_name "split"}, _)) $ Abs (v1, ty, Abs(v1', ty', s1))),
kaliszyk@35222
   564
     ((t2 as Const (@{const_name "split"}, _)) $ Abs (v2, _ , Abs(v2', _  , s2)))) =>
kaliszyk@35222
   565
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, Abs (v1', ty', regularize_trm ctxt (s1, s2)))
kaliszyk@35222
   566
kaliszyk@35222
   567
  | (((t1 as Const (@{const_name "split"}, _)) $ Abs (v1, ty, s1)),
kaliszyk@35222
   568
     ((t2 as Const (@{const_name "split"}, _)) $ Abs (v2, _ , s2))) =>
kaliszyk@35222
   569
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, regularize_trm ctxt (s1, s2))
kaliszyk@35222
   570
kaliszyk@35222
   571
  | (t1 $ t2, t1' $ t2') =>
kaliszyk@35222
   572
       regularize_trm ctxt (t1, t1') $ regularize_trm ctxt (t2, t2')
kaliszyk@35222
   573
kaliszyk@35222
   574
  | (Bound i, Bound i') =>
kaliszyk@35222
   575
       if i = i' then rtrm
kaliszyk@35222
   576
       else raise (LIFT_MATCH "regularize (bounds mismatch)")
kaliszyk@35222
   577
kaliszyk@35222
   578
  | _ =>
kaliszyk@35222
   579
       let
kaliszyk@35222
   580
         val rtrm_str = Syntax.string_of_term ctxt rtrm
kaliszyk@35222
   581
         val qtrm_str = Syntax.string_of_term ctxt qtrm
kaliszyk@35222
   582
       in
kaliszyk@35222
   583
         raise (LIFT_MATCH ("regularize failed (default: " ^ rtrm_str ^ "," ^ qtrm_str ^ ")"))
kaliszyk@35222
   584
       end
kaliszyk@35222
   585
kaliszyk@35222
   586
fun regularize_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   587
  regularize_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   588
  |> Syntax.check_term ctxt
kaliszyk@35222
   589
kaliszyk@35222
   590
kaliszyk@35222
   591
kaliszyk@35222
   592
(*** Rep/Abs Injection ***)
kaliszyk@35222
   593
kaliszyk@35222
   594
(*
kaliszyk@35222
   595
Injection of Rep/Abs means:
kaliszyk@35222
   596
kaliszyk@35222
   597
  For abstractions:
kaliszyk@35222
   598
kaliszyk@35222
   599
  * If the type of the abstraction needs lifting, then we add Rep/Abs
kaliszyk@35222
   600
    around the abstraction; otherwise we leave it unchanged.
kaliszyk@35222
   601
kaliszyk@35222
   602
  For applications:
kaliszyk@35222
   603
kaliszyk@35222
   604
  * If the application involves a bounded quantifier, we recurse on
kaliszyk@35222
   605
    the second argument. If the application is a bounded abstraction,
kaliszyk@35222
   606
    we always put an Rep/Abs around it (since bounded abstractions
kaliszyk@35222
   607
    are assumed to always need lifting). Otherwise we recurse on both
kaliszyk@35222
   608
    arguments.
kaliszyk@35222
   609
kaliszyk@35222
   610
  For constants:
kaliszyk@35222
   611
kaliszyk@35222
   612
  * If the constant is (op =), we leave it always unchanged.
kaliszyk@35222
   613
    Otherwise the type of the constant needs lifting, we put
kaliszyk@35222
   614
    and Rep/Abs around it.
kaliszyk@35222
   615
kaliszyk@35222
   616
  For free variables:
kaliszyk@35222
   617
kaliszyk@35222
   618
  * We put a Rep/Abs around it if the type needs lifting.
kaliszyk@35222
   619
kaliszyk@35222
   620
  Vars case cannot occur.
kaliszyk@35222
   621
*)
kaliszyk@35222
   622
kaliszyk@35222
   623
fun mk_repabs ctxt (T, T') trm =
kaliszyk@35222
   624
  absrep_fun RepF ctxt (T, T') $ (absrep_fun AbsF ctxt (T, T') $ trm)
kaliszyk@35222
   625
kaliszyk@35222
   626
fun inj_repabs_err ctxt msg rtrm qtrm =
kaliszyk@35222
   627
let
kaliszyk@35222
   628
  val rtrm_str = Syntax.string_of_term ctxt rtrm
kaliszyk@35222
   629
  val qtrm_str = Syntax.string_of_term ctxt qtrm
kaliszyk@35222
   630
in
kaliszyk@35222
   631
  raise LIFT_MATCH (space_implode " " [msg, quote rtrm_str, "and", quote qtrm_str])
kaliszyk@35222
   632
end
kaliszyk@35222
   633
kaliszyk@35222
   634
kaliszyk@35222
   635
(* bound variables need to be treated properly,
kaliszyk@35222
   636
   as the type of subterms needs to be calculated   *)
kaliszyk@35222
   637
fun inj_repabs_trm ctxt (rtrm, qtrm) =
kaliszyk@35222
   638
 case (rtrm, qtrm) of
kaliszyk@35222
   639
    (Const (@{const_name "Ball"}, T) $ r $ t, Const (@{const_name "All"}, _) $ t') =>
kaliszyk@35222
   640
       Const (@{const_name "Ball"}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   641
kaliszyk@35222
   642
  | (Const (@{const_name "Bex"}, T) $ r $ t, Const (@{const_name "Ex"}, _) $ t') =>
kaliszyk@35222
   643
       Const (@{const_name "Bex"}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   644
kaliszyk@35222
   645
  | (Const (@{const_name "Babs"}, T) $ r $ t, t' as (Abs _)) =>
kaliszyk@35222
   646
      let
kaliszyk@35222
   647
        val rty = fastype_of rtrm
kaliszyk@35222
   648
        val qty = fastype_of qtrm
kaliszyk@35222
   649
      in
kaliszyk@35222
   650
        mk_repabs ctxt (rty, qty) (Const (@{const_name "Babs"}, T) $ r $ (inj_repabs_trm ctxt (t, t')))
kaliszyk@35222
   651
      end
kaliszyk@35222
   652
kaliszyk@35222
   653
  | (Abs (x, T, t), Abs (x', T', t')) =>
kaliszyk@35222
   654
      let
kaliszyk@35222
   655
        val rty = fastype_of rtrm
kaliszyk@35222
   656
        val qty = fastype_of qtrm
kaliszyk@35222
   657
        val (y, s) = Term.dest_abs (x, T, t)
kaliszyk@35222
   658
        val (_, s') = Term.dest_abs (x', T', t')
kaliszyk@35222
   659
        val yvar = Free (y, T)
kaliszyk@35222
   660
        val result = Term.lambda_name (y, yvar) (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   661
      in
kaliszyk@35222
   662
        if rty = qty then result
kaliszyk@35222
   663
        else mk_repabs ctxt (rty, qty) result
kaliszyk@35222
   664
      end
kaliszyk@35222
   665
kaliszyk@35222
   666
  | (t $ s, t' $ s') =>
kaliszyk@35222
   667
       (inj_repabs_trm ctxt (t, t')) $ (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   668
kaliszyk@35222
   669
  | (Free (_, T), Free (_, T')) =>
kaliszyk@35222
   670
        if T = T' then rtrm
kaliszyk@35222
   671
        else mk_repabs ctxt (T, T') rtrm
kaliszyk@35222
   672
kaliszyk@35222
   673
  | (_, Const (@{const_name "op ="}, _)) => rtrm
kaliszyk@35222
   674
kaliszyk@35222
   675
  | (_, Const (_, T')) =>
kaliszyk@35222
   676
      let
kaliszyk@35222
   677
        val rty = fastype_of rtrm
kaliszyk@35222
   678
      in
kaliszyk@35222
   679
        if rty = T' then rtrm
kaliszyk@35222
   680
        else mk_repabs ctxt (rty, T') rtrm
kaliszyk@35222
   681
      end
kaliszyk@35222
   682
kaliszyk@35222
   683
  | _ => inj_repabs_err ctxt "injection (default):" rtrm qtrm
kaliszyk@35222
   684
kaliszyk@35222
   685
fun inj_repabs_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   686
  inj_repabs_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   687
  |> Syntax.check_term ctxt
kaliszyk@35222
   688
kaliszyk@35222
   689
kaliszyk@35222
   690
kaliszyk@35222
   691
(*** Wrapper for automatically transforming an rthm into a qthm ***)
kaliszyk@35222
   692
kaliszyk@35222
   693
(* subst_tys takes a list of (rty, qty) substitution pairs
kaliszyk@35222
   694
   and replaces all occurences of rty in the given type
kaliszyk@35222
   695
   by appropriate qty, with substitution *)
kaliszyk@35222
   696
fun subst_ty thy ty (rty, qty) r =
kaliszyk@35222
   697
  if r <> NONE then r else
kaliszyk@35222
   698
  case try (Sign.typ_match thy (rty, ty)) Vartab.empty of
kaliszyk@35222
   699
    SOME inst => SOME (Envir.subst_type inst qty)
kaliszyk@35222
   700
  | NONE => NONE
kaliszyk@35222
   701
fun subst_tys thy substs ty =
kaliszyk@35222
   702
  case fold (subst_ty thy ty) substs NONE of
kaliszyk@35222
   703
    SOME ty => ty
kaliszyk@35222
   704
  | NONE =>
kaliszyk@35222
   705
      (case ty of
kaliszyk@35222
   706
        Type (s, tys) => Type (s, map (subst_tys thy substs) tys)
kaliszyk@35222
   707
      | x => x)
kaliszyk@35222
   708
kaliszyk@35222
   709
(* subst_trms takes a list of (rtrm, qtrm) substitution pairs
kaliszyk@35222
   710
   and if the given term matches any of the raw terms it
kaliszyk@35222
   711
   returns the appropriate qtrm instantiated. If none of
kaliszyk@35222
   712
   them matched it returns NONE. *)
kaliszyk@35222
   713
fun subst_trm thy t (rtrm, qtrm) s =
kaliszyk@35222
   714
  if s <> NONE then s else
kaliszyk@35222
   715
    case try (Pattern.match thy (rtrm, t)) (Vartab.empty, Vartab.empty) of
kaliszyk@35222
   716
      SOME inst => SOME (Envir.subst_term inst qtrm)
kaliszyk@35222
   717
    | NONE => NONE;
kaliszyk@35222
   718
fun subst_trms thy substs t = fold (subst_trm thy t) substs NONE
kaliszyk@35222
   719
kaliszyk@35222
   720
(* prepares type and term substitution pairs to be used by above
kaliszyk@35222
   721
   functions that let replace all raw constructs by appropriate
kaliszyk@35222
   722
   lifted counterparts. *)
kaliszyk@35990
   723
fun get_ty_trm_substs qtys ctxt =
kaliszyk@35222
   724
let
kaliszyk@35222
   725
  val thy = ProofContext.theory_of ctxt
kaliszyk@35222
   726
  val quot_infos  = Quotient_Info.quotdata_dest ctxt
kaliszyk@35222
   727
  val const_infos = Quotient_Info.qconsts_dest ctxt
kaliszyk@35990
   728
  val all_ty_substs = map (fn ri => (#rtyp ri, #qtyp ri)) quot_infos
kaliszyk@35990
   729
  val ty_substs =
kaliszyk@35990
   730
    if qtys = [] then all_ty_substs else
haftmann@36692
   731
    filter (fn (_, qty) => member (op =) qtys qty) all_ty_substs
kaliszyk@35222
   732
  val const_substs = map (fn ci => (#rconst ci, #qconst ci)) const_infos
kaliszyk@35222
   733
  fun rel_eq rel = HOLogic.eq_const (subst_tys thy ty_substs (domain_type (fastype_of rel)))
kaliszyk@35222
   734
  val rel_substs = map (fn ri => (#equiv_rel ri, rel_eq (#equiv_rel ri))) quot_infos
kaliszyk@35990
   735
  fun valid_trm_subst (rt, qt) = (subst_tys thy ty_substs (fastype_of rt) = fastype_of qt)
kaliszyk@35990
   736
  val all_trm_substs = const_substs @ rel_substs
kaliszyk@35222
   737
in
kaliszyk@35990
   738
  (ty_substs, filter valid_trm_subst all_trm_substs)
kaliszyk@35222
   739
end
kaliszyk@35222
   740
kaliszyk@35990
   741
fun quotient_lift_const qtys (b, t) ctxt =
kaliszyk@35222
   742
let
kaliszyk@35222
   743
  val thy = ProofContext.theory_of ctxt
kaliszyk@35990
   744
  val (ty_substs, _) = get_ty_trm_substs qtys ctxt;
kaliszyk@35222
   745
  val (_, ty) = dest_Const t;
kaliszyk@35222
   746
  val nty = subst_tys thy ty_substs ty;
kaliszyk@35222
   747
in
kaliszyk@35222
   748
  Free(b, nty)
kaliszyk@35222
   749
end
kaliszyk@35222
   750
kaliszyk@35222
   751
(*
kaliszyk@35222
   752
Takes a term and
kaliszyk@35222
   753
kaliszyk@35222
   754
* replaces raw constants by the quotient constants
kaliszyk@35222
   755
kaliszyk@35222
   756
* replaces equivalence relations by equalities
kaliszyk@35222
   757
kaliszyk@35222
   758
* replaces raw types by the quotient types
kaliszyk@35222
   759
kaliszyk@35222
   760
*)
kaliszyk@35222
   761
kaliszyk@35990
   762
fun quotient_lift_all qtys ctxt t =
kaliszyk@35222
   763
let
kaliszyk@35222
   764
  val thy = ProofContext.theory_of ctxt
kaliszyk@35990
   765
  val (ty_substs, substs) = get_ty_trm_substs qtys ctxt
kaliszyk@35222
   766
  fun lift_aux t =
kaliszyk@35222
   767
    case subst_trms thy substs t of
kaliszyk@35222
   768
      SOME x => x
kaliszyk@35222
   769
    | NONE =>
kaliszyk@35222
   770
      (case t of
kaliszyk@35222
   771
        a $ b => lift_aux a $ lift_aux b
kaliszyk@35222
   772
      | Abs(a, ty, s) =>
kaliszyk@35222
   773
          let
kaliszyk@35222
   774
            val (y, s') = Term.dest_abs (a, ty, s)
kaliszyk@35222
   775
            val nty = subst_tys thy ty_substs ty
kaliszyk@35222
   776
          in
kaliszyk@35222
   777
            Abs(y, nty, abstract_over (Free (y, nty), lift_aux s'))
kaliszyk@35222
   778
          end
kaliszyk@35222
   779
      | Free(n, ty) => Free(n, subst_tys thy ty_substs ty)
kaliszyk@35222
   780
      | Var(n, ty) => Var(n, subst_tys thy ty_substs ty)
kaliszyk@35222
   781
      | Bound i => Bound i
kaliszyk@35222
   782
      | Const(s, ty) => Const(s, subst_tys thy ty_substs ty))
kaliszyk@35222
   783
in
kaliszyk@35222
   784
  lift_aux t
kaliszyk@35222
   785
end
kaliszyk@35222
   786
kaliszyk@35222
   787
end; (* structure *)