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