src/HOL/Tools/Lifting/lifting_term.ML
author wenzelm
Sun, 20 May 2012 11:34:33 +0200
changeset 47884 21c42b095c84
parent 47853 c01ba36769f5
child 47951 8c8a03765de7
permissions -rw-r--r--
try to avoid races again (cf. 8c37cb84065f and fd3a36e48b09);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     1
(*  Title:      HOL/Tools/Lifting/lifting_term.ML
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     2
    Author:     Ondrej Kuncar
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     3
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     4
Proves Quotient theorem.
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     5
*)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     6
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     7
signature LIFTING_TERM =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
     8
sig
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
     9
  exception QUOT_THM of typ * typ * Pretty.T
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    10
  exception CHECK_RTY of typ * typ
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    11
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    12
  val prove_quot_thm: Proof.context -> typ * typ -> thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    13
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
    14
  val abs_fun: Proof.context -> typ * typ -> term
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    15
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    16
  val equiv_relation: Proof.context -> typ * typ -> term
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    17
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    18
  val quot_thm_rel: thm -> term
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    19
  val quot_thm_abs: thm -> term
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    20
  val quot_thm_rep: thm -> term
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    21
  val quot_thm_rty_qty: thm -> typ * typ
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    22
end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    23
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    24
structure Lifting_Term: LIFTING_TERM =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    25
struct
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    26
47698
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47504
diff changeset
    27
open Lifting_Util
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47504
diff changeset
    28
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47504
diff changeset
    29
