src/HOL/Tools/Quotient/quotient_term.ML
author wenzelm
Sat Apr 16 16:15:37 2011 +0200 (2011-04-16)
changeset 42361 23f352990944
parent 41451 892e67be8304
child 44413 80d460bc6fa8
permissions -rw-r--r--
modernized structure Proof_Context;
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 =
wenzelm@41444
    66
  let
wenzelm@42361
    67
    val thy = Proof_Context.theory_of ctxt
wenzelm@41451
    68
    val mapfun = #mapfun (Quotient_Info.maps_lookup thy s) handle Quotient_Info.NotFound =>
wenzelm@41444
    69
      raise LIFT_MATCH ("No map function for type " ^ quote s ^ " found.")
wenzelm@41444
    70
  in
wenzelm@41444
    71
    Const (mapfun, dummyT)
wenzelm@41444
    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 =
wenzelm@41444
    86
  let
wenzelm@41444
    87
    val vs' = map mk_Free vs
kaliszyk@35222
    88
wenzelm@41444
    89
    fun mk_mapfun_aux rty =
wenzelm@41444
    90
      case rty of
wenzelm@41444
    91
        TVar _ => mk_Free rty
wenzelm@41444
    92
      | Type (_, []) => mk_identity rty
wenzelm@41444
    93
      | Type (s, tys) => list_comb (get_mapfun ctxt s, map mk_mapfun_aux tys)
wenzelm@41444
    94
      | _ => raise LIFT_MATCH "mk_mapfun (default)"
wenzelm@41444
    95
  in
wenzelm@41444
    96
    fold_rev Term.lambda vs' (mk_mapfun_aux rty)
wenzelm@41444
    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 =
wenzelm@41444
   103
  let
wenzelm@42361
   104
    val thy = Proof_Context.theory_of ctxt
wenzelm@41451
   105
    val qdata = Quotient_Info.quotdata_lookup thy s handle Quotient_Info.NotFound =>
wenzelm@41444
   106
      raise LIFT_MATCH ("No quotient type " ^ quote s ^ " found.")
wenzelm@41444
   107
  in
