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