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