wenzelm@41444
   108
    (#rtyp qdata, #qtyp qdata)
wenzelm@41444
   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 =
wenzelm@41444
   116
  let
wenzelm@41444
   117
    val v' = fst (dest_TVar v)
wenzelm@41444
   118
  in
wenzelm@41444
   119
    (snd (the (Vartab.lookup rtyenv v')), snd (the (Vartab.lookup qtyenv v')))
wenzelm@41444
   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 =
wenzelm@41444
   124
  let
wenzelm@42361
   125
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   126
  in
wenzelm@41444
   127
    Sign.typ_match thy (ty_pat, ty) Vartab.empty
wenzelm@41444
   128
      handle Type.TYPE_MATCH => err ctxt ty_pat ty
wenzelm@41444
   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 =
wenzelm@41444
   133
  let
wenzelm@41444
   134
    val qty_name = Long_Name.base_name qty_str
wenzelm@41444
   135
    val qualifier = Long_Name.qualifier qty_str
wenzelm@41444
   136
  in
wenzelm@41444
   137
    case flag of
wenzelm@41444
   138
      AbsF => Const (Long_Name.qualify qualifier ("abs_" ^ qty_name), dummyT)
wenzelm@41444
   139
    | RepF => Const (Long_Name.qualify qualifier ("rep_" ^ qty_name), dummyT)
wenzelm@41444
   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 =
wenzelm@41444
   147
  let
wenzelm@41444
   148
    val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
wenzelm@41444
   149
    val ty_str = Syntax.string_of_typ ctxt ty
wenzelm@41444
   150
  in
wenzelm@41444
   151
    raise LIFT_MATCH (space_implode " "
wenzelm@41444
   152
      ["absrep_fun (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
wenzelm@41444
   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
wenzelm@41444
   214
          let
wenzelm@41444
   215
            val args = map (absrep_fun flag ctxt) (tys ~~ tys')
wenzelm@41444
   216
          in
wenzelm@41444
   217
            list_comb (get_mapfun ctxt s, args)
wenzelm@41444
   218
          end
kaliszyk@35222
   219
        else
wenzelm@41444
   220
          let
wenzelm@41444
   221
            val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty ctxt s'
wenzelm@41444
   222
            val rtyenv = match ctxt absrep_match_err rty_pat rty
wenzelm@41444
   223
            val qtyenv = match ctxt absrep_match_err qty_pat qty
wenzelm@41444
   224
            val args_aux = map (double_lookup rtyenv qtyenv) vs
wenzelm@41444
   225
            val args = map (absrep_fun flag ctxt) args_aux
wenzelm@41444
   226
          in
wenzelm@41444
   227
            if forall is_identity args
wenzelm@41444
   228
            then absrep_const flag ctxt s'
wenzelm@41444
   229
            else
wenzelm@41444
   230
              let
wenzelm@41444
   231
                val map_fun = mk_mapfun ctxt vs rty_pat
wenzelm@41444
   232
                val result = list_comb (map_fun, args)
wenzelm@41444
   233
              in
wenzelm@41444
   234
                mk_fun_compose flag (absrep_const flag ctxt s', result)
wenzelm@41444
   235
              end
wenzelm@41444
   236
          end
kaliszyk@35222
   237
    | (TFree x, TFree x') =>
kaliszyk@35222
   238
        if x = x'
kaliszyk@35222
   239
        then mk_identity rty
kaliszyk@35222
   240
        else raise (LIFT_MATCH "absrep_fun (frees)")
kaliszyk@35222
   241
    | (TVar _, TVar _) => raise (LIFT_MATCH "absrep_fun (vars)")
kaliszyk@35222
   242
    | _ => raise (LIFT_MATCH "absrep_fun (default)")
kaliszyk@35222
   243
kaliszyk@35222
   244
fun absrep_fun_chk flag ctxt (rty, qty) =
kaliszyk@35222
   245
  absrep_fun flag ctxt (rty, qty)
kaliszyk@35222
   246
  |> Syntax.check_term ctxt
kaliszyk@35222
   247
kaliszyk@35222
   248
kaliszyk@35222
   249
kaliszyk@35222
   250
kaliszyk@35222
   251
(*** Aggregate Equivalence Relation ***)
kaliszyk@35222
   252
kaliszyk@35222
   253
kaliszyk@35222
   254
(* works very similar to the absrep generation,
kaliszyk@35222
   255
   except there is no need for polarities
kaliszyk@35222
   256
*)
kaliszyk@35222
   257
kaliszyk@35222
   258
(* instantiates TVars so that the term is of type ty *)
kaliszyk@35222
   259
fun force_typ ctxt trm ty =
wenzelm@41444
   260
  let
wenzelm@42361
   261
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   262
    val trm_ty = fastype_of trm
wenzelm@41444
   263
    val ty_inst = Sign.typ_match thy (trm_ty, ty) Vartab.empty
wenzelm@41444
   264
  in
wenzelm@41444
   265
    map_types (Envir.subst_type ty_inst) trm
wenzelm@41444
   266
  end
kaliszyk@35222
   267
haftmann@38864
   268
fun is_eq (Const (@{const_name HOL.eq}, _)) = true
kaliszyk@35222
   269
  | is_eq _ = false
kaliszyk@35222
   270
kaliszyk@35222
   271
fun mk_rel_compose (trm1, trm2) =
wenzelm@35402
   272
  Const (@{const_abbrev "rel_conj"}, dummyT) $ trm1 $ trm2
kaliszyk@35222
   273
kaliszyk@35222
   274
fun get_relmap ctxt s =
wenzelm@41444
   275
  let
wenzelm@42361
   276
    val thy = Proof_Context.theory_of ctxt
wenzelm@41451
   277
    val relmap = #relmap (Quotient_Info.maps_lookup thy s) handle Quotient_Info.NotFound =>
wenzelm@41444
   278
      raise LIFT_MATCH ("get_relmap (no relation map function found for type " ^ s ^ ")")
wenzelm@41444
   279
  in
wenzelm@41444
   280
    Const (relmap, dummyT)
wenzelm@41444
   281
  end
kaliszyk@35222
   282
kaliszyk@35222
   283
fun mk_relmap ctxt vs rty =
wenzelm@41444
   284
  let
wenzelm@41444
   285
    val vs' = map (mk_Free) vs
kaliszyk@35222
   286
wenzelm@41444
   287
    fun mk_relmap_aux rty =
wenzelm@41444
   288
      case rty of
wenzelm@41444
   289
        TVar _ => mk_Free rty
wenzelm@41444
   290
      | Type (_, []) => HOLogic.eq_const rty
wenzelm@41444
   291
      | Type (s, tys) => list_comb (get_relmap ctxt s, map mk_relmap_aux tys)
wenzelm@41444
   292
      | _ => raise LIFT_MATCH ("mk_relmap (default)")
wenzelm@41444
   293
  in
wenzelm@41444
   294
    fold_rev Term.lambda vs' (mk_relmap_aux rty)
wenzelm@41444
   295
  end
kaliszyk@35222
   296
kaliszyk@35222
   297
fun get_equiv_rel ctxt s =
wenzelm@41444
   298
  let
wenzelm@42361
   299
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   300
  in
wenzelm@41451
   301
    #equiv_rel (Quotient_Info.quotdata_lookup thy s) handle Quotient_Info.NotFound =>
wenzelm@41444
   302
      raise LIFT_MATCH ("get_quotdata (no quotient found for type " ^ s ^ ")")
wenzelm@41444
   303
  end
kaliszyk@35222
   304
kaliszyk@35222
   305
fun equiv_match_err ctxt ty_pat ty =
wenzelm@41444
   306
  let
wenzelm@41444
   307
    val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
wenzelm@41444
   308
    val ty_str = Syntax.string_of_typ ctxt ty
wenzelm@41444
   309
  in
wenzelm@41444
   310
    raise LIFT_MATCH (space_implode " "
wenzelm@41444
   311
      ["equiv_relation (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
wenzelm@41444
   312
  end
kaliszyk@35222
   313
kaliszyk@35222
   314
(* builds the aggregate equivalence relation
kaliszyk@35222
   315
   that will be the argument of Respects
kaliszyk@35222
   316
*)
kaliszyk@35222
   317
fun equiv_relation ctxt (rty, qty) =
kaliszyk@35222
   318
  if rty = qty
kaliszyk@35222
   319
  then HOLogic.eq_const rty
kaliszyk@35222
   320
  else
kaliszyk@35222
   321
    case (rty, qty) of
kaliszyk@35222
   322
      (Type (s, tys), Type (s', tys')) =>
wenzelm@41444
   323
        if s = s'
wenzelm@41444
   324
        then
wenzelm@41444
   325
          let
wenzelm@41444
   326
            val args = map (equiv_relation ctxt) (tys ~~ tys')
wenzelm@41444
   327
          in
wenzelm@41444
   328
            list_comb (get_relmap ctxt s, args)
wenzelm@41444
   329
          end
wenzelm@41444
   330
        else
wenzelm@41444
   331
          let
wenzelm@41444
   332
            val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty ctxt s'
wenzelm@41444
   333
            val rtyenv = match ctxt equiv_match_err rty_pat rty
wenzelm@41444
   334
            val qtyenv = match ctxt equiv_match_err qty_pat qty
wenzelm@41444
   335
            val args_aux = map (double_lookup rtyenv qtyenv) vs
wenzelm@41444
   336
            val args = map (equiv_relation ctxt) args_aux
wenzelm@41444
   337
            val eqv_rel = get_equiv_rel ctxt s'
wenzelm@41444
   338
            val eqv_rel' = force_typ ctxt eqv_rel ([rty, rty] ---> @{typ bool})
wenzelm@41444
   339
          in
wenzelm@41444
   340
            if forall is_eq args
wenzelm@41444
   341
            then eqv_rel'
wenzelm@41444
   342
            else
wenzelm@41444
   343
              let
wenzelm@41444
   344
                val rel_map = mk_relmap ctxt vs rty_pat
wenzelm@41444
   345
                val result = list_comb (rel_map, args)
wenzelm@41444
   346
              in
wenzelm@41444
   347
                mk_rel_compose (result, eqv_rel')
wenzelm@41444
   348
              end
wenzelm@41444
   349
          end
wenzelm@41444
   350
    | _ => HOLogic.eq_const rty
kaliszyk@35222
   351
kaliszyk@35222
   352
fun equiv_relation_chk ctxt (rty, qty) =
kaliszyk@35222
   353
  equiv_relation ctxt (rty, qty)
kaliszyk@35222
   354
  |> Syntax.check_term ctxt
kaliszyk@35222
   355
kaliszyk@35222
   356
kaliszyk@35222
   357
kaliszyk@35222
   358
(*** Regularization ***)
kaliszyk@35222
   359
kaliszyk@35222
   360
(* Regularizing an rtrm means:
kaliszyk@35222
   361
kaliszyk@35222
   362
 - Quantifiers over types that need lifting are replaced
kaliszyk@35222
   363
   by bounded quantifiers, for example:
kaliszyk@35222
   364
kaliszyk@35222
   365
      All P  ----> All (Respects R) P
kaliszyk@35222
   366
kaliszyk@35222
   367
   where the aggregate relation R is given by the rty and qty;
kaliszyk@35222
   368
kaliszyk@35222
   369
 - Abstractions over types that need lifting are replaced
kaliszyk@35222
   370
   by bounded abstractions, for example:
kaliszyk@35222
   371
kaliszyk@35222
   372
      %x. P  ----> Ball (Respects R) %x. P
kaliszyk@35222
   373
kaliszyk@35222
   374
 - Equalities over types that need lifting are replaced by
kaliszyk@35222
   375
   corresponding equivalence relations, for example:
kaliszyk@35222
   376
kaliszyk@35222
   377
      A = B  ----> R A B
kaliszyk@35222
   378
kaliszyk@35222
   379
   or
kaliszyk@35222
   380
kaliszyk@35222
   381
      A = B  ----> (R ===> R) A B
kaliszyk@35222
   382
kaliszyk@35222
   383
   for more complicated types of A and B
kaliszyk@35222
   384
kaliszyk@35222
   385
kaliszyk@35222
   386
 The regularize_trm accepts raw theorems in which equalities
kaliszyk@35222
   387
 and quantifiers match exactly the ones in the lifted theorem
kaliszyk@35222
   388
 but also accepts partially regularized terms.
kaliszyk@35222
   389
kaliszyk@35222
   390
 This means that the raw theorems can have:
kaliszyk@35222
   391
   Ball (Respects R),  Bex (Respects R), Bex1_rel (Respects R), Babs, R
kaliszyk@35222
   392
 in the places where:
kaliszyk@35222
   393
   All, Ex, Ex1, %, (op =)
kaliszyk@35222
   394
 is required the lifted theorem.
kaliszyk@35222
   395
kaliszyk@35222
   396
*)
kaliszyk@35222
   397
kaliszyk@35222
   398
val mk_babs = Const (@{const_name Babs}, dummyT)
kaliszyk@35222
   399
val mk_ball = Const (@{const_name Ball}, dummyT)
kaliszyk@35222
   400
val mk_bex  = Const (@{const_name Bex}, dummyT)
kaliszyk@35222
   401
val mk_bex1_rel = Const (@{const_name Bex1_rel}, dummyT)
kaliszyk@35222
   402
val mk_resp = Const (@{const_name Respects}, dummyT)
kaliszyk@35222
   403
kaliszyk@35222
   404
(* - applies f to the subterm of an abstraction,
kaliszyk@35222
   405
     otherwise to the given term,
kaliszyk@35222
   406
   - used by regularize, therefore abstracted
kaliszyk@35222
   407
     variables do not have to be treated specially
kaliszyk@35222
   408
*)
kaliszyk@35222
   409
fun apply_subt f (trm1, trm2) =
kaliszyk@35222
   410
  case (trm1, trm2) of
kaliszyk@35222
   411
    (Abs (x, T, t), Abs (_ , _, t')) => Abs (x, T, f (t, t'))
kaliszyk@35222
   412
  | _ => f (trm1, trm2)
kaliszyk@35222
   413
kaliszyk@35222
   414
fun term_mismatch str ctxt t1 t2 =
wenzelm@41444
   415
  let
wenzelm@41444
   416
    val t1_str = Syntax.string_of_term ctxt t1
wenzelm@41444
   417
    val t2_str = Syntax.string_of_term ctxt t2
wenzelm@41444
   418
    val t1_ty_str = Syntax.string_of_typ ctxt (fastype_of t1)
wenzelm@41444
   419
    val t2_ty_str = Syntax.string_of_typ ctxt (fastype_of t2)
wenzelm@41444
   420
  in
wenzelm@41444
   421
    raise LIFT_MATCH (cat_lines [str, t1_str ^ "::" ^ t1_ty_str, t2_str ^ "::" ^ t2_ty_str])
wenzelm@41444
   422
  end
kaliszyk@35222
   423
kaliszyk@35222
   424
(* the major type of All and Ex quantifiers *)
kaliszyk@35222
   425
fun qnt_typ ty = domain_type (domain_type ty)
kaliszyk@35222
   426
kaliszyk@35222
   427
(* Checks that two types match, for example:
kaliszyk@35222
   428
     rty -> rty   matches   qty -> qty *)
kaliszyk@35222
   429
fun matches_typ thy rT qT =
wenzelm@41444
   430
  if rT = qT then true
wenzelm@41444
   431
  else
wenzelm@41444
   432
    (case (rT, qT) of
wenzelm@41444
   433
      (Type (rs, rtys), Type (qs, qtys)) =>
wenzelm@41444
   434
        if rs = qs then
wenzelm@41444
   435
          if length rtys <> length qtys then false
wenzelm@41444
   436
          else forall (fn x => x = true) (map2 (matches_typ thy) rtys qtys)
wenzelm@41444
   437
        else
wenzelm@41451
   438
          (case Quotient_Info.quotdata_lookup_raw thy qs of
wenzelm@41444
   439
            SOME quotinfo => Sign.typ_instance thy (rT, #rtyp quotinfo)
wenzelm@41444
   440
          | NONE => false)
wenzelm@41444
   441
    | _ => false)
kaliszyk@35222
   442
kaliszyk@35222
   443
kaliszyk@35222
   444
(* produces a regularized version of rtrm
kaliszyk@35222
   445
kaliszyk@35222
   446
   - the result might contain dummyTs
kaliszyk@35222
   447
urbanc@38718
   448
   - for regularization we do not need any
kaliszyk@35222
   449
     special treatment of bound variables
kaliszyk@35222
   450
*)
kaliszyk@35222
   451
fun regularize_trm ctxt (rtrm, qtrm) =
kaliszyk@35222
   452
  case (rtrm, qtrm) of
kaliszyk@35222
   453
    (Abs (x, ty, t), Abs (_, ty', t')) =>
wenzelm@41444
   454
      let
wenzelm@41444
   455
        val subtrm = Abs(x, ty, regularize_trm ctxt (t, t'))
wenzelm@41444
   456
      in
wenzelm@41444
   457
        if ty = ty' then subtrm
wenzelm@41444
   458
        else mk_babs $ (mk_resp $ equiv_relation ctxt (ty, ty')) $ subtrm
wenzelm@41444
   459
      end
haftmann@37677
   460
  | (Const (@{const_name Babs}, T) $ resrel $ (t as (Abs (_, ty, _))), t' as (Abs (_, ty', _))) =>
wenzelm@41444
   461
      let
wenzelm@41444
   462
        val subtrm = regularize_trm ctxt (t, t')
wenzelm@41444
   463
        val needres = mk_resp $ equiv_relation_chk ctxt (ty, ty')
wenzelm@41444
   464
      in
wenzelm@41444
   465
        if resrel <> needres
wenzelm@41444
   466
        then term_mismatch "regularize (Babs)" ctxt resrel needres
wenzelm@41444
   467
        else mk_babs $ resrel $ subtrm
wenzelm@41444
   468
      end
kaliszyk@35222
   469
haftmann@37677
   470
  | (Const (@{const_name All}, ty) $ t, Const (@{const_name All}, 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 All}, ty) $ subtrm
wenzelm@41444
   475
        else mk_ball $ (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 Ex}, ty) $ t, Const (@{const_name Ex}, ty') $ t') =>
wenzelm@41444
   479
      let
wenzelm@41444
   480
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   481
      in
wenzelm@41444
   482
        if ty = ty' then Const (@{const_name Ex}, ty) $ subtrm
wenzelm@41444
   483
        else mk_bex $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   484
      end
kaliszyk@35222
   485
haftmann@37677
   486
  | (Const (@{const_name Ex1}, ty) $ (Abs (_, _,
haftmann@38795
   487
      (Const (@{const_name HOL.conj}, _) $ (Const (@{const_name Set.member}, _) $ _ $
haftmann@37677
   488
        (Const (@{const_name Respects}, _) $ resrel)) $ (t $ _)))),
haftmann@37677
   489
     Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   490
      let
wenzelm@41444
   491
        val t_ = incr_boundvars (~1) t
wenzelm@41444
   492
        val subtrm = apply_subt (regularize_trm ctxt) (t_, t')
wenzelm@41444
   493
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   494
      in
wenzelm@41444
   495
        if resrel <> needrel
wenzelm@41444
   496
        then term_mismatch "regularize (Bex1)" ctxt resrel needrel
wenzelm@41444
   497
        else mk_bex1_rel $ resrel $ subtrm
wenzelm@41444
   498
      end
kaliszyk@35222
   499
haftmann@38558
   500
  | (Const (@{const_name Ex1}, ty) $ t, Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   501
      let
wenzelm@41444
   502
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   503
      in
wenzelm@41444
   504
        if ty = ty' then Const (@{const_name Ex1}, ty) $ subtrm
wenzelm@41444
   505
        else mk_bex1_rel $ (equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   506
      end
kaliszyk@35222
   507
urbanc@38624
   508
  | (Const (@{const_name Ball}, ty) $ (Const (@{const_name Respects}, _) $ resrel) $ t,
haftmann@38558
   509
     Const (@{const_name All}, ty') $ t') =>
wenzelm@41444
   510
      let
wenzelm@41444
   511
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   512
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   513
      in
wenzelm@41444
   514
        if resrel <> needrel
wenzelm@41444
   515
        then term_mismatch "regularize (Ball)" ctxt resrel needrel
wenzelm@41444
   516
        else mk_ball $ (mk_resp $ resrel) $ subtrm
wenzelm@41444
   517
      end
kaliszyk@35222
   518
urbanc@38624
   519
  | (Const (@{const_name Bex}, ty) $ (Const (@{const_name Respects}, _) $ resrel) $ t,
haftmann@38558
   520
     Const (@{const_name Ex}, ty') $ t') =>
wenzelm@41444
   521
      let
wenzelm@41444
   522
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   523
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   524
      in
wenzelm@41444
   525
        if resrel <> needrel
wenzelm@41444
   526
        then term_mismatch "regularize (Bex)" ctxt resrel needrel
wenzelm@41444
   527
        else mk_bex $ (mk_resp $ resrel) $ subtrm
wenzelm@41444
   528
      end
kaliszyk@35222
   529
urbanc@38624
   530
  | (Const (@{const_name Bex1_rel}, ty) $ resrel $ t, Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   531
      let
wenzelm@41444
   532
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   533
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   534
      in
wenzelm@41444
   535
        if resrel <> needrel
wenzelm@41444
   536
        then term_mismatch "regularize (Bex1_res)" ctxt resrel needrel
wenzelm@41444
   537
        else mk_bex1_rel $ resrel $ subtrm
wenzelm@41444
   538
      end
kaliszyk@35222
   539
kaliszyk@35222
   540
  | (* equalities need to be replaced by appropriate equivalence relations *)
haftmann@38864
   541
    (Const (@{const_name HOL.eq}, ty), Const (@{const_name HOL.eq}, ty')) =>
wenzelm@41444
   542
        if ty = ty' then rtrm
wenzelm@41444
   543
        else equiv_relation ctxt (domain_type ty, domain_type ty')
kaliszyk@35222
   544
kaliszyk@35222
   545
  | (* in this case we just check whether the given equivalence relation is correct *)
haftmann@38864
   546
    (rel, Const (@{const_name HOL.eq}, ty')) =>
wenzelm@41444
   547
      let
wenzelm@41444
   548
        val rel_ty = fastype_of rel
wenzelm@41444
   549
        val rel' = equiv_relation_chk ctxt (domain_type rel_ty, domain_type ty')
wenzelm@41444
   550
      in
wenzelm@41444
   551
        if rel' aconv rel then rtrm
wenzelm@41444
   552
        else term_mismatch "regularize (relation mismatch)" ctxt rel rel'
wenzelm@41444
   553
      end
kaliszyk@35222
   554
kaliszyk@35222
   555
  | (_, Const _) =>
wenzelm@41444
   556
      let
wenzelm@42361
   557
        val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   558
        fun same_const (Const (s, T)) (Const (s', T')) = (s = s') andalso matches_typ thy T T'
wenzelm@41444
   559
          | same_const _ _ = false
wenzelm@41444
   560
      in
wenzelm@41444
   561
        if same_const rtrm qtrm then rtrm
wenzelm@41444
   562
        else
wenzelm@41444
   563
          let
wenzelm@41451
   564
            val rtrm' = #rconst (Quotient_Info.qconsts_lookup thy qtrm)
wenzelm@41444
   565
              handle Quotient_Info.NotFound =>
wenzelm@40236
   566
                term_mismatch "regularize (constant not found)" ctxt rtrm qtrm
wenzelm@41444
   567
          in
wenzelm@41444
   568
            if Pattern.matches thy (rtrm', rtrm)
wenzelm@41444
   569
            then rtrm else term_mismatch "regularize (constant mismatch)" ctxt rtrm qtrm
wenzelm@41444
   570
          end
wenzelm@41444
   571
      end
kaliszyk@35222
   572
haftmann@37591
   573
  | (((t1 as Const (@{const_name prod_case}, _)) $ Abs (v1, ty, Abs(v1', ty', s1))),
haftmann@37591
   574
     ((t2 as Const (@{const_name prod_case}, _)) $ Abs (v2, _ , Abs(v2', _  , s2)))) =>
kaliszyk@35222
   575
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, Abs (v1', ty', regularize_trm ctxt (s1, s2)))
kaliszyk@35222
   576
haftmann@37591
   577
  | (((t1 as Const (@{const_name prod_case}, _)) $ Abs (v1, ty, s1)),
haftmann@37591
   578
     ((t2 as Const (@{const_name prod_case}, _)) $ Abs (v2, _ , s2))) =>
kaliszyk@35222
   579
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, regularize_trm ctxt (s1, s2))
kaliszyk@35222
   580
kaliszyk@35222
   581
  | (t1 $ t2, t1' $ t2') =>
kaliszyk@35222
   582
       regularize_trm ctxt (t1, t1') $ regularize_trm ctxt (t2, t2')
kaliszyk@35222
   583
kaliszyk@35222
   584
  | (Bound i, Bound i') =>
wenzelm@41444
   585
      if i = i' then rtrm
wenzelm@41444
   586
      else raise (LIFT_MATCH "regularize (bounds mismatch)")
kaliszyk@35222
   587
kaliszyk@35222
   588
  | _ =>
wenzelm@41444
   589
      let
wenzelm@41444
   590
        val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   591
        val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   592
      in
wenzelm@41444
   593
        raise (LIFT_MATCH ("regularize failed (default: " ^ rtrm_str ^ "," ^ qtrm_str ^ ")"))
wenzelm@41444
   594
      end
kaliszyk@35222
   595
kaliszyk@35222
   596
fun regularize_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   597
  regularize_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   598
  |> Syntax.check_term ctxt
kaliszyk@35222
   599
kaliszyk@35222
   600
kaliszyk@35222
   601
kaliszyk@35222
   602
(*** Rep/Abs Injection ***)
kaliszyk@35222
   603
kaliszyk@35222
   604
(*
kaliszyk@35222
   605
Injection of Rep/Abs means:
kaliszyk@35222
   606
kaliszyk@35222
   607
  For abstractions:
kaliszyk@35222
   608
kaliszyk@35222
   609
  * If the type of the abstraction needs lifting, then we add Rep/Abs
kaliszyk@35222
   610
    around the abstraction; otherwise we leave it unchanged.
kaliszyk@35222
   611
kaliszyk@35222
   612
  For applications:
kaliszyk@35222
   613
kaliszyk@35222
   614
  * If the application involves a bounded quantifier, we recurse on
kaliszyk@35222
   615
    the second argument. If the application is a bounded abstraction,
kaliszyk@35222
   616
    we always put an Rep/Abs around it (since bounded abstractions
kaliszyk@35222
   617
    are assumed to always need lifting). Otherwise we recurse on both
kaliszyk@35222
   618
    arguments.
kaliszyk@35222
   619
kaliszyk@35222
   620
  For constants:
kaliszyk@35222
   621
kaliszyk@35222
   622
  * If the constant is (op =), we leave it always unchanged.
kaliszyk@35222
   623
    Otherwise the type of the constant needs lifting, we put
kaliszyk@35222
   624
    and Rep/Abs around it.
kaliszyk@35222
   625
kaliszyk@35222
   626
  For free variables:
kaliszyk@35222
   627
kaliszyk@35222
   628
  * We put a Rep/Abs around it if the type needs lifting.
kaliszyk@35222
   629
kaliszyk@35222
   630
  Vars case cannot occur.
kaliszyk@35222
   631
*)
kaliszyk@35222
   632
kaliszyk@35222
   633
fun mk_repabs ctxt (T, T') trm =
kaliszyk@35222
   634
  absrep_fun RepF ctxt (T, T') $ (absrep_fun AbsF ctxt (T, T') $ trm)
kaliszyk@35222
   635
kaliszyk@35222
   636
fun inj_repabs_err ctxt msg rtrm qtrm =
wenzelm@41444
   637
  let
wenzelm@41444
   638
    val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   639
    val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   640
  in
wenzelm@41444
   641
    raise LIFT_MATCH (space_implode " " [msg, quote rtrm_str, "and", quote qtrm_str])
wenzelm@41444
   642
  end
kaliszyk@35222
   643
kaliszyk@35222
   644
kaliszyk@35222
   645
(* bound variables need to be treated properly,
kaliszyk@35222
   646
   as the type of subterms needs to be calculated   *)
kaliszyk@35222
   647
fun inj_repabs_trm ctxt (rtrm, qtrm) =
kaliszyk@35222
   648
 case (rtrm, qtrm) of
urbanc@38624
   649
    (Const (@{const_name Ball}, T) $ r $ t, Const (@{const_name All}, _) $ t') =>
urbanc@38624
   650
       Const (@{const_name Ball}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   651
urbanc@38624
   652
  | (Const (@{const_name Bex}, T) $ r $ t, Const (@{const_name Ex}, _) $ t') =>
urbanc@38624
   653
       Const (@{const_name Bex}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   654
urbanc@38624
   655
  | (Const (@{const_name Babs}, T) $ r $ t, t' as (Abs _)) =>
kaliszyk@35222
   656
      let
kaliszyk@35222
   657
        val rty = fastype_of rtrm
kaliszyk@35222
   658
        val qty = fastype_of qtrm
kaliszyk@35222
   659
      in
urbanc@38624
   660
        mk_repabs ctxt (rty, qty) (Const (@{const_name Babs}, T) $ r $ (inj_repabs_trm ctxt (t, t')))
kaliszyk@35222
   661
      end
kaliszyk@35222
   662
kaliszyk@35222
   663
  | (Abs (x, T, t), Abs (x', T', t')) =>
kaliszyk@35222
   664
      let
kaliszyk@35222
   665
        val rty = fastype_of rtrm
kaliszyk@35222
   666
        val qty = fastype_of qtrm
kaliszyk@35222
   667
        val (y, s) = Term.dest_abs (x, T, t)
kaliszyk@35222
   668
        val (_, s') = Term.dest_abs (x', T', t')
kaliszyk@35222
   669
        val yvar = Free (y, T)
kaliszyk@35222
   670
        val result = Term.lambda_name (y, yvar) (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   671
      in
kaliszyk@35222
   672
        if rty = qty then result
kaliszyk@35222
   673
        else mk_repabs ctxt (rty, qty) result
kaliszyk@35222
   674
      end
kaliszyk@35222
   675
kaliszyk@35222
   676
  | (t $ s, t' $ s') =>
kaliszyk@35222
   677
       (inj_repabs_trm ctxt (t, t')) $ (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   678
kaliszyk@35222
   679
  | (Free (_, T), Free (_, T')) =>
kaliszyk@35222
   680
        if T = T' then rtrm
kaliszyk@35222
   681
        else mk_repabs ctxt (T, T') rtrm
kaliszyk@35222
   682
haftmann@38864
   683
  | (_, Const (@{const_name HOL.eq}, _)) => rtrm
kaliszyk@35222
   684
kaliszyk@35222
   685
  | (_, Const (_, T')) =>
kaliszyk@35222
   686
      let
kaliszyk@35222
   687
        val rty = fastype_of rtrm
kaliszyk@35222
   688
      in
kaliszyk@35222
   689
        if rty = T' then rtrm
kaliszyk@35222
   690
        else mk_repabs ctxt (rty, T') rtrm
kaliszyk@35222
   691
      end
kaliszyk@35222
   692
kaliszyk@35222
   693
  | _ => inj_repabs_err ctxt "injection (default):" rtrm qtrm
kaliszyk@35222
   694
kaliszyk@35222
   695
fun inj_repabs_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   696
  inj_repabs_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   697
  |> Syntax.check_term ctxt
kaliszyk@35222
   698
kaliszyk@35222
   699
kaliszyk@35222
   700
kaliszyk@35222
   701
(*** Wrapper for automatically transforming an rthm into a qthm ***)
kaliszyk@35222
   702
urbanc@37592
   703
(* substitutions functions for r/q-types and
urbanc@37592
   704
   r/q-constants, respectively
urbanc@37560
   705
*)
urbanc@37592
   706
fun subst_typ ctxt ty_subst rty =
urbanc@37560
   707
  case rty of
urbanc@37560
   708
    Type (s, rtys) =>
urbanc@37560
   709
      let
wenzelm@42361
   710
        val thy = Proof_Context.theory_of ctxt
urbanc@37592
   711
        val rty' = Type (s, map (subst_typ ctxt ty_subst) rtys)
urbanc@37560
   712
urbanc@37560
   713
        fun matches [] = rty'
urbanc@37560
   714
          | matches ((rty, qty)::tail) =
urbanc@37560
   715
              case try (Sign.typ_match thy (rty, rty')) Vartab.empty of
urbanc@37560
   716
                NONE => matches tail
urbanc@37560
   717
              | SOME inst => Envir.subst_type inst qty
urbanc@37560
   718
      in
wenzelm@41444
   719
        matches ty_subst
wenzelm@41444
   720
      end
urbanc@37560
   721
  | _ => rty
urbanc@37560
   722
urbanc@37592
   723
fun subst_trm ctxt ty_subst trm_subst rtrm =
urbanc@37560
   724
  case rtrm of
urbanc@37592
   725
    t1 $ t2 => (subst_trm ctxt ty_subst trm_subst t1) $ (subst_trm ctxt ty_subst trm_subst t2)
urbanc@37592
   726
  | Abs (x, ty, t) => Abs (x, subst_typ ctxt ty_subst ty, subst_trm ctxt ty_subst trm_subst t)
urbanc@37592
   727
  | Free(n, ty) => Free(n, subst_typ ctxt ty_subst ty)
urbanc@37592
   728
  | Var(n, ty) => Var(n, subst_typ ctxt ty_subst ty)
urbanc@37560
   729
  | Bound i => Bound i
wenzelm@41444
   730
  | Const (a, ty) =>
urbanc@37560
   731
      let
wenzelm@42361
   732
        val thy = Proof_Context.theory_of ctxt
kaliszyk@35222
   733
urbanc@37592
   734
        fun matches [] = Const (a, subst_typ ctxt ty_subst ty)
urbanc@37560
   735
          | matches ((rconst, qconst)::tail) =
urbanc@37560
   736
              case try (Pattern.match thy (rconst, rtrm)) (Vartab.empty, Vartab.empty) of
urbanc@37560
   737
                NONE => matches tail
urbanc@37560
   738
              | SOME inst => Envir.subst_term inst qconst
urbanc@37560
   739
      in
urbanc@37560
   740
        matches trm_subst
urbanc@37560
   741
      end
urbanc@37560
   742
urbanc@37592
   743
(* generate type and term substitutions out of the
wenzelm@41444
   744
   qtypes involved in a quotient; the direction flag
wenzelm@41444
   745
   indicates in which direction the substitutions work:
wenzelm@41444
   746
urbanc@37592
   747
     true:  quotient -> raw
urbanc@37592
   748
     false: raw -> quotient
urbanc@37560
   749
*)
urbanc@37592
   750
fun mk_ty_subst qtys direction ctxt =
wenzelm@41444
   751
  let
wenzelm@42361
   752
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   753
  in
wenzelm@41451
   754
    Quotient_Info.quotdata_dest ctxt
wenzelm@41444
   755
    |> map (fn x => (#rtyp x, #qtyp x))
wenzelm@41444
   756
    |> filter (fn (_, qty) => member (Sign.typ_instance thy o swap) qtys qty)
wenzelm@41444
   757
    |> map (if direction then swap else I)
wenzelm@41444
   758
  end
kaliszyk@35222
   759
urbanc@37592
   760
fun mk_trm_subst qtys direction ctxt =
wenzelm@41444
   761
  let
wenzelm@41444
   762
    val subst_typ' = subst_typ ctxt (mk_ty_subst qtys direction ctxt)
wenzelm@41444
   763
    fun proper (t1, t2) = subst_typ' (fastype_of t1) = fastype_of t2
kaliszyk@37563
   764
wenzelm@41444
   765
    val const_substs =
wenzelm@41451
   766
      Quotient_Info.qconsts_dest ctxt
wenzelm@41444
   767
      |> map (fn x => (#rconst x, #qconst x))
wenzelm@41444
   768
      |> map (if direction then swap else I)
urbanc@37560
   769
wenzelm@41444
   770
    val rel_substs =
wenzelm@41451
   771
      Quotient_Info.quotdata_dest ctxt
wenzelm@41444
   772
      |> map (fn x => (#equiv_rel x, HOLogic.eq_const (#qtyp x)))
wenzelm@41444
   773
      |> map (if direction then swap else I)
wenzelm@41444
   774
  in
wenzelm@41444
   775
    filter proper (const_substs @ rel_substs)
wenzelm@41444
   776
  end
kaliszyk@35222
   777
urbanc@37592
   778
urbanc@37560
   779
(* derives a qtyp and qtrm out of a rtyp and rtrm,
wenzelm@41444
   780
   respectively
urbanc@37560
   781
*)
urbanc@38624
   782
fun derive_qtyp ctxt qtys rty =
urbanc@37592
   783
  subst_typ ctxt (mk_ty_subst qtys false ctxt) rty
urbanc@37592
   784
urbanc@38624
   785
fun derive_qtrm ctxt qtys rtrm =
urbanc@37592
   786
  subst_trm ctxt (mk_ty_subst qtys false ctxt) (mk_trm_subst qtys false ctxt) rtrm
kaliszyk@35222
   787
urbanc@37592
   788
(* derives a rtyp and rtrm out of a qtyp and qtrm,
wenzelm@41444
   789
   respectively
urbanc@37592
   790
*)
urbanc@38624
   791
fun derive_rtyp ctxt qtys qty =
urbanc@37592
   792
  subst_typ ctxt (mk_ty_subst qtys true ctxt) qty
urbanc@37592
   793
urbanc@38624
   794
fun derive_rtrm ctxt qtys qtrm =
urbanc@37592
   795
  subst_trm ctxt (mk_ty_subst qtys true ctxt) (mk_trm_subst qtys true ctxt) qtrm
urbanc@37560
   796
kaliszyk@35222
   797
kaliszyk@35222
   798
end; (* structure *)