src/HOL/Tools/Quotient/quotient_term.ML
author kuncar
Fri Dec 09 14:14:37 2011 +0100 (2011-12-09)
changeset 45797 977cf00fb8d3
parent 45796 b2205eb270e3
child 46416 5f5665a0b973
permissions -rw-r--r--
make ctxt the first parameter
haftmann@37744
     1
(*  Title:      HOL/Tools/Quotient/quotient_term.ML
kaliszyk@35222
     2
    Author:     Cezary Kaliszyk and Christian Urban
kaliszyk@35222
     3
wenzelm@35788
     4
Constructs terms corresponding to goals from lifting theorems to
wenzelm@35788
     5
quotient types.
kaliszyk@35222
     6
*)
kaliszyk@35222
     7
kaliszyk@35222
     8
signature QUOTIENT_TERM =
kaliszyk@35222
     9
sig
kaliszyk@35222
    10
  exception LIFT_MATCH of string
kaliszyk@35222
    11
kaliszyk@35222
    12
  datatype flag = AbsF | RepF
kaliszyk@35222
    13
kuncar@45797
    14
  val absrep_fun: Proof.context -> flag -> typ * typ -> term
kuncar@45797
    15
  val absrep_fun_chk: Proof.context -> flag -> typ * typ -> term
kaliszyk@35222
    16
kaliszyk@35222
    17
  (* Allows Nitpick to represent quotient types as single elements from raw type *)
kuncar@45797
    18
  val absrep_const_chk: Proof.context -> flag -> string -> term
kaliszyk@35222
    19
kaliszyk@35222
    20
  val equiv_relation: Proof.context -> typ * typ -> term
kaliszyk@35222
    21
  val equiv_relation_chk: Proof.context -> typ * typ -> term
kaliszyk@35222
    22
kaliszyk@35222
    23
  val regularize_trm: Proof.context -> term * term -> term
kaliszyk@35222
    24
  val regularize_trm_chk: Proof.context -> term * term -> term
kaliszyk@35222
    25
kaliszyk@35222
    26
  val inj_repabs_trm: Proof.context -> term * term -> term
kaliszyk@35222
    27
  val inj_repabs_trm_chk: Proof.context -> term * term -> term
kaliszyk@35222
    28
urbanc@38624
    29
  val derive_qtyp: Proof.context -> typ list -> typ -> typ
urbanc@38624
    30
  val derive_qtrm: Proof.context -> typ list -> term -> term
urbanc@38624
    31
  val derive_rtyp: Proof.context -> typ list -> typ -> typ
urbanc@38624
    32
  val derive_rtrm: Proof.context -> typ list -> term -> term
kaliszyk@35222
    33
end;
kaliszyk@35222
    34
kaliszyk@35222
    35
structure Quotient_Term: QUOTIENT_TERM =
kaliszyk@35222
    36
struct
kaliszyk@35222
    37
kaliszyk@35222
    38
exception LIFT_MATCH of string
kaliszyk@35222
    39
kaliszyk@35222
    40
kaliszyk@35222
    41
kaliszyk@35222
    42
(*** Aggregate Rep/Abs Function ***)
kaliszyk@35222
    43
kaliszyk@35222
    44
kaliszyk@35222
    45
(* The flag RepF is for types in negative position; AbsF is for types
kaliszyk@35222
    46
   in positive position. Because of this, function types need to be
kaliszyk@35222
    47
   treated specially, since there the polarity changes.
kaliszyk@35222
    48
*)
kaliszyk@35222
    49
kaliszyk@35222
    50
datatype flag = AbsF | RepF
kaliszyk@35222
    51
kaliszyk@35222
    52
fun negF AbsF = RepF
kaliszyk@35222
    53
  | negF RepF = AbsF
kaliszyk@35222
    54
haftmann@37677
    55
fun is_identity (Const (@{const_name id}, _)) = true
kaliszyk@35222
    56
  | is_identity _ = false
kaliszyk@35222
    57
haftmann@37677
    58
fun mk_identity ty = Const (@{const_name id}, ty --> ty)
kaliszyk@35222
    59
kaliszyk@35222
    60
fun mk_fun_compose flag (trm1, trm2) =
kaliszyk@35222
    61
  case flag of
haftmann@37677
    62
    AbsF => Const (@{const_name comp}, dummyT) $ trm1 $ trm2
haftmann@37677
    63
  | RepF => Const (@{const_name comp}, dummyT) $ trm2 $ trm1
kaliszyk@35222
    64
kuncar@45796
    65
