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