infix 0 MRSL
18202d3d5832 move MRSL to a separate file
kuncar
parents: 47504
diff changeset
    30
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    31
exception QUOT_THM_INTERNAL of Pretty.T
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    32
exception QUOT_THM of typ * typ * Pretty.T
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    33
exception CHECK_RTY of typ * typ
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    34
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    35
fun match ctxt err ty_pat ty =
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    36
  let
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    37
    val thy = Proof_Context.theory_of ctxt
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    38
  in
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    39
    Sign.typ_match thy (ty_pat, ty) Vartab.empty
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    40
      handle Type.TYPE_MATCH => err ctxt ty_pat ty
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    41
  end
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    42
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    43
fun equiv_match_err ctxt ty_pat ty =
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    44
  let
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    45
    val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    46
    val ty_str = Syntax.string_of_typ ctxt ty
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    47
  in
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    48
    raise QUOT_THM_INTERNAL (Pretty.block
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    49
      [Pretty.str ("The quotient type " ^ quote ty_str),
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    50
       Pretty.brk 1,
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    51
       Pretty.str ("and the quotient type pattern " ^ quote ty_pat_str),
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    52
       Pretty.brk 1,
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    53
       Pretty.str "don't match."])
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    54
  end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    55
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    56
fun get_quot_thm ctxt s =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    57
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    58
    val thy = Proof_Context.theory_of ctxt
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    59
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    60
    (case Lifting_Info.lookup_quotients ctxt s of
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    61
      SOME qdata => Thm.transfer thy (#quot_thm qdata)
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    62
    | NONE => raise QUOT_THM_INTERNAL (Pretty.block 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    63
      [Pretty.str ("No quotient type " ^ quote s), 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    64
       Pretty.brk 1, 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    65
       Pretty.str "found."]))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    66
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    67
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    68
fun get_rel_quot_thm ctxt s =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    69
   let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    70
    val thy = Proof_Context.theory_of ctxt
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    71
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    72
    (case Lifting_Info.lookup_quotmaps ctxt s of
47777
f29e7dcd7c40 use a quot_map theorem attribute instead of the complicated map attribute
kuncar
parents: 47698
diff changeset
    73
      SOME map_data => Thm.transfer thy (#rel_quot_thm map_data)
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    74
    | NONE => raise QUOT_THM_INTERNAL (Pretty.block 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    75
      [Pretty.str ("No relator for the type " ^ quote s), 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    76
       Pretty.brk 1,
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
    77
       Pretty.str "found."]))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    78
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    79
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    80
fun is_id_quot thm = (prop_of thm = prop_of @{thm identity_quotient})
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
    81
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
    82
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
    83
  quot_thm_rel, quot_thm_abs, quot_thm_rep and quot_thm_rty_qty - simple functions
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
    84
    for destructing quotient theorems (Quotient R Abs Rep T).
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
    85
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
    86
47384
9f38eff9c45f add function dest_Quotient
huffman
parents: 47379
diff changeset
    87
fun quot_thm_rel quot_thm =
9f38eff9c45f add function dest_Quotient
huffman
parents: 47379
diff changeset
    88
  case (dest_Quotient o HOLogic.dest_Trueprop o prop_of) quot_thm of
9f38eff9c45f add function dest_Quotient
huffman
parents: 47379
diff changeset
    89
    (rel, _, _, _) => rel
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    90
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    91
fun quot_thm_abs quot_thm =
47384
9f38eff9c45f add function dest_Quotient
huffman
parents: 47379
diff changeset
    92
  case (dest_Quotient o HOLogic.dest_Trueprop o prop_of) quot_thm of
9f38eff9c45f add function dest_Quotient
huffman
parents: 47379
diff changeset
    93
    (_, abs, _, _) => abs
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    94
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    95
fun quot_thm_rep quot_thm =
47384
9f38eff9c45f add function dest_Quotient
huffman
parents: 47379
diff changeset
    96
  case (dest_Quotient o HOLogic.dest_Trueprop o prop_of) quot_thm of
9f38eff9c45f add function dest_Quotient
huffman
parents: 47379
diff changeset
    97
    (_, _, rep, _) => rep
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    98
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
    99
fun quot_thm_rty_qty quot_thm =
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   100
  let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   101
    val abs = quot_thm_abs quot_thm
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   102
    val abs_type = fastype_of abs  
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   103
  in
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   104
    (domain_type abs_type, range_type abs_type)
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   105
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   106
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   107
fun check_raw_types (provided_rty_name, rty_of_qty_name) qty_name =
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   108
  if provided_rty_name <> rty_of_qty_name then
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   109
    raise QUOT_THM_INTERNAL (Pretty.block 
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   110
        [Pretty.str ("The type " ^ quote provided_rty_name),
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   111
         Pretty.brk 1,
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   112
         Pretty.str ("is not a raw type for the quotient type " ^ quote qty_name ^ ";"),
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   113
         Pretty.brk 1,
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   114
         Pretty.str ("the correct raw type is " ^ quote rty_of_qty_name ^ ".")])
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   115
  else
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   116
    ()
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   117
47778
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   118
fun zip_Tvars ctxt type_name rty_Tvars qty_Tvars =
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   119
  case try (get_rel_quot_thm ctxt) type_name of
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   120
    NONE => rty_Tvars ~~ qty_Tvars
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   121
    | SOME rel_quot_thm =>
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   122
      let 
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   123
        fun quot_term_absT quot_term = 
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   124
          let 
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   125
            val (_, abs, _, _) = (dest_Quotient o HOLogic.dest_Trueprop) quot_term
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   126
          in
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   127
            fastype_of abs
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   128
          end
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   129
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   130
        fun equiv_univ_err ctxt ty_pat ty =
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   131
          let
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   132
            val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   133
            val ty_str = Syntax.string_of_typ ctxt ty
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   134
          in
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   135
            raise QUOT_THM_INTERNAL (Pretty.block
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   136
              [Pretty.str ("The type " ^ quote ty_str),
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   137
               Pretty.brk 1,
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   138
               Pretty.str ("and the relator type pattern " ^ quote ty_pat_str),
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   139
               Pretty.brk 1,
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   140
               Pretty.str "don't unify."])
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   141
          end
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   142
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   143
        fun raw_match (TVar (v, S), T) subs =
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   144
              (case Vartab.defined subs v of
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   145
                false => Vartab.update_new (v, (S, T)) subs
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   146
              | true => subs)
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   147
          | raw_match (Type (_, Ts), Type (_, Us)) subs =
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   148
              raw_matches (Ts, Us) subs
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   149
          | raw_match _ subs = subs
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   150
        and raw_matches (T :: Ts, U :: Us) subs = raw_matches (Ts, Us) (raw_match (T, U) subs)
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   151
          | raw_matches _ subs = subs
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   152
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   153
        val rty = Type (type_name, rty_Tvars)
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   154
        val qty = Type (type_name, qty_Tvars)
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   155
        val rel_quot_thm_concl = (Logic.strip_imp_concl o prop_of) rel_quot_thm
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   156
        val schematic_rel_absT = quot_term_absT rel_quot_thm_concl;
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   157
        val ctxt' = Variable.declare_typ schematic_rel_absT ctxt
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   158
        val thy = Proof_Context.theory_of ctxt'
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   159
        val absT = rty --> qty
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   160
        val schematic_absT = Logic.type_map (singleton (Variable.polymorphic ctxt')) absT
47853
c01ba36769f5 resolved maxidx bug
kuncar
parents: 47852
diff changeset
   161
        val maxidx = Term.maxidx_of_typs [schematic_rel_absT, schematic_absT]
c01ba36769f5 resolved maxidx bug
kuncar
parents: 47852
diff changeset
   162
        val _ = Sign.typ_unify thy (schematic_rel_absT, schematic_absT) (Vartab.empty,maxidx)
47778
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   163
          handle Type.TUNIFY => equiv_univ_err ctxt schematic_rel_absT schematic_absT
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   164
        val subs = raw_match (schematic_rel_absT, absT) Vartab.empty
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   165
        val rel_quot_thm_prems = (Logic.strip_imp_prems o prop_of) rel_quot_thm
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   166
      in
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   167
        map (dest_funT o 
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   168
             Envir.subst_type subs o
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   169
             quot_term_absT) 
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   170
          rel_quot_thm_prems
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   171
      end
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   172
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   173
fun prove_schematic_quot_thm ctxt (rty, qty) =
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   174
  (case (rty, qty) of
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   175
    (Type (s, tys), Type (s', tys')) =>
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   176
      if s = s'
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   177
      then
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   178
        let
47778
7bcdaa255a00 support Quotient map theorems with invariant parameters
kuncar
parents: 47777
diff changeset
   179
          val args = map (prove_schematic_quot_thm ctxt) (zip_Tvars ctxt s tys tys')
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   180
        in
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   181
          if forall is_id_quot args
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   182
          then
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   183
            @{thm identity_quotient}
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   184
          else
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   185
            args MRSL (get_rel_quot_thm ctxt s)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   186
        end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   187
      else
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   188
        let
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   189
          val quot_thm = get_quot_thm ctxt s'
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   190
          val (Type (rs, rtys), qty_pat) = quot_thm_rty_qty quot_thm
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   191
          val _ = check_raw_types (s, rs) s'
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   192
          val qtyenv = match ctxt equiv_match_err qty_pat qty
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   193
          val rtys' = map (Envir.subst_type qtyenv) rtys
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   194
          val args = map (prove_schematic_quot_thm ctxt) (tys ~~ rtys')
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   195
        in
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   196
          if forall is_id_quot args
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   197
          then
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   198
            quot_thm
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   199
          else
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   200
            let
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   201
              val rel_quot_thm = args MRSL (get_rel_quot_thm ctxt s)
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   202
            in
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   203
              [rel_quot_thm, quot_thm] MRSL @{thm Quotient_compose}
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   204
           end
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   205
        end
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   206
    | (_, Type (s', tys')) => 
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   207
      (case try (get_quot_thm ctxt) s' of
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   208
        SOME quot_thm => 
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   209
          let
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   210
            val rty_pat = (fst o quot_thm_rty_qty) quot_thm
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   211
          in
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   212
            prove_schematic_quot_thm ctxt (rty_pat, qty)
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   213
          end
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   214
        | NONE =>
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   215
          let
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   216
            val rty_pat = Type (s', map (fn _ => TFree ("a",[])) tys')
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   217
          in
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   218
            prove_schematic_quot_thm ctxt (rty_pat, qty)
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   219
          end)
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   220
    | _ => @{thm identity_quotient})
47379
075d22b3a32f detect incorrect situations; better error messages; sanity check for quot_thm in setup_lifting_infr
kuncar
parents: 47350
diff changeset
   221
    handle QUOT_THM_INTERNAL pretty_msg => raise QUOT_THM (rty, qty, pretty_msg)
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   222
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   223
fun force_qty_type thy qty quot_thm =
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   224
  let
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   225
    val (_, qty_schematic) = quot_thm_rty_qty quot_thm
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   226
    val match_env = Sign.typ_match thy (qty_schematic, qty) Vartab.empty
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   227
    fun prep_ty thy (x, (S, ty)) =
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   228
      (ctyp_of thy (TVar (x, S)), ctyp_of thy ty)
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   229
    val ty_inst = Vartab.fold (cons o (prep_ty thy)) match_env []
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   230
  in
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   231
    Thm.instantiate (ty_inst, []) quot_thm
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   232
  end
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   233
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   234
fun check_rty_type ctxt rty quot_thm =
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   235
  let  
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   236
    val thy = Proof_Context.theory_of ctxt
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   237
    val (rty_forced, _) = quot_thm_rty_qty quot_thm
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   238
    val rty_schematic = Logic.type_map (singleton (Variable.polymorphic ctxt)) rty
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   239
    val _ = Sign.typ_match thy (rty_schematic, rty_forced) Vartab.empty
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   240
      handle Type.TYPE_MATCH => raise CHECK_RTY (rty_schematic, rty_forced)
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   241
  in
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   242
    ()
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   243
  end
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   244
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
   245
(*
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
   246
  The function tries to prove that rty and qty form a quotient.
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
   247
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
   248
  Returns: Quotient theorem; an abstract type of the theorem is exactly
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
   249
    qty, a representation type of the theorem is an instance of rty in general.
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
   250
*)
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
   251
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   252
fun prove_quot_thm ctxt (rty, qty) =
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   253
  let
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   254
    val thy = Proof_Context.theory_of ctxt
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   255
    val schematic_quot_thm = prove_schematic_quot_thm ctxt (rty, qty)
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   256
    val quot_thm = force_qty_type thy qty schematic_quot_thm
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   257
    val _ = check_rty_type ctxt rty quot_thm
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   258
  in
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   259
    quot_thm
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   260
  end
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   261
47852
0c3b8d036a5c documentation of the Lifting package on the ML level & tuned
kuncar
parents: 47778
diff changeset
   262
fun abs_fun ctxt (rty, qty) =
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   263
  quot_thm_abs (prove_quot_thm ctxt (rty, qty))
47350
ec46b60aa582 prove_quot_theorem fixes types
huffman
parents: 47308
diff changeset
   264
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   265
fun equiv_relation ctxt (rty, qty) =
47504
aa1b8a59017f go back to the explicit compisition of quotient theorems
kuncar
parents: 47386
diff changeset
   266
  quot_thm_rel (prove_quot_thm ctxt (rty, qty))
47308
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   267
9caab698dbe4 new package Lifting - initial commit
kuncar
parents:
diff changeset
   268
end;