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