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