|
47308
|
1 |
(* Title: HOL/Tools/Lifting/lifting_term.ML
|
|
|
2 |
Author: Ondrej Kuncar
|
|
|
3 |
|
|
|
4 |
Proves Quotient theorem.
|
|
|
5 |
*)
|
|
|
6 |
|
|
|
7 |
signature LIFTING_TERM =
|
|
|
8 |
sig
|
|
|
9 |
val prove_quot_theorem: Proof.context -> typ * typ -> thm
|
|
|
10 |
|
|
|
11 |
val absrep_fun: Proof.context -> typ * typ -> term
|
|
|
12 |
|
|
|
13 |
(* Allows Nitpick to represent quotient types as single elements from raw type *)
|
|
|
14 |
(* val absrep_const_chk: Proof.context -> flag -> string -> term *)
|
|
|
15 |
|
|
|
16 |
val equiv_relation: Proof.context -> typ * typ -> term
|
|
|
17 |
|
|
|
18 |
val quot_thm_rel: thm -> term
|
|
|
19 |
val quot_thm_abs: thm -> term
|
|
|
20 |
val quot_thm_rep: thm -> term
|
|
|
21 |
val quot_thm_rty_qty: thm -> typ * typ
|
|
|
22 |
end
|
|
|
23 |
|
|
|
24 |
structure Lifting_Term: LIFTING_TERM =
|
|
|
25 |
struct
|
|
|
26 |
|
|
|
27 |
exception LIFT_MATCH of string
|
|
|
28 |
|
|
|
29 |
(* matches a type pattern with a type *)
|
|
|
30 |
fun match ctxt err ty_pat ty =
|
|
|
31 |
let
|
|
|
32 |
val thy = Proof_Context.theory_of ctxt
|
|
|
33 |
in
|
|
|
34 |
Sign.typ_match thy (ty_pat, ty) Vartab.empty
|
|
|
35 |
handle Type.TYPE_MATCH => err ctxt ty_pat ty
|
|
|
36 |
end
|
|
|
37 |
|
|
|
38 |
fun equiv_match_err ctxt ty_pat ty =
|
|
|
39 |
let
|
|
|
40 |
val ty_pat_str = Syntax.string_of_typ ctxt ty_pat
|
|
|
41 |
val ty_str = Syntax.string_of_typ ctxt ty
|
|
|
42 |
in
|
|
|
43 |
raise LIFT_MATCH (space_implode " "
|
|
|
44 |
["equiv_relation (Types ", quote ty_pat_str, "and", quote ty_str, " do not match.)"])
|
|
|
45 |
end
|
|
|
46 |
|
|
|
47 |
(* generation of the Quotient theorem *)
|
|
|
48 |
|
|
|
49 |
exception QUOT_THM of string
|
|
|
50 |
|
|
|
51 |
fun get_quot_thm ctxt s =
|
|
|
52 |
let
|
|
|
53 |
val thy = Proof_Context.theory_of ctxt
|
|
|
54 |
in
|
|
|
55 |
(case Lifting_Info.lookup_quotients ctxt s of
|
|
|
56 |
SOME qdata => Thm.transfer thy (#quot_thm qdata)
|
|
|
57 |
| NONE => raise QUOT_THM ("No quotient type " ^ quote s ^ " found."))
|
|
|
58 |
end
|
|
|
59 |
|
|
|
60 |
fun get_rel_quot_thm ctxt s =
|
|
|
61 |
let
|
|
|
62 |
val thy = Proof_Context.theory_of ctxt
|
|
|
63 |
in
|
|
|
64 |
(case Lifting_Info.lookup_quotmaps ctxt s of
|
|
|
65 |
SOME map_data => Thm.transfer thy (#quot_thm map_data)
|
|
|
66 |
| NONE => raise QUOT_THM ("get_relmap (no relation map function found for type " ^ s ^ ")"))
|
|
|
67 |
end
|
|
|
68 |
|
|
|
69 |
fun is_id_quot thm = (prop_of thm = prop_of @{thm identity_quotient})
|
|
|
70 |
|
|
|
71 |
infix 0 MRSL
|
|
|
72 |
|
|
|
73 |
fun ants MRSL thm = fold (fn rl => fn thm => rl RS thm) ants thm
|
|
|
74 |
|
|
|
75 |
exception NOT_IMPL of string
|
|
|
76 |
|
|
|
77 |
fun quot_thm_rel quot_thm =
|
|
|
78 |
let
|
|
|
79 |
val (Const (@{const_name Quotient}, _) $ rel $ _ $ _ $ _) =
|
|
|
80 |
(HOLogic.dest_Trueprop o prop_of) quot_thm
|
|
|
81 |
in
|
|
|
82 |
rel
|
|
|
83 |
end
|
|
|
84 |
|
|
|
85 |
fun quot_thm_abs quot_thm =
|
|
|
86 |
let
|
|
|
87 |
val (Const (@{const_name Quotient}, _) $ _ $ abs $ _ $ _) =
|
|
|
88 |
(HOLogic.dest_Trueprop o prop_of) quot_thm
|
|
|
89 |
in
|
|
|
90 |
abs
|
|
|
91 |
end
|
|
|
92 |
|
|
|
93 |
fun quot_thm_rep quot_thm =
|
|
|
94 |
let
|
|
|
95 |
val (Const (@{const_name Quotient}, _) $ _ $ _ $ rep $ _) =
|
|
|
96 |
(HOLogic.dest_Trueprop o prop_of) quot_thm
|
|
|
97 |
in
|
|
|
98 |
rep
|
|
|
99 |
end
|
|
|
100 |
|
|
|
101 |
fun quot_thm_rty_qty quot_thm =
|
|
|
102 |
let
|
|
|
103 |
val abs = quot_thm_abs quot_thm
|
|
|
104 |
val abs_type = fastype_of abs
|
|
|
105 |
in
|
|
|
106 |
(domain_type abs_type, range_type abs_type)
|
|
|
107 |
end
|
|
|
108 |
|
|
|
109 |
fun prove_quot_theorem ctxt (rty, qty) =
|
|
|
110 |
case (rty, qty) of
|
|
|
111 |
(Type (s, tys), Type (s', tys')) =>
|
|
|
112 |
if s = s'
|
|
|
113 |
then
|
|
|
114 |
let
|
|
|
115 |
val args = map (prove_quot_theorem ctxt) (tys ~~ tys')
|
|
|
116 |
in
|
|
|
117 |
if forall is_id_quot args
|
|
|
118 |
then
|
|
|
119 |
@{thm identity_quotient}
|
|
|
120 |
else
|
|
|
121 |
args MRSL (get_rel_quot_thm ctxt s)
|
|
|
122 |
end
|
|
|
123 |
else
|
|
|
124 |
let
|
|
|
125 |
val quot_thm = get_quot_thm ctxt s'
|
|
|
126 |
val (Type (_, rtys), qty_pat) = quot_thm_rty_qty quot_thm
|
|
|
127 |
val qtyenv = match ctxt equiv_match_err qty_pat qty
|
|
|
128 |
val rtys' = map (Envir.subst_type qtyenv) rtys
|
|
|
129 |
val args = map (prove_quot_theorem ctxt) (tys ~~ rtys')
|
|
|
130 |
in
|
|
|
131 |
if forall is_id_quot args
|
|
|
132 |
then
|
|
|
133 |
quot_thm
|
|
|
134 |
else
|
|
|
135 |
let
|
|
|
136 |
val rel_quot_thm = args MRSL (get_rel_quot_thm ctxt s)
|
|
|
137 |
in
|
|
|
138 |
[rel_quot_thm, quot_thm] MRSL @{thm Quotient_compose}
|
|
|
139 |
end
|
|
|
140 |
end
|
|
|
141 |
| _ => @{thm identity_quotient}
|
|
|
142 |
|
|
|
143 |
fun force_qty_type thy qty quot_thm =
|
|
|
144 |
let
|
|
|
145 |
val abs_schematic = quot_thm_abs quot_thm
|
|
|
146 |
val qty_schematic = (range_type o fastype_of) abs_schematic
|
|
|
147 |
val match_env = Sign.typ_match thy (qty_schematic, qty) Vartab.empty
|
|
|
148 |
fun prep_ty thy (x, (S, ty)) =
|
|
|
149 |
(ctyp_of thy (TVar (x, S)), ctyp_of thy ty)
|
|
|
150 |
val ty_inst = Vartab.fold (cons o (prep_ty thy)) match_env []
|
|
|
151 |
in
|
|
|
152 |
Thm.instantiate (ty_inst, []) quot_thm
|
|
|
153 |
end
|
|
|
154 |
|
|
|
155 |
fun absrep_fun ctxt (rty, qty) =
|
|
|
156 |
let
|
|
|
157 |
val thy = Proof_Context.theory_of ctxt
|
|
|
158 |
val quot_thm = prove_quot_theorem ctxt (rty, qty)
|
|
|
159 |
val forced_quot_thm = force_qty_type thy qty quot_thm
|
|
|
160 |
in
|
|
|
161 |
quot_thm_abs forced_quot_thm
|
|
|
162 |
end
|
|
|
163 |
|
|
|
164 |
fun equiv_relation ctxt (rty, qty) =
|
|
|
165 |
let
|
|
|
166 |
val thy = Proof_Context.theory_of ctxt
|
|
|
167 |
val quot_thm = prove_quot_theorem ctxt (rty, qty)
|
|
|
168 |
val forced_quot_thm = force_qty_type thy qty quot_thm
|
|
|
169 |
in
|
|
|
170 |
quot_thm_rel forced_quot_thm
|
|
|
171 |
end
|
|
|
172 |
|
|
|
173 |
end;
|