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