src/HOL/Tools/Quotient/quotient_term.ML
author wenzelm
Fri Nov 04 17:19:33 2011 +0100 (2011-11-04)
changeset 45340 98ec8b51af9c
parent 45280 9fd6fce8a230
child 45534 4ab21521b393
permissions -rw-r--r--
prefer global Quotient_Info lookup to accomodate Quotient_Term, which is not quite localized yet (cf. 9fd6fce8a230);
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
wenzelm@45340
    65
fun get_mapfun thy s =
wenzelm@45340
    66
  (case Quotient_Info.lookup_quotmaps_global thy s of
bulwahn@45273
    67
    SOME map_data => Const (#mapfun map_data, dummyT)
wenzelm@45279
    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
*)
wenzelm@45340
    81
fun mk_mapfun thy 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@45340
    89
      | Type (s, tys) => list_comb (get_mapfun thy 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
*)
wenzelm@45340
    98
fun get_rty_qty thy s =
wenzelm@45340
    99
  (case Quotient_Info.lookup_quotients_global thy s of
bulwahn@45272
   100
    SOME qdata => (#rtyp qdata, #qtyp qdata)
wenzelm@45280
   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) =
wenzelm@45340
   192
  let
wenzelm@45340
   193
    val thy = Proof_Context.theory_of ctxt
wenzelm@45340
   194
  in
wenzelm@45340
   195
    if rty = qty
wenzelm@45340
   196
    then mk_identity rty
wenzelm@45340
   197
    else
wenzelm@45340
   198
      case (rty, qty) of
wenzelm@45340
   199
        (Type ("fun", [ty1, ty2]), Type ("fun", [ty1', ty2'])) =>
wenzelm@41444
   200
          let
wenzelm@45340
   201
            val arg1 = absrep_fun (negF flag) ctxt (ty1, ty1')
wenzelm@45340
   202
            val arg2 = absrep_fun flag ctxt (ty2, ty2')
wenzelm@45340
   203
          in
wenzelm@45340
   204
            list_comb (get_mapfun thy "fun", [arg1, arg2])
wenzelm@45340
   205
          end
wenzelm@45340
   206
  (* FIXME: use type_name antiquotation if set type becomes explicit *)
wenzelm@45340
   207
      | (Type ("Set.set", [ty]), Type ("Set.set", [ty'])) =>
wenzelm@45340
   208
          let
wenzelm@45340
   209
            val arg = absrep_fun (negF flag) ctxt (ty, ty')
wenzelm@41444
   210
          in
wenzelm@45340
   211
            get_mapfun thy "Set.set" $ arg
wenzelm@41444
   212
          end
wenzelm@45340
   213
      | (Type (s, tys), Type (s', tys')) =>
wenzelm@45340
   214
          if s = s'
wenzelm@45340
   215
          then
wenzelm@45340
   216
            let
wenzelm@45340
   217
              val args = map (absrep_fun flag ctxt) (tys ~~ tys')
wenzelm@45340
   218
            in
wenzelm@45340
   219
              list_comb (get_mapfun thy s, args)
wenzelm@45340
   220
            end
wenzelm@45340
   221
          else
wenzelm@45340
   222
            let
wenzelm@45340
   223
              val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty thy s'
wenzelm@45340
   224
              val rtyenv = match ctxt absrep_match_err rty_pat rty
wenzelm@45340
   225
              val qtyenv = match ctxt absrep_match_err qty_pat qty
wenzelm@45340
   226
              val args_aux = map (double_lookup rtyenv qtyenv) vs
wenzelm@45340
   227
              val args = map (absrep_fun flag ctxt) args_aux
wenzelm@45340
   228
            in
wenzelm@45340
   229
              if forall is_identity args
wenzelm@45340
   230
              then absrep_const flag ctxt s'
wenzelm@45340
   231
              else
wenzelm@45340
   232
                let
wenzelm@45340
   233
                  val map_fun = mk_mapfun thy vs rty_pat
wenzelm@45340
   234
                  val result = list_comb (map_fun, args)
wenzelm@45340
   235
                in
wenzelm@45340
   236
                  mk_fun_compose flag (absrep_const flag ctxt s', result)
wenzelm@45340
   237
                end
wenzelm@45340
   238
            end
wenzelm@45340
   239
      | (TFree x, TFree x') =>
wenzelm@45340
   240
          if x = x'
wenzelm@45340
   241
          then mk_identity rty
wenzelm@45340
   242
          else raise (LIFT_MATCH "absrep_fun (frees)")
wenzelm@45340
   243
      | (TVar _, TVar _) => raise (LIFT_MATCH "absrep_fun (vars)")
wenzelm@45340
   244
      | _ => raise (LIFT_MATCH "absrep_fun (default)")
wenzelm@45340
   245
  end
kaliszyk@35222
   246
kaliszyk@35222
   247
fun absrep_fun_chk flag ctxt (rty, qty) =
kaliszyk@35222
   248
  absrep_fun flag ctxt (rty, qty)
kaliszyk@35222
   249
  |> Syntax.check_term ctxt
kaliszyk@35222
   250
kaliszyk@35222
   251
kaliszyk@35222
   252
kaliszyk@35222
   253
kaliszyk@35222
   254
(*** Aggregate Equivalence Relation ***)
kaliszyk@35222
   255
kaliszyk@35222
   256
kaliszyk@35222
   257
(* works very similar to the absrep generation,
kaliszyk@35222
   258
   except there is no need for polarities
kaliszyk@35222
   259
*)
kaliszyk@35222
   260
kaliszyk@35222
   261
(* instantiates TVars so that the term is of type ty *)
kaliszyk@35222
   262
fun force_typ ctxt trm ty =
wenzelm@41444
   263
  let
wenzelm@42361
   264
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   265
    val trm_ty = fastype_of trm
wenzelm@41444
   266
    val ty_inst = Sign.typ_match thy (trm_ty, ty) Vartab.empty
wenzelm@41444
   267
  in
wenzelm@41444
   268
    map_types (Envir.subst_type ty_inst) trm
wenzelm@41444
   269
  end
kaliszyk@35222
   270
haftmann@38864
   271
fun is_eq (Const (@{const_name HOL.eq}, _)) = true
kaliszyk@35222
   272
  | is_eq _ = false
kaliszyk@35222
   273
kaliszyk@35222
   274
fun mk_rel_compose (trm1, trm2) =
wenzelm@35402
   275
  Const (@{const_abbrev "rel_conj"}, dummyT) $ trm1 $ trm2
kaliszyk@35222
   276
wenzelm@45340
   277
fun get_relmap thy s =
wenzelm@45340
   278
  (case Quotient_Info.lookup_quotmaps thy s of
bulwahn@45273
   279
    SOME map_data => Const (#relmap map_data, dummyT)
wenzelm@45279
   280
  | NONE => raise LIFT_MATCH ("get_relmap (no relation map function found for type " ^ s ^ ")"))
kaliszyk@35222
   281
kaliszyk@35222
   282
fun mk_relmap ctxt vs rty =
wenzelm@41444
   283
  let
wenzelm@41444
   284
    val vs' = map (mk_Free) vs
kaliszyk@35222
   285
wenzelm@41444
   286
    fun mk_relmap_aux rty =
wenzelm@41444
   287
      case rty of
wenzelm@41444
   288
        TVar _ => mk_Free rty
wenzelm@41444
   289
      | Type (_, []) => HOLogic.eq_const rty
wenzelm@41444
   290
      | Type (s, tys) => list_comb (get_relmap ctxt s, map mk_relmap_aux tys)
wenzelm@41444
   291
      | _ => raise LIFT_MATCH ("mk_relmap (default)")
wenzelm@41444
   292
  in
wenzelm@41444
   293
    fold_rev Term.lambda vs' (mk_relmap_aux rty)
wenzelm@41444
   294
  end
kaliszyk@35222
   295
wenzelm@45340
   296
fun get_equiv_rel thy s =
wenzelm@45340
   297
  (case Quotient_Info.lookup_quotients thy s of
wenzelm@45279
   298
    SOME qdata => #equiv_rel qdata
wenzelm@45279
   299
  | NONE => raise LIFT_MATCH ("get_quotdata (no quotient found for type " ^ s ^ ")"))
kaliszyk@35222
   300
kaliszyk@35222
   301
fun equiv_match_err ctxt ty_pat ty =
wenzelm@41444
   302
  let
wenzelm@41444
   303
    val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
wenzelm@41444
   304
    val ty_str = Syntax.string_of_typ ctxt ty
wenzelm@41444
   305
  in
wenzelm@41444
   306
    raise LIFT_MATCH (space_implode " "
wenzelm@41444
   307
      ["equiv_relation (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
wenzelm@41444
   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) =
wenzelm@45340
   314
  let
wenzelm@45340
   315
    val thy = Proof_Context.theory_of ctxt
wenzelm@45340
   316
  in
wenzelm@45340
   317
    if rty = qty
wenzelm@45340
   318
    then HOLogic.eq_const rty
wenzelm@45340
   319
    else
wenzelm@45340
   320
      case (rty, qty) of
wenzelm@45340
   321
        (Type (s, tys), Type (s', tys')) =>
wenzelm@45340
   322
          if s = s'
wenzelm@45340
   323
          then
wenzelm@45340
   324
            let
wenzelm@45340
   325
              val args = map (equiv_relation ctxt) (tys ~~ tys')
wenzelm@45340
   326
            in
wenzelm@45340
   327
              list_comb (get_relmap ctxt s, args)
wenzelm@45340
   328
            end
wenzelm@45340
   329
          else
wenzelm@45340
   330
            let
wenzelm@45340
   331
              val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty thy s'
wenzelm@45340
   332
              val rtyenv = match ctxt equiv_match_err rty_pat rty
wenzelm@45340
   333
              val qtyenv = match ctxt equiv_match_err qty_pat qty
wenzelm@45340
   334
              val args_aux = map (double_lookup rtyenv qtyenv) vs
wenzelm@45340
   335
              val args = map (equiv_relation ctxt) args_aux
wenzelm@45340
   336
              val eqv_rel = get_equiv_rel ctxt s'
wenzelm@45340
   337
              val eqv_rel' = force_typ ctxt eqv_rel ([rty, rty] ---> @{typ bool})
wenzelm@45340
   338
            in
wenzelm@45340
   339
              if forall is_eq args
wenzelm@45340
   340
              then eqv_rel'
wenzelm@45340
   341
              else
wenzelm@45340
   342
                let
wenzelm@45340
   343
                  val rel_map = mk_relmap ctxt vs rty_pat
wenzelm@45340
   344
                  val result = list_comb (rel_map, args)
wenzelm@45340
   345
                in
wenzelm@45340
   346
                  mk_rel_compose (result, eqv_rel')
wenzelm@45340
   347
                end
wenzelm@45340
   348
            end
wenzelm@45340
   349
      | _ => HOLogic.eq_const rty
wenzelm@45340
   350
  end
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 *)
wenzelm@45280
   429
fun matches_typ ctxt rT qT =
wenzelm@45340
   430
  let
wenzelm@45340
   431
    val thy = Proof_Context.theory_of ctxt
wenzelm@45340
   432
  in
wenzelm@45340
   433
    if rT = qT then true
wenzelm@45340
   434
    else
wenzelm@45340
   435
      (case (rT, qT) of
wenzelm@45340
   436
        (Type (rs, rtys), Type (qs, qtys)) =>
wenzelm@45340
   437
          if rs = qs then
wenzelm@45340
   438
            if length rtys <> length qtys then false
wenzelm@45340
   439
            else forall (fn x => x = true) (map2 (matches_typ ctxt) rtys qtys)
wenzelm@45340
   440
          else
wenzelm@45340
   441
            (case Quotient_Info.lookup_quotients_global thy qs of
wenzelm@45340
   442
              SOME quotinfo => Sign.typ_instance thy (rT, #rtyp quotinfo)
wenzelm@45340
   443
            | NONE => false)
wenzelm@45340
   444
      | _ => false)
wenzelm@45340
   445
  end
kaliszyk@35222
   446
kaliszyk@35222
   447
kaliszyk@35222
   448
(* produces a regularized version of rtrm
kaliszyk@35222
   449
kaliszyk@35222
   450
   - the result might contain dummyTs
kaliszyk@35222
   451
urbanc@38718
   452
   - for regularization we do not need any
kaliszyk@35222
   453
     special treatment of bound variables
kaliszyk@35222
   454
*)
kaliszyk@35222
   455
fun regularize_trm ctxt (rtrm, qtrm) =
wenzelm@45280
   456
  (case (rtrm, qtrm) of
kaliszyk@35222
   457
    (Abs (x, ty, t), Abs (_, ty', t')) =>
wenzelm@41444
   458
      let
wenzelm@41444
   459
        val subtrm = Abs(x, ty, regularize_trm ctxt (t, t'))
wenzelm@41444
   460
      in
wenzelm@41444
   461
        if ty = ty' then subtrm
wenzelm@41444
   462
        else mk_babs $ (mk_resp $ equiv_relation ctxt (ty, ty')) $ subtrm
wenzelm@41444
   463
      end
wenzelm@45280
   464
haftmann@37677
   465
  | (Const (@{const_name Babs}, T) $ resrel $ (t as (Abs (_, ty, _))), t' as (Abs (_, ty', _))) =>
wenzelm@41444
   466
      let
wenzelm@41444
   467
        val subtrm = regularize_trm ctxt (t, t')
wenzelm@41444
   468
        val needres = mk_resp $ equiv_relation_chk ctxt (ty, ty')
wenzelm@41444
   469
      in
wenzelm@41444
   470
        if resrel <> needres
wenzelm@41444
   471
        then term_mismatch "regularize (Babs)" ctxt resrel needres
wenzelm@41444
   472
        else mk_babs $ resrel $ subtrm
wenzelm@41444
   473
      end
kaliszyk@35222
   474
haftmann@37677
   475
  | (Const (@{const_name All}, ty) $ t, Const (@{const_name All}, ty') $ t') =>
wenzelm@41444
   476
      let
wenzelm@41444
   477
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   478
      in
wenzelm@41444
   479
        if ty = ty' then Const (@{const_name All}, ty) $ subtrm
wenzelm@41444
   480
        else mk_ball $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   481
      end
kaliszyk@35222
   482
haftmann@37677
   483
  | (Const (@{const_name Ex}, ty) $ t, Const (@{const_name Ex}, ty') $ t') =>
wenzelm@41444
   484
      let
wenzelm@41444
   485
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   486
      in
wenzelm@41444
   487
        if ty = ty' then Const (@{const_name Ex}, ty) $ subtrm
wenzelm@41444
   488
        else mk_bex $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   489
      end
kaliszyk@35222
   490
haftmann@37677
   491
  | (Const (@{const_name Ex1}, ty) $ (Abs (_, _,
haftmann@38795
   492
      (Const (@{const_name HOL.conj}, _) $ (Const (@{const_name Set.member}, _) $ _ $
haftmann@37677
   493
        (Const (@{const_name Respects}, _) $ resrel)) $ (t $ _)))),
haftmann@37677
   494
     Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   495
      let
wenzelm@41444
   496
        val t_ = incr_boundvars (~1) t
wenzelm@41444
   497
        val subtrm = apply_subt (regularize_trm ctxt) (t_, t')
wenzelm@41444
   498
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   499
      in
wenzelm@41444
   500
        if resrel <> needrel
wenzelm@41444
   501
        then term_mismatch "regularize (Bex1)" ctxt resrel needrel
wenzelm@41444
   502
        else mk_bex1_rel $ resrel $ subtrm
wenzelm@41444
   503
      end
kaliszyk@35222
   504
haftmann@38558
   505
  | (Const (@{const_name Ex1}, ty) $ t, Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   506
      let
wenzelm@41444
   507
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   508
      in
wenzelm@41444
   509
        if ty = ty' then Const (@{const_name Ex1}, ty) $ subtrm
wenzelm@41444
   510
        else mk_bex1_rel $ (equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   511
      end
kaliszyk@35222
   512
urbanc@38624
   513
  | (Const (@{const_name Ball}, ty) $ (Const (@{const_name Respects}, _) $ resrel) $ t,
haftmann@38558
   514
     Const (@{const_name All}, ty') $ t') =>
wenzelm@41444
   515
      let
wenzelm@41444
   516
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   517
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   518
      in
wenzelm@41444
   519
        if resrel <> needrel
wenzelm@41444
   520
        then term_mismatch "regularize (Ball)" ctxt resrel needrel
wenzelm@41444
   521
        else mk_ball $ (mk_resp $ resrel) $ subtrm
wenzelm@41444
   522
      end
kaliszyk@35222
   523
urbanc@38624
   524
  | (Const (@{const_name Bex}, ty) $ (Const (@{const_name Respects}, _) $ resrel) $ t,
haftmann@38558
   525
     Const (@{const_name Ex}, ty') $ t') =>
wenzelm@41444
   526
      let
wenzelm@41444
   527
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   528
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   529
      in
wenzelm@41444
   530
        if resrel <> needrel
wenzelm@41444
   531
        then term_mismatch "regularize (Bex)" ctxt resrel needrel
wenzelm@41444
   532
        else mk_bex $ (mk_resp $ resrel) $ subtrm
wenzelm@41444
   533
      end
kaliszyk@35222
   534
urbanc@38624
   535
  | (Const (@{const_name Bex1_rel}, ty) $ resrel $ t, Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   536
      let
wenzelm@41444
   537
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   538
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   539
      in
wenzelm@41444
   540
        if resrel <> needrel
wenzelm@41444
   541
        then term_mismatch "regularize (Bex1_res)" ctxt resrel needrel
wenzelm@41444
   542
        else mk_bex1_rel $ resrel $ subtrm
wenzelm@41444
   543
      end
kaliszyk@35222
   544
kaliszyk@35222
   545
  | (* equalities need to be replaced by appropriate equivalence relations *)
haftmann@38864
   546
    (Const (@{const_name HOL.eq}, ty), Const (@{const_name HOL.eq}, ty')) =>
wenzelm@41444
   547
        if ty = ty' then rtrm
wenzelm@41444
   548
        else equiv_relation ctxt (domain_type ty, domain_type ty')
kaliszyk@35222
   549
kaliszyk@35222
   550
  | (* in this case we just check whether the given equivalence relation is correct *)
haftmann@38864
   551
    (rel, Const (@{const_name HOL.eq}, ty')) =>
wenzelm@41444
   552
      let
wenzelm@41444
   553
        val rel_ty = fastype_of rel
wenzelm@41444
   554
        val rel' = equiv_relation_chk ctxt (domain_type rel_ty, domain_type ty')
wenzelm@41444
   555
      in
wenzelm@41444
   556
        if rel' aconv rel then rtrm
wenzelm@41444
   557
        else term_mismatch "regularize (relation mismatch)" ctxt rel rel'
wenzelm@41444
   558
      end
kaliszyk@35222
   559
kaliszyk@35222
   560
  | (_, Const _) =>
wenzelm@41444
   561
      let
wenzelm@42361
   562
        val thy = Proof_Context.theory_of ctxt
wenzelm@45280
   563
        fun same_const (Const (s, T)) (Const (s', T')) = s = s' andalso matches_typ ctxt T T'
wenzelm@41444
   564
          | same_const _ _ = false
wenzelm@41444
   565
      in
wenzelm@41444
   566
        if same_const rtrm qtrm then rtrm
wenzelm@41444
   567
        else
wenzelm@41444
   568
          let
wenzelm@45279
   569
            val rtrm' =
wenzelm@45340
   570
              (case Quotient_Info.lookup_quotconsts_global thy qtrm of
wenzelm@45279
   571
                SOME qconst_info => #rconst qconst_info
wenzelm@45279
   572
              | NONE => term_mismatch "regularize (constant not found)" ctxt rtrm qtrm)
wenzelm@41444
   573
          in
wenzelm@41444
   574
            if Pattern.matches thy (rtrm', rtrm)
wenzelm@41444
   575
            then rtrm else term_mismatch "regularize (constant mismatch)" ctxt rtrm qtrm
wenzelm@41444
   576
          end
wenzelm@41444
   577
      end
kaliszyk@35222
   578
haftmann@37591
   579
  | (((t1 as Const (@{const_name prod_case}, _)) $ Abs (v1, ty, Abs(v1', ty', s1))),
haftmann@37591
   580
     ((t2 as Const (@{const_name prod_case}, _)) $ Abs (v2, _ , Abs(v2', _  , s2)))) =>
kaliszyk@35222
   581
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, Abs (v1', ty', regularize_trm ctxt (s1, s2)))
kaliszyk@35222
   582
haftmann@37591
   583
  | (((t1 as Const (@{const_name prod_case}, _)) $ Abs (v1, ty, s1)),
haftmann@37591
   584
     ((t2 as Const (@{const_name prod_case}, _)) $ Abs (v2, _ , s2))) =>
kaliszyk@35222
   585
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, regularize_trm ctxt (s1, s2))
kaliszyk@35222
   586
kaliszyk@35222
   587
  | (t1 $ t2, t1' $ t2') =>
kaliszyk@35222
   588
       regularize_trm ctxt (t1, t1') $ regularize_trm ctxt (t2, t2')
kaliszyk@35222
   589
kaliszyk@35222
   590
  | (Bound i, Bound i') =>
wenzelm@41444
   591
      if i = i' then rtrm
wenzelm@41444
   592
      else raise (LIFT_MATCH "regularize (bounds mismatch)")
kaliszyk@35222
   593
kaliszyk@35222
   594
  | _ =>
wenzelm@41444
   595
      let
wenzelm@41444
   596
        val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   597
        val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   598
      in
wenzelm@41444
   599
        raise (LIFT_MATCH ("regularize failed (default: " ^ rtrm_str ^ "," ^ qtrm_str ^ ")"))
wenzelm@45280
   600
      end)
kaliszyk@35222
   601
kaliszyk@35222
   602
fun regularize_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   603
  regularize_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   604
  |> Syntax.check_term ctxt
kaliszyk@35222
   605
kaliszyk@35222
   606
kaliszyk@35222
   607
kaliszyk@35222
   608
(*** Rep/Abs Injection ***)
kaliszyk@35222
   609
kaliszyk@35222
   610
(*
kaliszyk@35222
   611
Injection of Rep/Abs means:
kaliszyk@35222
   612
kaliszyk@35222
   613
  For abstractions:
kaliszyk@35222
   614
kaliszyk@35222
   615
  * If the type of the abstraction needs lifting, then we add Rep/Abs
kaliszyk@35222
   616
    around the abstraction; otherwise we leave it unchanged.
kaliszyk@35222
   617
kaliszyk@35222
   618
  For applications:
kaliszyk@35222
   619
kaliszyk@35222
   620
  * If the application involves a bounded quantifier, we recurse on
kaliszyk@35222
   621
    the second argument. If the application is a bounded abstraction,
kaliszyk@35222
   622
    we always put an Rep/Abs around it (since bounded abstractions
kaliszyk@35222
   623
    are assumed to always need lifting). Otherwise we recurse on both
kaliszyk@35222
   624
    arguments.
kaliszyk@35222
   625
kaliszyk@35222
   626
  For constants:
kaliszyk@35222
   627
kaliszyk@35222
   628
  * If the constant is (op =), we leave it always unchanged.
kaliszyk@35222
   629
    Otherwise the type of the constant needs lifting, we put
kaliszyk@35222
   630
    and Rep/Abs around it.
kaliszyk@35222
   631
kaliszyk@35222
   632
  For free variables:
kaliszyk@35222
   633
kaliszyk@35222
   634
  * We put a Rep/Abs around it if the type needs lifting.
kaliszyk@35222
   635
kaliszyk@35222
   636
  Vars case cannot occur.
kaliszyk@35222
   637
*)
kaliszyk@35222
   638
kaliszyk@35222
   639
fun mk_repabs ctxt (T, T') trm =
kaliszyk@35222
   640
  absrep_fun RepF ctxt (T, T') $ (absrep_fun AbsF ctxt (T, T') $ trm)
kaliszyk@35222
   641
kaliszyk@35222
   642
fun inj_repabs_err ctxt msg rtrm qtrm =
wenzelm@41444
   643
  let
wenzelm@41444
   644
    val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   645
    val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   646
  in
wenzelm@41444
   647
    raise LIFT_MATCH (space_implode " " [msg, quote rtrm_str, "and", quote qtrm_str])
wenzelm@41444
   648
  end
kaliszyk@35222
   649
kaliszyk@35222
   650
kaliszyk@35222
   651
(* bound variables need to be treated properly,
kaliszyk@35222
   652
   as the type of subterms needs to be calculated   *)
kaliszyk@35222
   653
fun inj_repabs_trm ctxt (rtrm, qtrm) =
kaliszyk@35222
   654
 case (rtrm, qtrm) of
urbanc@38624
   655
    (Const (@{const_name Ball}, T) $ r $ t, Const (@{const_name All}, _) $ t') =>
urbanc@38624
   656
       Const (@{const_name Ball}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   657
urbanc@38624
   658
  | (Const (@{const_name Bex}, T) $ r $ t, Const (@{const_name Ex}, _) $ t') =>
urbanc@38624
   659
       Const (@{const_name Bex}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   660
urbanc@38624
   661
  | (Const (@{const_name Babs}, T) $ r $ t, t' as (Abs _)) =>
kaliszyk@35222
   662
      let
kaliszyk@35222
   663
        val rty = fastype_of rtrm
kaliszyk@35222
   664
        val qty = fastype_of qtrm
kaliszyk@35222
   665
      in
urbanc@38624
   666
        mk_repabs ctxt (rty, qty) (Const (@{const_name Babs}, T) $ r $ (inj_repabs_trm ctxt (t, t')))
kaliszyk@35222
   667
      end
kaliszyk@35222
   668
kaliszyk@35222
   669
  | (Abs (x, T, t), Abs (x', T', t')) =>
kaliszyk@35222
   670
      let
kaliszyk@35222
   671
        val rty = fastype_of rtrm
kaliszyk@35222
   672
        val qty = fastype_of qtrm
kaliszyk@35222
   673
        val (y, s) = Term.dest_abs (x, T, t)
kaliszyk@35222
   674
        val (_, s') = Term.dest_abs (x', T', t')
kaliszyk@35222
   675
        val yvar = Free (y, T)
kaliszyk@35222
   676
        val result = Term.lambda_name (y, yvar) (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   677
      in
kaliszyk@35222
   678
        if rty = qty then result
kaliszyk@35222
   679
        else mk_repabs ctxt (rty, qty) result
kaliszyk@35222
   680
      end
kaliszyk@35222
   681
kaliszyk@35222
   682
  | (t $ s, t' $ s') =>
kaliszyk@35222
   683
       (inj_repabs_trm ctxt (t, t')) $ (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   684
kaliszyk@35222
   685
  | (Free (_, T), Free (_, T')) =>
kaliszyk@35222
   686
        if T = T' then rtrm
kaliszyk@35222
   687
        else mk_repabs ctxt (T, T') rtrm
kaliszyk@35222
   688
haftmann@38864
   689
  | (_, Const (@{const_name HOL.eq}, _)) => rtrm
kaliszyk@35222
   690
kaliszyk@35222
   691
  | (_, Const (_, T')) =>
kaliszyk@35222
   692
      let
kaliszyk@35222
   693
        val rty = fastype_of rtrm
kaliszyk@35222
   694
      in
kaliszyk@35222
   695
        if rty = T' then rtrm
kaliszyk@35222
   696
        else mk_repabs ctxt (rty, T') rtrm
kaliszyk@35222
   697
      end
kaliszyk@35222
   698
kaliszyk@35222
   699
  | _ => inj_repabs_err ctxt "injection (default):" rtrm qtrm
kaliszyk@35222
   700
kaliszyk@35222
   701
fun inj_repabs_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   702
  inj_repabs_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   703
  |> Syntax.check_term ctxt
kaliszyk@35222
   704
kaliszyk@35222
   705
kaliszyk@35222
   706
kaliszyk@35222
   707
(*** Wrapper for automatically transforming an rthm into a qthm ***)
kaliszyk@35222
   708
urbanc@37592
   709
(* substitutions functions for r/q-types and
urbanc@37592
   710
   r/q-constants, respectively
urbanc@37560
   711
*)
urbanc@37592
   712
fun subst_typ ctxt ty_subst rty =
urbanc@37560
   713
  case rty of
urbanc@37560
   714
    Type (s, rtys) =>
urbanc@37560
   715
      let
wenzelm@42361
   716
        val thy = Proof_Context.theory_of ctxt
urbanc@37592
   717
        val rty' = Type (s, map (subst_typ ctxt ty_subst) rtys)
urbanc@37560
   718
urbanc@37560
   719
        fun matches [] = rty'
urbanc@37560
   720
          | matches ((rty, qty)::tail) =
wenzelm@45280
   721
              (case try (Sign.typ_match thy (rty, rty')) Vartab.empty of
urbanc@37560
   722
                NONE => matches tail
wenzelm@45280
   723
              | SOME inst => Envir.subst_type inst qty)
urbanc@37560
   724
      in
wenzelm@41444
   725
        matches ty_subst
wenzelm@41444
   726
      end
urbanc@37560
   727
  | _ => rty
urbanc@37560
   728
urbanc@37592
   729
fun subst_trm ctxt ty_subst trm_subst rtrm =
urbanc@37560
   730
  case rtrm of
urbanc@37592
   731
    t1 $ t2 => (subst_trm ctxt ty_subst trm_subst t1) $ (subst_trm ctxt ty_subst trm_subst t2)
urbanc@37592
   732
  | Abs (x, ty, t) => Abs (x, subst_typ ctxt ty_subst ty, subst_trm ctxt ty_subst trm_subst t)
urbanc@37592
   733
  | Free(n, ty) => Free(n, subst_typ ctxt ty_subst ty)
urbanc@37592
   734
  | Var(n, ty) => Var(n, subst_typ ctxt ty_subst ty)
urbanc@37560
   735
  | Bound i => Bound i
wenzelm@41444
   736
  | Const (a, ty) =>
urbanc@37560
   737
      let
wenzelm@42361
   738
        val thy = Proof_Context.theory_of ctxt
kaliszyk@35222
   739
urbanc@37592
   740
        fun matches [] = Const (a, subst_typ ctxt ty_subst ty)
urbanc@37560
   741
          | matches ((rconst, qconst)::tail) =
wenzelm@45280
   742
              (case try (Pattern.match thy (rconst, rtrm)) (Vartab.empty, Vartab.empty) of
urbanc@37560
   743
                NONE => matches tail
wenzelm@45280
   744
              | SOME inst => Envir.subst_term inst qconst)
urbanc@37560
   745
      in
urbanc@37560
   746
        matches trm_subst
urbanc@37560
   747
      end
urbanc@37560
   748
urbanc@37592
   749
(* generate type and term substitutions out of the
wenzelm@41444
   750
   qtypes involved in a quotient; the direction flag
wenzelm@41444
   751
   indicates in which direction the substitutions work:
wenzelm@41444
   752
urbanc@37592
   753
     true:  quotient -> raw
urbanc@37592
   754
     false: raw -> quotient
urbanc@37560
   755
*)
urbanc@37592
   756
fun mk_ty_subst qtys direction ctxt =
wenzelm@41444
   757
  let
wenzelm@42361
   758
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   759
  in
wenzelm@45279
   760
    Quotient_Info.dest_quotients ctxt
wenzelm@41444
   761
    |> map (fn x => (#rtyp x, #qtyp x))
wenzelm@41444
   762
    |> filter (fn (_, qty) => member (Sign.typ_instance thy o swap) qtys qty)
wenzelm@41444
   763
    |> map (if direction then swap else I)
wenzelm@41444
   764
  end
kaliszyk@35222
   765
urbanc@37592
   766
fun mk_trm_subst qtys direction ctxt =
wenzelm@41444
   767
  let
wenzelm@41444
   768
    val subst_typ' = subst_typ ctxt (mk_ty_subst qtys direction ctxt)
wenzelm@41444
   769
    fun proper (t1, t2) = subst_typ' (fastype_of t1) = fastype_of t2
kaliszyk@37563
   770
wenzelm@41444
   771
    val const_substs =
wenzelm@45279
   772
      Quotient_Info.dest_quotconsts ctxt
wenzelm@41444
   773
      |> map (fn x => (#rconst x, #qconst x))
wenzelm@41444
   774
      |> map (if direction then swap else I)
urbanc@37560
   775
wenzelm@41444
   776
    val rel_substs =
wenzelm@45279
   777
      Quotient_Info.dest_quotients ctxt
wenzelm@41444
   778
      |> map (fn x => (#equiv_rel x, HOLogic.eq_const (#qtyp x)))
wenzelm@41444
   779
      |> map (if direction then swap else I)
wenzelm@41444
   780
  in
wenzelm@41444
   781
    filter proper (const_substs @ rel_substs)
wenzelm@41444
   782
  end
kaliszyk@35222
   783
urbanc@37592
   784
urbanc@37560
   785
(* derives a qtyp and qtrm out of a rtyp and rtrm,
wenzelm@41444
   786
   respectively
urbanc@37560
   787
*)
urbanc@38624
   788
fun derive_qtyp ctxt qtys rty =
urbanc@37592
   789
  subst_typ ctxt (mk_ty_subst qtys false ctxt) rty
urbanc@37592
   790
urbanc@38624
   791
fun derive_qtrm ctxt qtys rtrm =
urbanc@37592
   792
  subst_trm ctxt (mk_ty_subst qtys false ctxt) (mk_trm_subst qtys false ctxt) rtrm
kaliszyk@35222
   793
urbanc@37592
   794
(* derives a rtyp and rtrm out of a qtyp and qtrm,
wenzelm@41444
   795
   respectively
urbanc@37592
   796
*)
urbanc@38624
   797
fun derive_rtyp ctxt qtys qty =
urbanc@37592
   798
  subst_typ ctxt (mk_ty_subst qtys true ctxt) qty
urbanc@37592
   799
urbanc@38624
   800
fun derive_rtrm ctxt qtys qtrm =
urbanc@37592
   801
  subst_trm ctxt (mk_ty_subst qtys true ctxt) (mk_trm_subst qtys true ctxt) qtrm
urbanc@37560
   802
kaliszyk@35222
   803
wenzelm@45279
   804
end;