src/HOL/Tools/Quotient/quotient_term.ML
author bulwahn
Thu Oct 27 13:50:55 2011 +0200 (2011-10-27)
changeset 45273 728ed9d28c63
parent 45272 5995ab88a00f
child 45274 252cd58847e0
permissions -rw-r--r--
respecting isabelle's programming style in the quotient package by simplifying map_lookup function for data access
haftmann@37744
     1
(*  Title:      HOL/Tools/Quotient/quotient_term.ML
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
urbanc@38624
    29
  val derive_qtyp: Proof.context -> typ list -> typ -> typ
urbanc@38624
    30
  val derive_qtrm: Proof.context -> typ list -> term -> term
urbanc@38624
    31
  val derive_rtyp: Proof.context -> typ list -> typ -> typ
urbanc@38624
    32
  val derive_rtrm: Proof.context -> typ list -> term -> term
kaliszyk@35222
    33
end;
kaliszyk@35222
    34
kaliszyk@35222
    35
structure Quotient_Term: QUOTIENT_TERM =
kaliszyk@35222
    36
struct
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
haftmann@37677
    55
fun is_identity (Const (@{const_name id}, _)) = true
kaliszyk@35222
    56
  | is_identity _ = false
kaliszyk@35222
    57
haftmann@37677
    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
haftmann@37677
    62
    AbsF => Const (@{const_name comp}, dummyT) $ trm1 $ trm2
haftmann@37677
    63
  | RepF => Const (@{const_name comp}, dummyT) $ trm2 $ trm1
kaliszyk@35222
    64
kaliszyk@35222
    65
fun get_mapfun ctxt s =
bulwahn@45273
    66
  case Quotient_Info.maps_lookup (Proof_Context.theory_of ctxt) s of
bulwahn@45273
    67
    SOME map_data => Const (#mapfun map_data, dummyT)
bulwahn@45273
    68
  | NONE => raise LIFT_MATCH ("No map function for type " ^ quote s ^ " found.")
kaliszyk@35222
    69
kaliszyk@35222
    70
(* makes a Free out of a TVar *)
kaliszyk@35222
    71
fun mk_Free (TVar ((x, i), _)) = Free (unprefix "'" x ^ string_of_int i, dummyT)
kaliszyk@35222
    72
kaliszyk@35222
    73
(* produces an aggregate map function for the
kaliszyk@35222
    74
   rty-part of a quotient definition; abstracts
kaliszyk@35222
    75
   over all variables listed in vs (these variables
kaliszyk@35222
    76
   correspond to the type variables in rty)
kaliszyk@35222
    77
kaliszyk@35222
    78
   for example for: (?'a list * ?'b)
kaliszyk@35222
    79
   it produces:     %a b. prod_map (map a) b
kaliszyk@35222
    80
*)
kaliszyk@35222
    81
fun mk_mapfun ctxt vs rty =
wenzelm@41444
    82
  let
wenzelm@41444
    83
    val vs' = map mk_Free vs
kaliszyk@35222
    84
wenzelm@41444
    85
    fun mk_mapfun_aux rty =
wenzelm@41444
    86
      case rty of
wenzelm@41444
    87
        TVar _ => mk_Free rty
wenzelm@41444
    88
      | Type (_, []) => mk_identity rty
wenzelm@41444
    89
      | Type (s, tys) => list_comb (get_mapfun ctxt s, map mk_mapfun_aux tys)
wenzelm@41444
    90
      | _ => raise LIFT_MATCH "mk_mapfun (default)"
wenzelm@41444
    91
  in
wenzelm@41444
    92
    fold_rev Term.lambda vs' (mk_mapfun_aux rty)
wenzelm@41444
    93
  end
kaliszyk@35222
    94
kaliszyk@35222
    95
(* looks up the (varified) rty and qty for
kaliszyk@35222
    96
   a quotient definition
kaliszyk@35222
    97
*)
kaliszyk@35222
    98
fun get_rty_qty ctxt s =
bulwahn@45272
    99
  case Quotient_Info.quotdata_lookup (Proof_Context.theory_of ctxt) s of
bulwahn@45272
   100
    SOME qdata => (#rtyp qdata, #qtyp qdata)
bulwahn@45272
   101
  | NONE => raise LIFT_MATCH ("No quotient type " ^ quote s ^ " found.");
kaliszyk@35222
   102
kaliszyk@35222
   103
(* takes two type-environments and looks
kaliszyk@35222
   104
   up in both of them the variable v, which
kaliszyk@35222
   105
   must be listed in the environment
kaliszyk@35222
   106
*)
kaliszyk@35222
   107
fun double_lookup rtyenv qtyenv v =
wenzelm@41444
   108
  let
wenzelm@41444
   109
    val v' = fst (dest_TVar v)
wenzelm@41444
   110
  in
wenzelm@41444
   111
    (snd (the (Vartab.lookup rtyenv v')), snd (the (Vartab.lookup qtyenv v')))
wenzelm@41444
   112
  end
kaliszyk@35222
   113
kaliszyk@35222
   114
(* matches a type pattern with a type *)
kaliszyk@35222
   115
fun match ctxt err ty_pat ty =
wenzelm@41444
   116
  let
wenzelm@42361
   117
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   118
  in
wenzelm@41444
   119
    Sign.typ_match thy (ty_pat, ty) Vartab.empty
wenzelm@41444
   120
      handle Type.TYPE_MATCH => err ctxt ty_pat ty
wenzelm@41444
   121
  end
kaliszyk@35222
   122
kaliszyk@35222
   123
(* produces the rep or abs constant for a qty *)
kaliszyk@35222
   124
fun absrep_const flag ctxt qty_str =
wenzelm@41444
   125
  let
wenzelm@41444
   126
    val qty_name = Long_Name.base_name qty_str
wenzelm@41444
   127
    val qualifier = Long_Name.qualifier qty_str
wenzelm@41444
   128
  in
wenzelm@41444
   129
    case flag of
wenzelm@41444
   130
      AbsF => Const (Long_Name.qualify qualifier ("abs_" ^ qty_name), dummyT)
wenzelm@41444
   131
    | RepF => Const (Long_Name.qualify qualifier ("rep_" ^ qty_name), dummyT)
wenzelm@41444
   132
  end
kaliszyk@35222
   133
kaliszyk@35222
   134
(* Lets Nitpick represent elements of quotient types as elements of the raw type *)
kaliszyk@35222
   135
fun absrep_const_chk flag ctxt qty_str =
kaliszyk@35222
   136
  Syntax.check_term ctxt (absrep_const flag ctxt qty_str)
kaliszyk@35222
   137
kaliszyk@35222
   138
fun absrep_match_err ctxt ty_pat ty =
wenzelm@41444
   139
  let
wenzelm@41444
   140
    val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
wenzelm@41444
   141
    val ty_str = Syntax.string_of_typ ctxt ty
wenzelm@41444
   142
  in
wenzelm@41444
   143
    raise LIFT_MATCH (space_implode " "
wenzelm@41444
   144
      ["absrep_fun (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
wenzelm@41444
   145
  end
kaliszyk@35222
   146
kaliszyk@35222
   147
kaliszyk@35222
   148
(** generation of an aggregate absrep function **)
kaliszyk@35222
   149
kaliszyk@35222
   150
(* - In case of equal types we just return the identity.
kaliszyk@35222
   151
kaliszyk@35222
   152
   - In case of TFrees we also return the identity.
kaliszyk@35222
   153
kaliszyk@35222
   154
   - In case of function types we recurse taking
kaliszyk@35222
   155
     the polarity change into account.
kaliszyk@35222
   156
kaliszyk@35222
   157
   - If the type constructors are equal, we recurse for the
kaliszyk@35222
   158
     arguments and build the appropriate map function.
kaliszyk@35222
   159
kaliszyk@35222
   160
   - If the type constructors are unequal, there must be an
kaliszyk@35222
   161
     instance of quotient types:
kaliszyk@35222
   162
kaliszyk@35222
   163
       - we first look up the corresponding rty_pat and qty_pat
kaliszyk@35222
   164
         from the quotient definition; the arguments of qty_pat
kaliszyk@35222
   165
         must be some distinct TVars
kaliszyk@35222
   166
       - we then match the rty_pat with rty and qty_pat with qty;
kaliszyk@35222
   167
         if matching fails the types do not correspond -> error
kaliszyk@35222
   168
       - the matching produces two environments; we look up the
kaliszyk@35222
   169
         assignments for the qty_pat variables and recurse on the
kaliszyk@35222
   170
         assignments
kaliszyk@35222
   171
       - we prefix the aggregate map function for the rty_pat,
kaliszyk@35222
   172
         which is an abstraction over all type variables
kaliszyk@35222
   173
       - finally we compose the result with the appropriate
kaliszyk@35222
   174
         absrep function in case at least one argument produced
kaliszyk@35222
   175
         a non-identity function /
kaliszyk@35222
   176
         otherwise we just return the appropriate absrep
kaliszyk@35222
   177
         function
kaliszyk@35222
   178
kaliszyk@35222
   179
     The composition is necessary for types like
kaliszyk@35222
   180
kaliszyk@35222
   181
        ('a list) list / ('a foo) foo
kaliszyk@35222
   182
kaliszyk@35222
   183
     The matching is necessary for types like
kaliszyk@35222
   184
kaliszyk@35222
   185
        ('a * 'a) list / 'a bar
kaliszyk@35222
   186
kaliszyk@35222
   187
     The test is necessary in order to eliminate superfluous
kaliszyk@35222
   188
     identity maps.
kaliszyk@35222
   189
*)
kaliszyk@35222
   190
kaliszyk@35222
   191
fun absrep_fun flag ctxt (rty, qty) =
kaliszyk@35222
   192
  if rty = qty
kaliszyk@35222
   193
  then mk_identity rty
kaliszyk@35222
   194
  else
kaliszyk@35222
   195
    case (rty, qty) of
kaliszyk@35222
   196
      (Type ("fun", [ty1, ty2]), Type ("fun", [ty1', ty2'])) =>
kaliszyk@35222
   197
        let
kaliszyk@35222
   198
          val arg1 = absrep_fun (negF flag) ctxt (ty1, ty1')
kaliszyk@35222
   199
          val arg2 = absrep_fun flag ctxt (ty2, ty2')
kaliszyk@35222
   200
        in
kaliszyk@35222
   201
          list_comb (get_mapfun ctxt "fun", [arg1, arg2])
kaliszyk@35222
   202
        end
kaliszyk@44413
   203
(* FIXME: use type_name antiquotation if set type becomes explicit *)
kaliszyk@44413
   204
    | (Type ("Set.set", [ty]), Type ("Set.set", [ty'])) =>
kaliszyk@44413
   205
        let
kaliszyk@44413
   206
          val arg = absrep_fun (negF flag) ctxt (ty, ty')
kaliszyk@44413
   207
        in
kaliszyk@44413
   208
          get_mapfun ctxt "Set.set" $ arg
kaliszyk@44413
   209
        end
kaliszyk@35222
   210
    | (Type (s, tys), Type (s', tys')) =>
kaliszyk@35222
   211
        if s = s'
kaliszyk@35222
   212
        then
wenzelm@41444
   213
          let
wenzelm@41444
   214
            val args = map (absrep_fun flag ctxt) (tys ~~ tys')
wenzelm@41444
   215
          in
wenzelm@41444
   216
            list_comb (get_mapfun ctxt s, args)
wenzelm@41444
   217
          end
kaliszyk@35222
   218
        else
wenzelm@41444
   219
          let
wenzelm@41444
   220
            val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty ctxt s'
wenzelm@41444
   221
            val rtyenv = match ctxt absrep_match_err rty_pat rty
wenzelm@41444
   222
            val qtyenv = match ctxt absrep_match_err qty_pat qty
wenzelm@41444
   223
            val args_aux = map (double_lookup rtyenv qtyenv) vs
wenzelm@41444
   224
            val args = map (absrep_fun flag ctxt) args_aux
wenzelm@41444
   225
          in
wenzelm@41444
   226
            if forall is_identity args
wenzelm@41444
   227
            then absrep_const flag ctxt s'
wenzelm@41444
   228
            else
wenzelm@41444
   229
              let
wenzelm@41444
   230
                val map_fun = mk_mapfun ctxt vs rty_pat
wenzelm@41444
   231
                val result = list_comb (map_fun, args)
wenzelm@41444
   232
              in
wenzelm@41444
   233
                mk_fun_compose flag (absrep_const flag ctxt s', result)
wenzelm@41444
   234
              end
wenzelm@41444
   235
          end
kaliszyk@35222
   236
    | (TFree x, TFree x') =>
kaliszyk@35222
   237
        if x = x'
kaliszyk@35222
   238
        then mk_identity rty
kaliszyk@35222
   239
        else raise (LIFT_MATCH "absrep_fun (frees)")
kaliszyk@35222
   240
    | (TVar _, TVar _) => raise (LIFT_MATCH "absrep_fun (vars)")
kaliszyk@35222
   241
    | _ => raise (LIFT_MATCH "absrep_fun (default)")
kaliszyk@35222
   242
kaliszyk@35222
   243
fun absrep_fun_chk flag ctxt (rty, qty) =
kaliszyk@35222
   244
  absrep_fun flag ctxt (rty, qty)
kaliszyk@35222
   245
  |> Syntax.check_term ctxt
kaliszyk@35222
   246
kaliszyk@35222
   247
kaliszyk@35222
   248
kaliszyk@35222
   249
kaliszyk@35222
   250
(*** Aggregate Equivalence Relation ***)
kaliszyk@35222
   251
kaliszyk@35222
   252
kaliszyk@35222
   253
(* works very similar to the absrep generation,
kaliszyk@35222
   254
   except there is no need for polarities
kaliszyk@35222
   255
*)
kaliszyk@35222
   256
kaliszyk@35222
   257
(* instantiates TVars so that the term is of type ty *)
kaliszyk@35222
   258
fun force_typ ctxt trm ty =
wenzelm@41444
   259
  let
wenzelm@42361
   260
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   261
    val trm_ty = fastype_of trm
wenzelm@41444
   262
    val ty_inst = Sign.typ_match thy (trm_ty, ty) Vartab.empty
wenzelm@41444
   263
  in
wenzelm@41444
   264
    map_types (Envir.subst_type ty_inst) trm
wenzelm@41444
   265
  end
kaliszyk@35222
   266
haftmann@38864
   267
fun is_eq (Const (@{const_name HOL.eq}, _)) = true
kaliszyk@35222
   268
  | is_eq _ = false
kaliszyk@35222
   269
kaliszyk@35222
   270
fun mk_rel_compose (trm1, trm2) =
wenzelm@35402
   271
  Const (@{const_abbrev "rel_conj"}, dummyT) $ trm1 $ trm2
kaliszyk@35222
   272
kaliszyk@35222
   273
fun get_relmap ctxt s =
bulwahn@45273
   274
  case Quotient_Info.maps_lookup (Proof_Context.theory_of ctxt) s of
bulwahn@45273
   275
    SOME map_data => Const (#relmap map_data, dummyT)
bulwahn@45273
   276
  | NONE => raise LIFT_MATCH ("get_relmap (no relation map function found for type " ^ s ^ ")")
kaliszyk@35222
   277
kaliszyk@35222
   278
fun mk_relmap ctxt vs rty =
wenzelm@41444
   279
  let
wenzelm@41444
   280
    val vs' = map (mk_Free) vs
kaliszyk@35222
   281
wenzelm@41444
   282
    fun mk_relmap_aux rty =
wenzelm@41444
   283
      case rty of
wenzelm@41444
   284
        TVar _ => mk_Free rty
wenzelm@41444
   285
      | Type (_, []) => HOLogic.eq_const rty
wenzelm@41444
   286
      | Type (s, tys) => list_comb (get_relmap ctxt s, map mk_relmap_aux tys)
wenzelm@41444
   287
      | _ => raise LIFT_MATCH ("mk_relmap (default)")
wenzelm@41444
   288
  in
wenzelm@41444
   289
    fold_rev Term.lambda vs' (mk_relmap_aux rty)
wenzelm@41444
   290
  end
kaliszyk@35222
   291
kaliszyk@35222
   292
fun get_equiv_rel ctxt s =
bulwahn@45272
   293
  case Quotient_Info.quotdata_lookup (Proof_Context.theory_of ctxt) s of
bulwahn@45272
   294
      SOME qdata => #equiv_rel qdata
bulwahn@45272
   295
    | NONE => raise LIFT_MATCH ("get_quotdata (no quotient found for type " ^ s ^ ")")
kaliszyk@35222
   296
kaliszyk@35222
   297
fun equiv_match_err ctxt ty_pat ty =
wenzelm@41444
   298
  let
wenzelm@41444
   299
    val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
wenzelm@41444
   300
    val ty_str = Syntax.string_of_typ ctxt ty
wenzelm@41444
   301
  in
wenzelm@41444
   302
    raise LIFT_MATCH (space_implode " "
wenzelm@41444
   303
      ["equiv_relation (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
wenzelm@41444
   304
  end
kaliszyk@35222
   305
kaliszyk@35222
   306
(* builds the aggregate equivalence relation
kaliszyk@35222
   307
   that will be the argument of Respects
kaliszyk@35222
   308
*)
kaliszyk@35222
   309
fun equiv_relation ctxt (rty, qty) =
kaliszyk@35222
   310
  if rty = qty
kaliszyk@35222
   311
  then HOLogic.eq_const rty
kaliszyk@35222
   312
  else
kaliszyk@35222
   313
    case (rty, qty) of
kaliszyk@35222
   314
      (Type (s, tys), Type (s', tys')) =>
wenzelm@41444
   315
        if s = s'
wenzelm@41444
   316
        then
wenzelm@41444
   317
          let
wenzelm@41444
   318
            val args = map (equiv_relation ctxt) (tys ~~ tys')
wenzelm@41444
   319
          in
wenzelm@41444
   320
            list_comb (get_relmap ctxt s, args)
wenzelm@41444
   321
          end
wenzelm@41444
   322
        else
wenzelm@41444
   323
          let
wenzelm@41444
   324
            val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty ctxt s'
wenzelm@41444
   325
            val rtyenv = match ctxt equiv_match_err rty_pat rty
wenzelm@41444
   326
            val qtyenv = match ctxt equiv_match_err qty_pat qty
wenzelm@41444
   327
            val args_aux = map (double_lookup rtyenv qtyenv) vs
wenzelm@41444
   328
            val args = map (equiv_relation ctxt) args_aux
wenzelm@41444
   329
            val eqv_rel = get_equiv_rel ctxt s'
wenzelm@41444
   330
            val eqv_rel' = force_typ ctxt eqv_rel ([rty, rty] ---> @{typ bool})
wenzelm@41444
   331
          in
wenzelm@41444
   332
            if forall is_eq args
wenzelm@41444
   333
            then eqv_rel'
wenzelm@41444
   334
            else
wenzelm@41444
   335
              let
wenzelm@41444
   336
                val rel_map = mk_relmap ctxt vs rty_pat
wenzelm@41444
   337
                val result = list_comb (rel_map, args)
wenzelm@41444
   338
              in
wenzelm@41444
   339
                mk_rel_compose (result, eqv_rel')
wenzelm@41444
   340
              end
wenzelm@41444
   341
          end
wenzelm@41444
   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 =
wenzelm@41444
   407
  let
wenzelm@41444
   408
    val t1_str = Syntax.string_of_term ctxt t1
wenzelm@41444
   409
    val t2_str = Syntax.string_of_term ctxt t2
wenzelm@41444
   410
    val t1_ty_str = Syntax.string_of_typ ctxt (fastype_of t1)
wenzelm@41444
   411
    val t2_ty_str = Syntax.string_of_typ ctxt (fastype_of t2)
wenzelm@41444
   412
  in
wenzelm@41444
   413
    raise LIFT_MATCH (cat_lines [str, t1_str ^ "::" ^ t1_ty_str, t2_str ^ "::" ^ t2_ty_str])
wenzelm@41444
   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 =
wenzelm@41444
   422
  if rT = qT then true
wenzelm@41444
   423
  else
wenzelm@41444
   424
    (case (rT, qT) of
wenzelm@41444
   425
      (Type (rs, rtys), Type (qs, qtys)) =>
wenzelm@41444
   426
        if rs = qs then
wenzelm@41444
   427
          if length rtys <> length qtys then false
wenzelm@41444
   428
          else forall (fn x => x = true) (map2 (matches_typ thy) rtys qtys)
wenzelm@41444
   429
        else
bulwahn@45272
   430
          (case Quotient_Info.quotdata_lookup thy qs of
wenzelm@41444
   431
            SOME quotinfo => Sign.typ_instance thy (rT, #rtyp quotinfo)
wenzelm@41444
   432
          | NONE => false)
wenzelm@41444
   433
    | _ => false)
kaliszyk@35222
   434
kaliszyk@35222
   435
kaliszyk@35222
   436
(* produces a regularized version of rtrm
kaliszyk@35222
   437
kaliszyk@35222
   438
   - the result might contain dummyTs
kaliszyk@35222
   439
urbanc@38718
   440
   - for regularization we do not need any
kaliszyk@35222
   441
     special treatment of bound variables
kaliszyk@35222
   442
*)
kaliszyk@35222
   443
fun regularize_trm ctxt (rtrm, qtrm) =
kaliszyk@35222
   444
  case (rtrm, qtrm) of
kaliszyk@35222
   445
    (Abs (x, ty, t), Abs (_, ty', t')) =>
wenzelm@41444
   446
      let
wenzelm@41444
   447
        val subtrm = Abs(x, ty, regularize_trm ctxt (t, t'))
wenzelm@41444
   448
      in
wenzelm@41444
   449
        if ty = ty' then subtrm
wenzelm@41444
   450
        else mk_babs $ (mk_resp $ equiv_relation ctxt (ty, ty')) $ subtrm
wenzelm@41444
   451
      end
haftmann@37677
   452
  | (Const (@{const_name Babs}, T) $ resrel $ (t as (Abs (_, ty, _))), t' as (Abs (_, ty', _))) =>
wenzelm@41444
   453
      let
wenzelm@41444
   454
        val subtrm = regularize_trm ctxt (t, t')
wenzelm@41444
   455
        val needres = mk_resp $ equiv_relation_chk ctxt (ty, ty')
wenzelm@41444
   456
      in
wenzelm@41444
   457
        if resrel <> needres
wenzelm@41444
   458
        then term_mismatch "regularize (Babs)" ctxt resrel needres
wenzelm@41444
   459
        else mk_babs $ resrel $ subtrm
wenzelm@41444
   460
      end
kaliszyk@35222
   461
haftmann@37677
   462
  | (Const (@{const_name All}, ty) $ t, Const (@{const_name All}, ty') $ t') =>
wenzelm@41444
   463
      let
wenzelm@41444
   464
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   465
      in
wenzelm@41444
   466
        if ty = ty' then Const (@{const_name All}, ty) $ subtrm
wenzelm@41444
   467
        else mk_ball $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   468
      end
kaliszyk@35222
   469
haftmann@37677
   470
  | (Const (@{const_name Ex}, ty) $ t, Const (@{const_name Ex}, ty') $ t') =>
wenzelm@41444
   471
      let
wenzelm@41444
   472
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   473
      in
wenzelm@41444
   474
        if ty = ty' then Const (@{const_name Ex}, ty) $ subtrm
wenzelm@41444
   475
        else mk_bex $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   476
      end
kaliszyk@35222
   477
haftmann@37677
   478
  | (Const (@{const_name Ex1}, ty) $ (Abs (_, _,
haftmann@38795
   479
      (Const (@{const_name HOL.conj}, _) $ (Const (@{const_name Set.member}, _) $ _ $
haftmann@37677
   480
        (Const (@{const_name Respects}, _) $ resrel)) $ (t $ _)))),
haftmann@37677
   481
     Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   482
      let
wenzelm@41444
   483
        val t_ = incr_boundvars (~1) t
wenzelm@41444
   484
        val subtrm = apply_subt (regularize_trm ctxt) (t_, t')
wenzelm@41444
   485
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   486
      in
wenzelm@41444
   487
        if resrel <> needrel
wenzelm@41444
   488
        then term_mismatch "regularize (Bex1)" ctxt resrel needrel
wenzelm@41444
   489
        else mk_bex1_rel $ resrel $ subtrm
wenzelm@41444
   490
      end
kaliszyk@35222
   491
haftmann@38558
   492
  | (Const (@{const_name Ex1}, ty) $ t, Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   493
      let
wenzelm@41444
   494
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   495
      in
wenzelm@41444
   496
        if ty = ty' then Const (@{const_name Ex1}, ty) $ subtrm
wenzelm@41444
   497
        else mk_bex1_rel $ (equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   498
      end
kaliszyk@35222
   499
urbanc@38624
   500
  | (Const (@{const_name Ball}, ty) $ (Const (@{const_name Respects}, _) $ resrel) $ t,
haftmann@38558
   501
     Const (@{const_name All}, ty') $ t') =>
wenzelm@41444
   502
      let
wenzelm@41444
   503
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   504
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   505
      in
wenzelm@41444
   506
        if resrel <> needrel
wenzelm@41444
   507
        then term_mismatch "regularize (Ball)" ctxt resrel needrel
wenzelm@41444
   508
        else mk_ball $ (mk_resp $ resrel) $ subtrm
wenzelm@41444
   509
      end
kaliszyk@35222
   510
urbanc@38624
   511
  | (Const (@{const_name Bex}, ty) $ (Const (@{const_name Respects}, _) $ resrel) $ t,
haftmann@38558
   512
     Const (@{const_name Ex}, ty') $ t') =>
wenzelm@41444
   513
      let
wenzelm@41444
   514
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   515
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   516
      in
wenzelm@41444
   517
        if resrel <> needrel
wenzelm@41444
   518
        then term_mismatch "regularize (Bex)" ctxt resrel needrel
wenzelm@41444
   519
        else mk_bex $ (mk_resp $ resrel) $ subtrm
wenzelm@41444
   520
      end
kaliszyk@35222
   521
urbanc@38624
   522
  | (Const (@{const_name Bex1_rel}, ty) $ resrel $ t, Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   523
      let
wenzelm@41444
   524
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   525
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   526
      in
wenzelm@41444
   527
        if resrel <> needrel
wenzelm@41444
   528
        then term_mismatch "regularize (Bex1_res)" ctxt resrel needrel
wenzelm@41444
   529
        else mk_bex1_rel $ resrel $ subtrm
wenzelm@41444
   530
      end
kaliszyk@35222
   531
kaliszyk@35222
   532
  | (* equalities need to be replaced by appropriate equivalence relations *)
haftmann@38864
   533
    (Const (@{const_name HOL.eq}, ty), Const (@{const_name HOL.eq}, ty')) =>
wenzelm@41444
   534
        if ty = ty' then rtrm
wenzelm@41444
   535
        else equiv_relation ctxt (domain_type ty, domain_type ty')
kaliszyk@35222
   536
kaliszyk@35222
   537
  | (* in this case we just check whether the given equivalence relation is correct *)
haftmann@38864
   538
    (rel, Const (@{const_name HOL.eq}, ty')) =>
wenzelm@41444
   539
      let
wenzelm@41444
   540
        val rel_ty = fastype_of rel
wenzelm@41444
   541
        val rel' = equiv_relation_chk ctxt (domain_type rel_ty, domain_type ty')
wenzelm@41444
   542
      in
wenzelm@41444
   543
        if rel' aconv rel then rtrm
wenzelm@41444
   544
        else term_mismatch "regularize (relation mismatch)" ctxt rel rel'
wenzelm@41444
   545
      end
kaliszyk@35222
   546
kaliszyk@35222
   547
  | (_, Const _) =>
wenzelm@41444
   548
      let
wenzelm@42361
   549
        val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   550
        fun same_const (Const (s, T)) (Const (s', T')) = (s = s') andalso matches_typ thy T T'
wenzelm@41444
   551
          | same_const _ _ = false
wenzelm@41444
   552
      in
wenzelm@41444
   553
        if same_const rtrm qtrm then rtrm
wenzelm@41444
   554
        else
wenzelm@41444
   555
          let
wenzelm@41451
   556
            val rtrm' = #rconst (Quotient_Info.qconsts_lookup thy qtrm)
wenzelm@41444
   557
              handle Quotient_Info.NotFound =>
wenzelm@40236
   558
                term_mismatch "regularize (constant not found)" ctxt rtrm qtrm
wenzelm@41444
   559
          in
wenzelm@41444
   560
            if Pattern.matches thy (rtrm', rtrm)
wenzelm@41444
   561
            then rtrm else term_mismatch "regularize (constant mismatch)" ctxt rtrm qtrm
wenzelm@41444
   562
          end
wenzelm@41444
   563
      end
kaliszyk@35222
   564
haftmann@37591
   565
  | (((t1 as Const (@{const_name prod_case}, _)) $ Abs (v1, ty, Abs(v1', ty', s1))),
haftmann@37591
   566
     ((t2 as Const (@{const_name prod_case}, _)) $ Abs (v2, _ , Abs(v2', _  , s2)))) =>
kaliszyk@35222
   567
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, Abs (v1', ty', regularize_trm ctxt (s1, s2)))
kaliszyk@35222
   568
haftmann@37591
   569
  | (((t1 as Const (@{const_name prod_case}, _)) $ Abs (v1, ty, s1)),
haftmann@37591
   570
     ((t2 as Const (@{const_name prod_case}, _)) $ Abs (v2, _ , s2))) =>
kaliszyk@35222
   571
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, regularize_trm ctxt (s1, s2))
kaliszyk@35222
   572
kaliszyk@35222
   573
  | (t1 $ t2, t1' $ t2') =>
kaliszyk@35222
   574
       regularize_trm ctxt (t1, t1') $ regularize_trm ctxt (t2, t2')
kaliszyk@35222
   575
kaliszyk@35222
   576
  | (Bound i, Bound i') =>
wenzelm@41444
   577
      if i = i' then rtrm
wenzelm@41444
   578
      else raise (LIFT_MATCH "regularize (bounds mismatch)")
kaliszyk@35222
   579
kaliszyk@35222
   580
  | _ =>
wenzelm@41444
   581
      let
wenzelm@41444
   582
        val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   583
        val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   584
      in
wenzelm@41444
   585
        raise (LIFT_MATCH ("regularize failed (default: " ^ rtrm_str ^ "," ^ qtrm_str ^ ")"))
wenzelm@41444
   586
      end
kaliszyk@35222
   587
kaliszyk@35222
   588
fun regularize_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   589
  regularize_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   590
  |> Syntax.check_term ctxt
kaliszyk@35222
   591
kaliszyk@35222
   592
kaliszyk@35222
   593
kaliszyk@35222
   594
(*** Rep/Abs Injection ***)
kaliszyk@35222
   595
kaliszyk@35222
   596
(*
kaliszyk@35222
   597
Injection of Rep/Abs means:
kaliszyk@35222
   598
kaliszyk@35222
   599
  For abstractions:
kaliszyk@35222
   600
kaliszyk@35222
   601
  * If the type of the abstraction needs lifting, then we add Rep/Abs
kaliszyk@35222
   602
    around the abstraction; otherwise we leave it unchanged.
kaliszyk@35222
   603
kaliszyk@35222
   604
  For applications:
kaliszyk@35222
   605
kaliszyk@35222
   606
  * If the application involves a bounded quantifier, we recurse on
kaliszyk@35222
   607
    the second argument. If the application is a bounded abstraction,
kaliszyk@35222
   608
    we always put an Rep/Abs around it (since bounded abstractions
kaliszyk@35222
   609
    are assumed to always need lifting). Otherwise we recurse on both
kaliszyk@35222
   610
    arguments.
kaliszyk@35222
   611
kaliszyk@35222
   612
  For constants:
kaliszyk@35222
   613
kaliszyk@35222
   614
  * If the constant is (op =), we leave it always unchanged.
kaliszyk@35222
   615
    Otherwise the type of the constant needs lifting, we put
kaliszyk@35222
   616
    and Rep/Abs around it.
kaliszyk@35222
   617
kaliszyk@35222
   618
  For free variables:
kaliszyk@35222
   619
kaliszyk@35222
   620
  * We put a Rep/Abs around it if the type needs lifting.
kaliszyk@35222
   621
kaliszyk@35222
   622
  Vars case cannot occur.
kaliszyk@35222
   623
*)
kaliszyk@35222
   624
kaliszyk@35222
   625
fun mk_repabs ctxt (T, T') trm =
kaliszyk@35222
   626
  absrep_fun RepF ctxt (T, T') $ (absrep_fun AbsF ctxt (T, T') $ trm)
kaliszyk@35222
   627
kaliszyk@35222
   628
fun inj_repabs_err ctxt msg rtrm qtrm =
wenzelm@41444
   629
  let
wenzelm@41444
   630
    val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   631
    val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   632
  in
wenzelm@41444
   633
    raise LIFT_MATCH (space_implode " " [msg, quote rtrm_str, "and", quote qtrm_str])
wenzelm@41444
   634
  end
kaliszyk@35222
   635
kaliszyk@35222
   636
kaliszyk@35222
   637
(* bound variables need to be treated properly,
kaliszyk@35222
   638
   as the type of subterms needs to be calculated   *)
kaliszyk@35222
   639
fun inj_repabs_trm ctxt (rtrm, qtrm) =
kaliszyk@35222
   640
 case (rtrm, qtrm) of
urbanc@38624
   641
    (Const (@{const_name Ball}, T) $ r $ t, Const (@{const_name All}, _) $ t') =>
urbanc@38624
   642
       Const (@{const_name Ball}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   643
urbanc@38624
   644
  | (Const (@{const_name Bex}, T) $ r $ t, Const (@{const_name Ex}, _) $ t') =>
urbanc@38624
   645
       Const (@{const_name Bex}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   646
urbanc@38624
   647
  | (Const (@{const_name Babs}, T) $ r $ t, t' as (Abs _)) =>
kaliszyk@35222
   648
      let
kaliszyk@35222
   649
        val rty = fastype_of rtrm
kaliszyk@35222
   650
        val qty = fastype_of qtrm
kaliszyk@35222
   651
      in
urbanc@38624
   652
        mk_repabs ctxt (rty, qty) (Const (@{const_name Babs}, T) $ r $ (inj_repabs_trm ctxt (t, t')))
kaliszyk@35222
   653
      end
kaliszyk@35222
   654
kaliszyk@35222
   655
  | (Abs (x, T, t), Abs (x', T', t')) =>
kaliszyk@35222
   656
      let
kaliszyk@35222
   657
        val rty = fastype_of rtrm
kaliszyk@35222
   658
        val qty = fastype_of qtrm
kaliszyk@35222
   659
        val (y, s) = Term.dest_abs (x, T, t)
kaliszyk@35222
   660
        val (_, s') = Term.dest_abs (x', T', t')
kaliszyk@35222
   661
        val yvar = Free (y, T)
kaliszyk@35222
   662
        val result = Term.lambda_name (y, yvar) (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   663
      in
kaliszyk@35222
   664
        if rty = qty then result
kaliszyk@35222
   665
        else mk_repabs ctxt (rty, qty) result
kaliszyk@35222
   666
      end
kaliszyk@35222
   667
kaliszyk@35222
   668
  | (t $ s, t' $ s') =>
kaliszyk@35222
   669
       (inj_repabs_trm ctxt (t, t')) $ (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   670
kaliszyk@35222
   671
  | (Free (_, T), Free (_, T')) =>
kaliszyk@35222
   672
        if T = T' then rtrm
kaliszyk@35222
   673
        else mk_repabs ctxt (T, T') rtrm
kaliszyk@35222
   674
haftmann@38864
   675
  | (_, Const (@{const_name HOL.eq}, _)) => rtrm
kaliszyk@35222
   676
kaliszyk@35222
   677
  | (_, Const (_, T')) =>
kaliszyk@35222
   678
      let
kaliszyk@35222
   679
        val rty = fastype_of rtrm
kaliszyk@35222
   680
      in
kaliszyk@35222
   681
        if rty = T' then rtrm
kaliszyk@35222
   682
        else mk_repabs ctxt (rty, T') rtrm
kaliszyk@35222
   683
      end
kaliszyk@35222
   684
kaliszyk@35222
   685
  | _ => inj_repabs_err ctxt "injection (default):" rtrm qtrm
kaliszyk@35222
   686
kaliszyk@35222
   687
fun inj_repabs_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   688
  inj_repabs_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   689
  |> Syntax.check_term ctxt
kaliszyk@35222
   690
kaliszyk@35222
   691
kaliszyk@35222
   692
kaliszyk@35222
   693
(*** Wrapper for automatically transforming an rthm into a qthm ***)
kaliszyk@35222
   694
urbanc@37592
   695
(* substitutions functions for r/q-types and
urbanc@37592
   696
   r/q-constants, respectively
urbanc@37560
   697
*)
urbanc@37592
   698
fun subst_typ ctxt ty_subst rty =
urbanc@37560
   699
  case rty of
urbanc@37560
   700
    Type (s, rtys) =>
urbanc@37560
   701
      let
wenzelm@42361
   702
        val thy = Proof_Context.theory_of ctxt
urbanc@37592
   703
        val rty' = Type (s, map (subst_typ ctxt ty_subst) rtys)
urbanc@37560
   704
urbanc@37560
   705
        fun matches [] = rty'
urbanc@37560
   706
          | matches ((rty, qty)::tail) =
urbanc@37560
   707
              case try (Sign.typ_match thy (rty, rty')) Vartab.empty of
urbanc@37560
   708
                NONE => matches tail
urbanc@37560
   709
              | SOME inst => Envir.subst_type inst qty
urbanc@37560
   710
      in
wenzelm@41444
   711
        matches ty_subst
wenzelm@41444
   712
      end
urbanc@37560
   713
  | _ => rty
urbanc@37560
   714
urbanc@37592
   715
fun subst_trm ctxt ty_subst trm_subst rtrm =
urbanc@37560
   716
  case rtrm of
urbanc@37592
   717
    t1 $ t2 => (subst_trm ctxt ty_subst trm_subst t1) $ (subst_trm ctxt ty_subst trm_subst t2)
urbanc@37592
   718
  | Abs (x, ty, t) => Abs (x, subst_typ ctxt ty_subst ty, subst_trm ctxt ty_subst trm_subst t)
urbanc@37592
   719
  | Free(n, ty) => Free(n, subst_typ ctxt ty_subst ty)
urbanc@37592
   720
  | Var(n, ty) => Var(n, subst_typ ctxt ty_subst ty)
urbanc@37560
   721
  | Bound i => Bound i
wenzelm@41444
   722
  | Const (a, ty) =>
urbanc@37560
   723
      let
wenzelm@42361
   724
        val thy = Proof_Context.theory_of ctxt
kaliszyk@35222
   725
urbanc@37592
   726
        fun matches [] = Const (a, subst_typ ctxt ty_subst ty)
urbanc@37560
   727
          | matches ((rconst, qconst)::tail) =
urbanc@37560
   728
              case try (Pattern.match thy (rconst, rtrm)) (Vartab.empty, Vartab.empty) of
urbanc@37560
   729
                NONE => matches tail
urbanc@37560
   730
              | SOME inst => Envir.subst_term inst qconst
urbanc@37560
   731
      in
urbanc@37560
   732
        matches trm_subst
urbanc@37560
   733
      end
urbanc@37560
   734
urbanc@37592
   735
(* generate type and term substitutions out of the
wenzelm@41444
   736
   qtypes involved in a quotient; the direction flag
wenzelm@41444
   737
   indicates in which direction the substitutions work:
wenzelm@41444
   738
urbanc@37592
   739
     true:  quotient -> raw
urbanc@37592
   740
     false: raw -> quotient
urbanc@37560
   741
*)
urbanc@37592
   742
fun mk_ty_subst qtys direction ctxt =
wenzelm@41444
   743
  let
wenzelm@42361
   744
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   745
  in
wenzelm@41451
   746
    Quotient_Info.quotdata_dest ctxt
wenzelm@41444
   747
    |> map (fn x => (#rtyp x, #qtyp x))
wenzelm@41444
   748
    |> filter (fn (_, qty) => member (Sign.typ_instance thy o swap) qtys qty)
wenzelm@41444
   749
    |> map (if direction then swap else I)
wenzelm@41444
   750
  end
kaliszyk@35222
   751
urbanc@37592
   752
fun mk_trm_subst qtys direction ctxt =
wenzelm@41444
   753
  let
wenzelm@41444
   754
    val subst_typ' = subst_typ ctxt (mk_ty_subst qtys direction ctxt)
wenzelm@41444
   755
    fun proper (t1, t2) = subst_typ' (fastype_of t1) = fastype_of t2
kaliszyk@37563
   756
wenzelm@41444
   757
    val const_substs =
wenzelm@41451
   758
      Quotient_Info.qconsts_dest ctxt
wenzelm@41444
   759
      |> map (fn x => (#rconst x, #qconst x))
wenzelm@41444
   760
      |> map (if direction then swap else I)
urbanc@37560
   761
wenzelm@41444
   762
    val rel_substs =
wenzelm@41451
   763
      Quotient_Info.quotdata_dest ctxt
wenzelm@41444
   764
      |> map (fn x => (#equiv_rel x, HOLogic.eq_const (#qtyp x)))
wenzelm@41444
   765
      |> map (if direction then swap else I)
wenzelm@41444
   766
  in
wenzelm@41444
   767
    filter proper (const_substs @ rel_substs)
wenzelm@41444
   768
  end
kaliszyk@35222
   769
urbanc@37592
   770
urbanc@37560
   771
(* derives a qtyp and qtrm out of a rtyp and rtrm,
wenzelm@41444
   772
   respectively
urbanc@37560
   773
*)
urbanc@38624
   774
fun derive_qtyp ctxt qtys rty =
urbanc@37592
   775
  subst_typ ctxt (mk_ty_subst qtys false ctxt) rty
urbanc@37592
   776
urbanc@38624
   777
fun derive_qtrm ctxt qtys rtrm =
urbanc@37592
   778
  subst_trm ctxt (mk_ty_subst qtys false ctxt) (mk_trm_subst qtys false ctxt) rtrm
kaliszyk@35222
   779
urbanc@37592
   780
(* derives a rtyp and rtrm out of a qtyp and qtrm,
wenzelm@41444
   781
   respectively
urbanc@37592
   782
*)
urbanc@38624
   783
fun derive_rtyp ctxt qtys qty =
urbanc@37592
   784
  subst_typ ctxt (mk_ty_subst qtys true ctxt) qty
urbanc@37592
   785
urbanc@38624
   786
fun derive_rtrm ctxt qtys qtrm =
urbanc@37592
   787
  subst_trm ctxt (mk_ty_subst qtys true ctxt) (mk_trm_subst qtys true ctxt) qtrm
urbanc@37560
   788
kaliszyk@35222
   789
kaliszyk@35222
   790
end; (* structure *)