fun get_mapfun_data ctxt s =
kuncar@45796
    66
  (case Symtab.lookup (Enriched_Type.entries ctxt) s of
kuncar@45795
    67
    SOME [map_data] => (case try dest_Const (#mapper map_data) of
kuncar@45795
    68
      SOME (c, _) => (Const (c, dummyT), #variances map_data)
kuncar@45795
    69
    | NONE => raise LIFT_MATCH ("map function for type " ^ quote s ^ " is not a constant."))
kuncar@45795
    70
  | SOME _ => raise LIFT_MATCH ("map function for type " ^ quote s ^ " is non-singleton entry.")
kuncar@45795
    71
  | NONE => raise LIFT_MATCH ("No map function for type " ^ quote s ^ " found.")) 
kaliszyk@35222
    72
kuncar@45796
    73
fun defined_mapfun_data ctxt s =
kuncar@45796
    74
  Symtab.defined (Enriched_Type.entries ctxt) s
kuncar@45795
    75
  
kaliszyk@35222
    76
(* makes a Free out of a TVar *)
kaliszyk@35222
    77
fun mk_Free (TVar ((x, i), _)) = Free (unprefix "'" x ^ string_of_int i, dummyT)
kaliszyk@35222
    78
kaliszyk@35222
    79
(* looks up the (varified) rty and qty for
kaliszyk@35222
    80
   a quotient definition
kaliszyk@35222
    81
*)
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
kuncar@45795
   282
(* takes two type-environments and looks
kuncar@45795
   283
   up in both of them the variable v, which
kuncar@45795
   284
   must be listed in the environment
kuncar@45795
   285
*)
kuncar@45795
   286
fun double_lookup rtyenv qtyenv v =
kuncar@45795
   287
  let
kuncar@45795
   288
    val v' = fst (dest_TVar v)
kuncar@45795
   289
  in
kuncar@45795
   290
    (snd (the (Vartab.lookup rtyenv v')), snd (the (Vartab.lookup qtyenv v')))
kuncar@45795
   291
  end
kuncar@45795
   292
kaliszyk@35222
   293
fun mk_relmap ctxt vs rty =
wenzelm@41444
   294
  let
wenzelm@41444
   295
    val vs' = map (mk_Free) vs
kaliszyk@35222
   296
wenzelm@41444
   297
    fun mk_relmap_aux rty =
wenzelm@41444
   298
      case rty of
wenzelm@41444
   299
        TVar _ => mk_Free rty
wenzelm@41444
   300
      | Type (_, []) => HOLogic.eq_const rty
wenzelm@41444
   301
      | Type (s, tys) => list_comb (get_relmap ctxt s, map mk_relmap_aux tys)
wenzelm@41444
   302
      | _ => raise LIFT_MATCH ("mk_relmap (default)")
wenzelm@41444
   303
  in
wenzelm@41444
   304
    fold_rev Term.lambda vs' (mk_relmap_aux rty)
wenzelm@41444
   305
  end
kaliszyk@35222
   306
wenzelm@45340
   307
fun get_equiv_rel thy s =
wenzelm@45340
   308
  (case Quotient_Info.lookup_quotients thy s of
wenzelm@45279
   309
    SOME qdata => #equiv_rel qdata
wenzelm@45279
   310
  | NONE => raise LIFT_MATCH ("get_quotdata (no quotient found for type " ^ s ^ ")"))
kaliszyk@35222
   311
kaliszyk@35222
   312
fun equiv_match_err ctxt ty_pat ty =
wenzelm@41444
   313
  let
wenzelm@41444
   314
    val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
wenzelm@41444
   315
    val ty_str = Syntax.string_of_typ ctxt ty
wenzelm@41444
   316
  in
wenzelm@41444
   317
    raise LIFT_MATCH (space_implode " "
wenzelm@41444
   318
      ["equiv_relation (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
wenzelm@41444
   319
  end
kaliszyk@35222
   320
kaliszyk@35222
   321
(* builds the aggregate equivalence relation
kaliszyk@35222
   322
   that will be the argument of Respects
kaliszyk@35222
   323
*)
kaliszyk@35222
   324
fun equiv_relation ctxt (rty, qty) =
kuncar@45796
   325
  if rty = qty
kuncar@45796
   326
  then HOLogic.eq_const rty
kuncar@45796
   327
  else
kuncar@45796
   328
    case (rty, qty) of
kuncar@45796
   329
      (Type (s, tys), Type (s', tys')) =>
kuncar@45796
   330
        if s = s'
kuncar@45796
   331
        then
kuncar@45796
   332
          let
kuncar@45796
   333
            val args = map (equiv_relation ctxt) (tys ~~ tys')
kuncar@45796
   334
          in
kuncar@45796
   335
            list_comb (get_relmap ctxt s, args)
kuncar@45796
   336
          end
kuncar@45796
   337
        else
kuncar@45796
   338
          let
kuncar@45796
   339
            val (rty_pat, qty_pat as Type (_, vs)) = get_rty_qty ctxt s'
kuncar@45796
   340
            val rtyenv = match ctxt equiv_match_err rty_pat rty
kuncar@45796
   341
            val qtyenv = match ctxt equiv_match_err qty_pat qty
kuncar@45796
   342
            val args_aux = map (double_lookup rtyenv qtyenv) vs
kuncar@45796
   343
            val args = map (equiv_relation ctxt) args_aux
kuncar@45796
   344
            val eqv_rel = get_equiv_rel ctxt s'
kuncar@45796
   345
            val eqv_rel' = force_typ ctxt eqv_rel ([rty, rty] ---> @{typ bool})
kuncar@45796
   346
          in
kuncar@45796
   347
            if forall is_eq args
kuncar@45796
   348
            then eqv_rel'
kuncar@45796
   349
            else
kuncar@45796
   350
              let
kuncar@45796
   351
                val rel_map = mk_relmap ctxt vs rty_pat
kuncar@45796
   352
                val result = list_comb (rel_map, args)
kuncar@45796
   353
              in
kuncar@45796
   354
                mk_rel_compose (result, eqv_rel')
kuncar@45796
   355
              end
kuncar@45796
   356
          end
kuncar@45796
   357
    | _ => HOLogic.eq_const rty
kuncar@45796
   358
kaliszyk@35222
   359
kaliszyk@35222
   360
fun equiv_relation_chk ctxt (rty, qty) =
kaliszyk@35222
   361
  equiv_relation ctxt (rty, qty)
kaliszyk@35222
   362
  |> Syntax.check_term ctxt
kaliszyk@35222
   363
kaliszyk@35222
   364
kaliszyk@35222
   365
kaliszyk@35222
   366
(*** Regularization ***)
kaliszyk@35222
   367
kaliszyk@35222
   368
(* Regularizing an rtrm means:
kaliszyk@35222
   369
kaliszyk@35222
   370
 - Quantifiers over types that need lifting are replaced
kaliszyk@35222
   371
   by bounded quantifiers, for example:
kaliszyk@35222
   372
kaliszyk@35222
   373
      All P  ----> All (Respects R) P
kaliszyk@35222
   374
kaliszyk@35222
   375
   where the aggregate relation R is given by the rty and qty;
kaliszyk@35222
   376
kaliszyk@35222
   377
 - Abstractions over types that need lifting are replaced
kaliszyk@35222
   378
   by bounded abstractions, for example:
kaliszyk@35222
   379
kaliszyk@35222
   380
      %x. P  ----> Ball (Respects R) %x. P
kaliszyk@35222
   381
kaliszyk@35222
   382
 - Equalities over types that need lifting are replaced by
kaliszyk@35222
   383
   corresponding equivalence relations, for example:
kaliszyk@35222
   384
kaliszyk@35222
   385
      A = B  ----> R A B
kaliszyk@35222
   386
kaliszyk@35222
   387
   or
kaliszyk@35222
   388
kaliszyk@35222
   389
      A = B  ----> (R ===> R) A B
kaliszyk@35222
   390
kaliszyk@35222
   391
   for more complicated types of A and B
kaliszyk@35222
   392
kaliszyk@35222
   393
kaliszyk@35222
   394
 The regularize_trm accepts raw theorems in which equalities
kaliszyk@35222
   395
 and quantifiers match exactly the ones in the lifted theorem
kaliszyk@35222
   396
 but also accepts partially regularized terms.
kaliszyk@35222
   397
kaliszyk@35222
   398
 This means that the raw theorems can have:
kaliszyk@35222
   399
   Ball (Respects R),  Bex (Respects R), Bex1_rel (Respects R), Babs, R
kaliszyk@35222
   400
 in the places where:
kaliszyk@35222
   401
   All, Ex, Ex1, %, (op =)
kaliszyk@35222
   402
 is required the lifted theorem.
kaliszyk@35222
   403
kaliszyk@35222
   404
*)
kaliszyk@35222
   405
kaliszyk@35222
   406
val mk_babs = Const (@{const_name Babs}, dummyT)
kaliszyk@35222
   407
val mk_ball = Const (@{const_name Ball}, dummyT)
kaliszyk@35222
   408
val mk_bex  = Const (@{const_name Bex}, dummyT)
kaliszyk@35222
   409
val mk_bex1_rel = Const (@{const_name Bex1_rel}, dummyT)
kaliszyk@35222
   410
val mk_resp = Const (@{const_name Respects}, dummyT)
kaliszyk@35222
   411
kaliszyk@35222
   412
(* - applies f to the subterm of an abstraction,
kaliszyk@35222
   413
     otherwise to the given term,
kaliszyk@35222
   414
   - used by regularize, therefore abstracted
kaliszyk@35222
   415
     variables do not have to be treated specially
kaliszyk@35222
   416
*)
kaliszyk@35222
   417
fun apply_subt f (trm1, trm2) =
kaliszyk@35222
   418
  case (trm1, trm2) of
kaliszyk@35222
   419
    (Abs (x, T, t), Abs (_ , _, t')) => Abs (x, T, f (t, t'))
kaliszyk@35222
   420
  | _ => f (trm1, trm2)
kaliszyk@35222
   421
kaliszyk@35222
   422
fun term_mismatch str ctxt t1 t2 =
wenzelm@41444
   423
  let
wenzelm@41444
   424
    val t1_str = Syntax.string_of_term ctxt t1
wenzelm@41444
   425
    val t2_str = Syntax.string_of_term ctxt t2
wenzelm@41444
   426
    val t1_ty_str = Syntax.string_of_typ ctxt (fastype_of t1)
wenzelm@41444
   427
    val t2_ty_str = Syntax.string_of_typ ctxt (fastype_of t2)
wenzelm@41444
   428
  in
wenzelm@41444
   429
    raise LIFT_MATCH (cat_lines [str, t1_str ^ "::" ^ t1_ty_str, t2_str ^ "::" ^ t2_ty_str])
wenzelm@41444
   430
  end
kaliszyk@35222
   431
kaliszyk@35222
   432
(* the major type of All and Ex quantifiers *)
kaliszyk@35222
   433
fun qnt_typ ty = domain_type (domain_type ty)
kaliszyk@35222
   434
kaliszyk@35222
   435
(* Checks that two types match, for example:
kaliszyk@35222
   436
     rty -> rty   matches   qty -> qty *)
wenzelm@45280
   437
fun matches_typ ctxt rT qT =
wenzelm@45340
   438
  let
wenzelm@45340
   439
    val thy = Proof_Context.theory_of ctxt
wenzelm@45340
   440
  in
wenzelm@45340
   441
    if rT = qT then true
wenzelm@45340
   442
    else
wenzelm@45340
   443
      (case (rT, qT) of
wenzelm@45340
   444
        (Type (rs, rtys), Type (qs, qtys)) =>
wenzelm@45340
   445
          if rs = qs then
wenzelm@45340
   446
            if length rtys <> length qtys then false
wenzelm@45340
   447
            else forall (fn x => x = true) (map2 (matches_typ ctxt) rtys qtys)
wenzelm@45340
   448
          else
wenzelm@45340
   449
            (case Quotient_Info.lookup_quotients_global thy qs of
wenzelm@45340
   450
              SOME quotinfo => Sign.typ_instance thy (rT, #rtyp quotinfo)
wenzelm@45340
   451
            | NONE => false)
wenzelm@45340
   452
      | _ => false)
wenzelm@45340
   453
  end
kaliszyk@35222
   454
kaliszyk@35222
   455
kaliszyk@35222
   456
(* produces a regularized version of rtrm
kaliszyk@35222
   457
kaliszyk@35222
   458
   - the result might contain dummyTs
kaliszyk@35222
   459
urbanc@38718
   460
   - for regularization we do not need any
kaliszyk@35222
   461
     special treatment of bound variables
kaliszyk@35222
   462
*)
kaliszyk@35222
   463
fun regularize_trm ctxt (rtrm, qtrm) =
wenzelm@45280
   464
  (case (rtrm, qtrm) of
kaliszyk@35222
   465
    (Abs (x, ty, t), Abs (_, ty', t')) =>
wenzelm@41444
   466
      let
wenzelm@41444
   467
        val subtrm = Abs(x, ty, regularize_trm ctxt (t, t'))
wenzelm@41444
   468
      in
wenzelm@41444
   469
        if ty = ty' then subtrm
wenzelm@41444
   470
        else mk_babs $ (mk_resp $ equiv_relation ctxt (ty, ty')) $ subtrm
wenzelm@41444
   471
      end
wenzelm@45280
   472
haftmann@37677
   473
  | (Const (@{const_name Babs}, T) $ resrel $ (t as (Abs (_, ty, _))), t' as (Abs (_, ty', _))) =>
wenzelm@41444
   474
      let
wenzelm@41444
   475
        val subtrm = regularize_trm ctxt (t, t')
wenzelm@41444
   476
        val needres = mk_resp $ equiv_relation_chk ctxt (ty, ty')
wenzelm@41444
   477
      in
wenzelm@41444
   478
        if resrel <> needres
wenzelm@41444
   479
        then term_mismatch "regularize (Babs)" ctxt resrel needres
wenzelm@41444
   480
        else mk_babs $ resrel $ subtrm
wenzelm@41444
   481
      end
kaliszyk@35222
   482
haftmann@37677
   483
  | (Const (@{const_name All}, ty) $ t, Const (@{const_name All}, ty') $ t') =>
wenzelm@41444
   484
      let
wenzelm@41444
   485
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   486
      in
wenzelm@41444
   487
        if ty = ty' then Const (@{const_name All}, ty) $ subtrm
wenzelm@41444
   488
        else mk_ball $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   489
      end
kaliszyk@35222
   490
haftmann@37677
   491
  | (Const (@{const_name Ex}, ty) $ t, Const (@{const_name Ex}, ty') $ t') =>
wenzelm@41444
   492
      let
wenzelm@41444
   493
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   494
      in
wenzelm@41444
   495
        if ty = ty' then Const (@{const_name Ex}, ty) $ subtrm
wenzelm@41444
   496
        else mk_bex $ (mk_resp $ equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   497
      end
kaliszyk@35222
   498
haftmann@37677
   499
  | (Const (@{const_name Ex1}, ty) $ (Abs (_, _,
haftmann@38795
   500
      (Const (@{const_name HOL.conj}, _) $ (Const (@{const_name Set.member}, _) $ _ $
haftmann@37677
   501
        (Const (@{const_name Respects}, _) $ resrel)) $ (t $ _)))),
haftmann@37677
   502
     Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   503
      let
wenzelm@41444
   504
        val t_ = incr_boundvars (~1) t
wenzelm@41444
   505
        val subtrm = apply_subt (regularize_trm ctxt) (t_, t')
wenzelm@41444
   506
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   507
      in
wenzelm@41444
   508
        if resrel <> needrel
wenzelm@41444
   509
        then term_mismatch "regularize (Bex1)" ctxt resrel needrel
wenzelm@41444
   510
        else mk_bex1_rel $ resrel $ subtrm
wenzelm@41444
   511
      end
kaliszyk@35222
   512
haftmann@38558
   513
  | (Const (@{const_name Ex1}, ty) $ t, Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   514
      let
wenzelm@41444
   515
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   516
      in
wenzelm@41444
   517
        if ty = ty' then Const (@{const_name Ex1}, ty) $ subtrm
wenzelm@41444
   518
        else mk_bex1_rel $ (equiv_relation ctxt (qnt_typ ty, qnt_typ ty')) $ subtrm
wenzelm@41444
   519
      end
kaliszyk@35222
   520
urbanc@38624
   521
  | (Const (@{const_name Ball}, ty) $ (Const (@{const_name Respects}, _) $ resrel) $ t,
haftmann@38558
   522
     Const (@{const_name All}, ty') $ t') =>
wenzelm@41444
   523
      let
wenzelm@41444
   524
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   525
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   526
      in
wenzelm@41444
   527
        if resrel <> needrel
wenzelm@41444
   528
        then term_mismatch "regularize (Ball)" ctxt resrel needrel
wenzelm@41444
   529
        else mk_ball $ (mk_resp $ resrel) $ subtrm
wenzelm@41444
   530
      end
kaliszyk@35222
   531
urbanc@38624
   532
  | (Const (@{const_name Bex}, ty) $ (Const (@{const_name Respects}, _) $ resrel) $ t,
haftmann@38558
   533
     Const (@{const_name Ex}, ty') $ t') =>
wenzelm@41444
   534
      let
wenzelm@41444
   535
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   536
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   537
      in
wenzelm@41444
   538
        if resrel <> needrel
wenzelm@41444
   539
        then term_mismatch "regularize (Bex)" ctxt resrel needrel
wenzelm@41444
   540
        else mk_bex $ (mk_resp $ resrel) $ subtrm
wenzelm@41444
   541
      end
kaliszyk@35222
   542
urbanc@38624
   543
  | (Const (@{const_name Bex1_rel}, ty) $ resrel $ t, Const (@{const_name Ex1}, ty') $ t') =>
wenzelm@41444
   544
      let
wenzelm@41444
   545
        val subtrm = apply_subt (regularize_trm ctxt) (t, t')
wenzelm@41444
   546
        val needrel = equiv_relation_chk ctxt (qnt_typ ty, qnt_typ ty')
wenzelm@41444
   547
      in
wenzelm@41444
   548
        if resrel <> needrel
wenzelm@41444
   549
        then term_mismatch "regularize (Bex1_res)" ctxt resrel needrel
wenzelm@41444
   550
        else mk_bex1_rel $ resrel $ subtrm
wenzelm@41444
   551
      end
kaliszyk@35222
   552
kaliszyk@35222
   553
  | (* equalities need to be replaced by appropriate equivalence relations *)
haftmann@38864
   554
    (Const (@{const_name HOL.eq}, ty), Const (@{const_name HOL.eq}, ty')) =>
wenzelm@41444
   555
        if ty = ty' then rtrm
wenzelm@41444
   556
        else equiv_relation ctxt (domain_type ty, domain_type ty')
kaliszyk@35222
   557
kaliszyk@35222
   558
  | (* in this case we just check whether the given equivalence relation is correct *)
haftmann@38864
   559
    (rel, Const (@{const_name HOL.eq}, ty')) =>
wenzelm@41444
   560
      let
wenzelm@41444
   561
        val rel_ty = fastype_of rel
wenzelm@41444
   562
        val rel' = equiv_relation_chk ctxt (domain_type rel_ty, domain_type ty')
wenzelm@41444
   563
      in
wenzelm@41444
   564
        if rel' aconv rel then rtrm
wenzelm@41444
   565
        else term_mismatch "regularize (relation mismatch)" ctxt rel rel'
wenzelm@41444
   566
      end
kaliszyk@35222
   567
kaliszyk@35222
   568
  | (_, Const _) =>
wenzelm@41444
   569
      let
wenzelm@42361
   570
        val thy = Proof_Context.theory_of ctxt
wenzelm@45280
   571
        fun same_const (Const (s, T)) (Const (s', T')) = s = s' andalso matches_typ ctxt T T'
wenzelm@41444
   572
          | same_const _ _ = false
wenzelm@41444
   573
      in
wenzelm@41444
   574
        if same_const rtrm qtrm then rtrm
wenzelm@41444
   575
        else
wenzelm@41444
   576
          let
wenzelm@45279
   577
            val rtrm' =
wenzelm@45340
   578
              (case Quotient_Info.lookup_quotconsts_global thy qtrm of
wenzelm@45279
   579
                SOME qconst_info => #rconst qconst_info
wenzelm@45279
   580
              | NONE => term_mismatch "regularize (constant not found)" ctxt rtrm qtrm)
wenzelm@41444
   581
          in
wenzelm@41444
   582
            if Pattern.matches thy (rtrm', rtrm)
wenzelm@41444
   583
            then rtrm else term_mismatch "regularize (constant mismatch)" ctxt rtrm qtrm
wenzelm@41444
   584
          end
wenzelm@41444
   585
      end
kaliszyk@35222
   586
haftmann@37591
   587
  | (((t1 as Const (@{const_name prod_case}, _)) $ Abs (v1, ty, Abs(v1', ty', s1))),
haftmann@37591
   588
     ((t2 as Const (@{const_name prod_case}, _)) $ Abs (v2, _ , Abs(v2', _  , s2)))) =>
kaliszyk@35222
   589
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, Abs (v1', ty', regularize_trm ctxt (s1, s2)))
kaliszyk@35222
   590
haftmann@37591
   591
  | (((t1 as Const (@{const_name prod_case}, _)) $ Abs (v1, ty, s1)),
haftmann@37591
   592
     ((t2 as Const (@{const_name prod_case}, _)) $ Abs (v2, _ , s2))) =>
kaliszyk@35222
   593
       regularize_trm ctxt (t1, t2) $ Abs (v1, ty, regularize_trm ctxt (s1, s2))
kaliszyk@35222
   594
kaliszyk@35222
   595
  | (t1 $ t2, t1' $ t2') =>
kaliszyk@35222
   596
       regularize_trm ctxt (t1, t1') $ regularize_trm ctxt (t2, t2')
kaliszyk@35222
   597
kaliszyk@35222
   598
  | (Bound i, Bound i') =>
wenzelm@41444
   599
      if i = i' then rtrm
wenzelm@41444
   600
      else raise (LIFT_MATCH "regularize (bounds mismatch)")
kaliszyk@35222
   601
kaliszyk@35222
   602
  | _ =>
wenzelm@41444
   603
      let
wenzelm@41444
   604
        val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   605
        val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   606
      in
wenzelm@41444
   607
        raise (LIFT_MATCH ("regularize failed (default: " ^ rtrm_str ^ "," ^ qtrm_str ^ ")"))
wenzelm@45280
   608
      end)
kaliszyk@35222
   609
kaliszyk@35222
   610
fun regularize_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   611
  regularize_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   612
  |> Syntax.check_term ctxt
kaliszyk@35222
   613
kaliszyk@35222
   614
kaliszyk@35222
   615
kaliszyk@35222
   616
(*** Rep/Abs Injection ***)
kaliszyk@35222
   617
kaliszyk@35222
   618
(*
kaliszyk@35222
   619
Injection of Rep/Abs means:
kaliszyk@35222
   620
kaliszyk@35222
   621
  For abstractions:
kaliszyk@35222
   622
kaliszyk@35222
   623
  * If the type of the abstraction needs lifting, then we add Rep/Abs
kaliszyk@35222
   624
    around the abstraction; otherwise we leave it unchanged.
kaliszyk@35222
   625
kaliszyk@35222
   626
  For applications:
kaliszyk@35222
   627
kaliszyk@35222
   628
  * If the application involves a bounded quantifier, we recurse on
kaliszyk@35222
   629
    the second argument. If the application is a bounded abstraction,
kaliszyk@35222
   630
    we always put an Rep/Abs around it (since bounded abstractions
kaliszyk@35222
   631
    are assumed to always need lifting). Otherwise we recurse on both
kaliszyk@35222
   632
    arguments.
kaliszyk@35222
   633
kaliszyk@35222
   634
  For constants:
kaliszyk@35222
   635
kaliszyk@35222
   636
  * If the constant is (op =), we leave it always unchanged.
kaliszyk@35222
   637
    Otherwise the type of the constant needs lifting, we put
kaliszyk@35222
   638
    and Rep/Abs around it.
kaliszyk@35222
   639
kaliszyk@35222
   640
  For free variables:
kaliszyk@35222
   641
kaliszyk@35222
   642
  * We put a Rep/Abs around it if the type needs lifting.
kaliszyk@35222
   643
kaliszyk@35222
   644
  Vars case cannot occur.
kaliszyk@35222
   645
*)
kaliszyk@35222
   646
kaliszyk@35222
   647
fun mk_repabs ctxt (T, T') trm =
kuncar@45797
   648
  absrep_fun ctxt RepF (T, T') $ (absrep_fun ctxt AbsF (T, T') $ trm)
kaliszyk@35222
   649
kaliszyk@35222
   650
fun inj_repabs_err ctxt msg rtrm qtrm =
wenzelm@41444
   651
  let
wenzelm@41444
   652
    val rtrm_str = Syntax.string_of_term ctxt rtrm
wenzelm@41444
   653
    val qtrm_str = Syntax.string_of_term ctxt qtrm
wenzelm@41444
   654
  in
wenzelm@41444
   655
    raise LIFT_MATCH (space_implode " " [msg, quote rtrm_str, "and", quote qtrm_str])
wenzelm@41444
   656
  end
kaliszyk@35222
   657
kaliszyk@35222
   658
kaliszyk@35222
   659
(* bound variables need to be treated properly,
kaliszyk@35222
   660
   as the type of subterms needs to be calculated   *)
kaliszyk@35222
   661
fun inj_repabs_trm ctxt (rtrm, qtrm) =
kaliszyk@35222
   662
 case (rtrm, qtrm) of
urbanc@38624
   663
    (Const (@{const_name Ball}, T) $ r $ t, Const (@{const_name All}, _) $ t') =>
urbanc@38624
   664
       Const (@{const_name Ball}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   665
urbanc@38624
   666
  | (Const (@{const_name Bex}, T) $ r $ t, Const (@{const_name Ex}, _) $ t') =>
urbanc@38624
   667
       Const (@{const_name Bex}, T) $ r $ (inj_repabs_trm ctxt (t, t'))
kaliszyk@35222
   668
urbanc@38624
   669
  | (Const (@{const_name Babs}, T) $ r $ t, t' as (Abs _)) =>
kaliszyk@35222
   670
      let
kaliszyk@35222
   671
        val rty = fastype_of rtrm
kaliszyk@35222
   672
        val qty = fastype_of qtrm
kaliszyk@35222
   673
      in
urbanc@38624
   674
        mk_repabs ctxt (rty, qty) (Const (@{const_name Babs}, T) $ r $ (inj_repabs_trm ctxt (t, t')))
kaliszyk@35222
   675
      end
kaliszyk@35222
   676
kaliszyk@35222
   677
  | (Abs (x, T, t), Abs (x', T', t')) =>
kaliszyk@35222
   678
      let
kaliszyk@35222
   679
        val rty = fastype_of rtrm
kaliszyk@35222
   680
        val qty = fastype_of qtrm
kaliszyk@35222
   681
        val (y, s) = Term.dest_abs (x, T, t)
kaliszyk@35222
   682
        val (_, s') = Term.dest_abs (x', T', t')
kaliszyk@35222
   683
        val yvar = Free (y, T)
kaliszyk@35222
   684
        val result = Term.lambda_name (y, yvar) (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   685
      in
kaliszyk@35222
   686
        if rty = qty then result
kaliszyk@35222
   687
        else mk_repabs ctxt (rty, qty) result
kaliszyk@35222
   688
      end
kaliszyk@35222
   689
kaliszyk@35222
   690
  | (t $ s, t' $ s') =>
kaliszyk@35222
   691
       (inj_repabs_trm ctxt (t, t')) $ (inj_repabs_trm ctxt (s, s'))
kaliszyk@35222
   692
kaliszyk@35222
   693
  | (Free (_, T), Free (_, T')) =>
kaliszyk@35222
   694
        if T = T' then rtrm
kaliszyk@35222
   695
        else mk_repabs ctxt (T, T') rtrm
kaliszyk@35222
   696
haftmann@38864
   697
  | (_, Const (@{const_name HOL.eq}, _)) => rtrm
kaliszyk@35222
   698
kaliszyk@35222
   699
  | (_, Const (_, T')) =>
kaliszyk@35222
   700
      let
kaliszyk@35222
   701
        val rty = fastype_of rtrm
kaliszyk@35222
   702
      in
kaliszyk@35222
   703
        if rty = T' then rtrm
kaliszyk@35222
   704
        else mk_repabs ctxt (rty, T') rtrm
kaliszyk@35222
   705
      end
kaliszyk@35222
   706
kaliszyk@35222
   707
  | _ => inj_repabs_err ctxt "injection (default):" rtrm qtrm
kaliszyk@35222
   708
kaliszyk@35222
   709
fun inj_repabs_trm_chk ctxt (rtrm, qtrm) =
kaliszyk@35222
   710
  inj_repabs_trm ctxt (rtrm, qtrm)
kaliszyk@35222
   711
  |> Syntax.check_term ctxt
kaliszyk@35222
   712
kaliszyk@35222
   713
kaliszyk@35222
   714
kaliszyk@35222
   715
(*** Wrapper for automatically transforming an rthm into a qthm ***)
kaliszyk@35222
   716
urbanc@37592
   717
(* substitutions functions for r/q-types and
urbanc@37592
   718
   r/q-constants, respectively
urbanc@37560
   719
*)
urbanc@37592
   720
fun subst_typ ctxt ty_subst rty =
urbanc@37560
   721
  case rty of
urbanc@37560
   722
    Type (s, rtys) =>
urbanc@37560
   723
      let
wenzelm@42361
   724
        val thy = Proof_Context.theory_of ctxt
urbanc@37592
   725
        val rty' = Type (s, map (subst_typ ctxt ty_subst) rtys)
urbanc@37560
   726
urbanc@37560
   727
        fun matches [] = rty'
urbanc@37560
   728
          | matches ((rty, qty)::tail) =
wenzelm@45280
   729
              (case try (Sign.typ_match thy (rty, rty')) Vartab.empty of
urbanc@37560
   730
                NONE => matches tail
wenzelm@45280
   731
              | SOME inst => Envir.subst_type inst qty)
urbanc@37560
   732
      in
wenzelm@41444
   733
        matches ty_subst
wenzelm@41444
   734
      end
urbanc@37560
   735
  | _ => rty
urbanc@37560
   736
urbanc@37592
   737
fun subst_trm ctxt ty_subst trm_subst rtrm =
urbanc@37560
   738
  case rtrm of
urbanc@37592
   739
    t1 $ t2 => (subst_trm ctxt ty_subst trm_subst t1) $ (subst_trm ctxt ty_subst trm_subst t2)
urbanc@37592
   740
  | Abs (x, ty, t) => Abs (x, subst_typ ctxt ty_subst ty, subst_trm ctxt ty_subst trm_subst t)
urbanc@37592
   741
  | Free(n, ty) => Free(n, subst_typ ctxt ty_subst ty)
urbanc@37592
   742
  | Var(n, ty) => Var(n, subst_typ ctxt ty_subst ty)
urbanc@37560
   743
  | Bound i => Bound i
wenzelm@41444
   744
  | Const (a, ty) =>
urbanc@37560
   745
      let
wenzelm@42361
   746
        val thy = Proof_Context.theory_of ctxt
kaliszyk@35222
   747
urbanc@37592
   748
        fun matches [] = Const (a, subst_typ ctxt ty_subst ty)
urbanc@37560
   749
          | matches ((rconst, qconst)::tail) =
wenzelm@45280
   750
              (case try (Pattern.match thy (rconst, rtrm)) (Vartab.empty, Vartab.empty) of
urbanc@37560
   751
                NONE => matches tail
wenzelm@45280
   752
              | SOME inst => Envir.subst_term inst qconst)
urbanc@37560
   753
      in
urbanc@37560
   754
        matches trm_subst
urbanc@37560
   755
      end
urbanc@37560
   756
urbanc@37592
   757
(* generate type and term substitutions out of the
wenzelm@41444
   758
   qtypes involved in a quotient; the direction flag
wenzelm@41444
   759
   indicates in which direction the substitutions work:
wenzelm@41444
   760
urbanc@37592
   761
     true:  quotient -> raw
urbanc@37592
   762
     false: raw -> quotient
urbanc@37560
   763
*)
urbanc@37592
   764
fun mk_ty_subst qtys direction ctxt =
wenzelm@41444
   765
  let
wenzelm@42361
   766
    val thy = Proof_Context.theory_of ctxt
wenzelm@41444
   767
  in
wenzelm@45279
   768
    Quotient_Info.dest_quotients ctxt
wenzelm@41444
   769
    |> map (fn x => (#rtyp x, #qtyp x))
wenzelm@41444
   770
    |> filter (fn (_, qty) => member (Sign.typ_instance thy o swap) qtys qty)
wenzelm@41444
   771
    |> map (if direction then swap else I)
wenzelm@41444
   772
  end
kaliszyk@35222
   773
urbanc@37592
   774
fun mk_trm_subst qtys direction ctxt =
wenzelm@41444
   775
  let
wenzelm@41444
   776
    val subst_typ' = subst_typ ctxt (mk_ty_subst qtys direction ctxt)
wenzelm@41444
   777
    fun proper (t1, t2) = subst_typ' (fastype_of t1) = fastype_of t2
kaliszyk@37563
   778
wenzelm@41444
   779
    val const_substs =
wenzelm@45279
   780
      Quotient_Info.dest_quotconsts ctxt
wenzelm@41444
   781
      |> map (fn x => (#rconst x, #qconst x))
wenzelm@41444
   782
      |> map (if direction then swap else I)
urbanc@37560
   783
wenzelm@41444
   784
    val rel_substs =
wenzelm@45279
   785
      Quotient_Info.dest_quotients ctxt
wenzelm@41444
   786
      |> map (fn x => (#equiv_rel x, HOLogic.eq_const (#qtyp x)))
wenzelm@41444
   787
      |> map (if direction then swap else I)
wenzelm@41444
   788
  in
wenzelm@41444
   789
    filter proper (const_substs @ rel_substs)
wenzelm@41444
   790
  end
kaliszyk@35222
   791
urbanc@37592
   792
urbanc@37560
   793
(* derives a qtyp and qtrm out of a rtyp and rtrm,
wenzelm@41444
   794
   respectively
urbanc@37560
   795
*)
urbanc@38624
   796
fun derive_qtyp ctxt qtys rty =
urbanc@37592
   797
  subst_typ ctxt (mk_ty_subst qtys false ctxt) rty
urbanc@37592
   798
urbanc@38624
   799
fun derive_qtrm ctxt qtys rtrm =
urbanc@37592
   800
  subst_trm ctxt (mk_ty_subst qtys false ctxt) (mk_trm_subst qtys false ctxt) rtrm
kaliszyk@35222
   801
urbanc@37592
   802
(* derives a rtyp and rtrm out of a qtyp and qtrm,
wenzelm@41444
   803
   respectively
urbanc@37592
   804
*)
urbanc@38624
   805
fun derive_rtyp ctxt qtys qty =
urbanc@37592
   806
  subst_typ ctxt (mk_ty_subst qtys true ctxt) qty
urbanc@37592
   807
urbanc@38624
   808
fun derive_rtrm ctxt qtys qtrm =
urbanc@37592
   809
  subst_trm ctxt (mk_ty_subst qtys true ctxt) (mk_trm_subst qtys true ctxt) qtrm
urbanc@37560
   810
kaliszyk@35222
   811
wenzelm@45279
   812
end;