First (untested) version of regularize for abstractions.
theory QuotMain
imports QuotScript QuotList Prove
uses ("quotient.ML")
begin
locale QUOT_TYPE =
fixes R :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
and Abs :: "('a \<Rightarrow> bool) \<Rightarrow> 'b"
and Rep :: "'b \<Rightarrow> ('a \<Rightarrow> bool)"
assumes equiv: "EQUIV R"
and rep_prop: "\<And>y. \<exists>x. Rep y = R x"
and rep_inverse: "\<And>x. Abs (Rep x) = x"
and abs_inverse: "\<And>x. (Rep (Abs (R x))) = (R x)"
and rep_inject: "\<And>x y. (Rep x = Rep y) = (x = y)"
begin
definition
"ABS x \<equiv> Abs (R x)"
definition
"REP a = Eps (Rep a)"
lemma lem9:
shows "R (Eps (R x)) = R x"
proof -
have a: "R x x" using equiv by (simp add: EQUIV_REFL_SYM_TRANS REFL_def)
then have "R x (Eps (R x))" by (rule someI)
then show "R (Eps (R x)) = R x"
using equiv unfolding EQUIV_def by simp
qed
theorem thm10:
shows "ABS (REP a) \<equiv> a"
apply (rule eq_reflection)
unfolding ABS_def REP_def
proof -
from rep_prop
obtain x where eq: "Rep a = R x" by auto
have "Abs (R (Eps (Rep a))) = Abs (R (Eps (R x)))" using eq by simp
also have "\<dots> = Abs (R x)" using lem9 by simp
also have "\<dots> = Abs (Rep a)" using eq by simp
also have "\<dots> = a" using rep_inverse by simp
finally
show "Abs (R (Eps (Rep a))) = a" by simp
qed
lemma REP_refl:
shows "R (REP a) (REP a)"
unfolding REP_def
by (simp add: equiv[simplified EQUIV_def])
lemma lem7:
shows "(R x = R y) = (Abs (R x) = Abs (R y))"
apply(rule iffI)
apply(simp)
apply(drule rep_inject[THEN iffD2])
apply(simp add: abs_inverse)
done
theorem thm11:
shows "R r r' = (ABS r = ABS r')"
unfolding ABS_def
by (simp only: equiv[simplified EQUIV_def] lem7)
lemma REP_ABS_rsp:
shows "R f (REP (ABS g)) = R f g"
and "R (REP (ABS g)) f = R g f"
by (simp_all add: thm10 thm11)
lemma QUOTIENT:
"QUOTIENT R ABS REP"
apply(unfold QUOTIENT_def)
apply(simp add: thm10)
apply(simp add: REP_refl)
apply(subst thm11[symmetric])
apply(simp add: equiv[simplified EQUIV_def])
done
lemma R_trans:
assumes ab: "R a b"
and bc: "R b c"
shows "R a c"
proof -
have tr: "TRANS R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp
moreover have ab: "R a b" by fact
moreover have bc: "R b c" by fact
ultimately show "R a c" unfolding TRANS_def by blast
qed
lemma R_sym:
assumes ab: "R a b"
shows "R b a"
proof -
have re: "SYM R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp
then show "R b a" using ab unfolding SYM_def by blast
qed
lemma R_trans2:
assumes ac: "R a c"
and bd: "R b d"
shows "R a b = R c d"
proof
assume "R a b"
then have "R b a" using R_sym by blast
then have "R b c" using ac R_trans by blast
then have "R c b" using R_sym by blast
then show "R c d" using bd R_trans by blast
next
assume "R c d"
then have "R a d" using ac R_trans by blast
then have "R d a" using R_sym by blast
then have "R b a" using bd R_trans by blast
then show "R a b" using R_sym by blast
qed
lemma REPS_same:
shows "R (REP a) (REP b) \<equiv> (a = b)"
proof -
have "R (REP a) (REP b) = (a = b)"
proof
assume as: "R (REP a) (REP b)"
from rep_prop
obtain x y
where eqs: "Rep a = R x" "Rep b = R y" by blast
from eqs have "R (Eps (R x)) (Eps (R y))" using as unfolding REP_def by simp
then have "R x (Eps (R y))" using lem9 by simp
then have "R (Eps (R y)) x" using R_sym by blast
then have "R y x" using lem9 by simp
then have "R x y" using R_sym by blast
then have "ABS x = ABS y" using thm11 by simp
then have "Abs (Rep a) = Abs (Rep b)" using eqs unfolding ABS_def by simp
then show "a = b" using rep_inverse by simp
next
assume ab: "a = b"
have "REFL R" using equiv EQUIV_REFL_SYM_TRANS[of R] by simp
then show "R (REP a) (REP b)" unfolding REFL_def using ab by auto
qed
then show "R (REP a) (REP b) \<equiv> (a = b)" by simp
qed
end
section {* type definition for the quotient type *}
use "quotient.ML"
term EQUIV
ML {*
val no_vars = Thm.rule_attribute (fn context => fn th =>
let
val ctxt = Variable.set_body false (Context.proof_of context);
val ((_, [th']), _) = Variable.import true [th] ctxt;
in th' end);
*}
section {* various tests for quotient types*}
datatype trm =
var "nat"
| app "trm" "trm"
| lam "nat" "trm"
axiomatization
RR :: "trm \<Rightarrow> trm \<Rightarrow> bool"
where
r_eq: "EQUIV RR"
quotient qtrm = trm / "RR"
apply(rule r_eq)
done
typ qtrm
term Rep_qtrm
term REP_qtrm
term Abs_qtrm
term ABS_qtrm
thm QUOT_TYPE_qtrm
thm QUOTIENT_qtrm
thm REP_qtrm_def
(* Test interpretation *)
thm QUOT_TYPE_I_qtrm.thm11
thm QUOT_TYPE.thm11
print_theorems
thm Rep_qtrm
text {* another test *}
datatype 'a trm' =
var' "'a"
| app' "'a trm'" "'a trm'"
| lam' "'a" "'a trm'"
consts R' :: "'a trm' \<Rightarrow> 'a trm' \<Rightarrow> bool"
axioms r_eq': "EQUIV R'"
quotient qtrm' = "'a trm'" / "R'"
apply(rule r_eq')
done
print_theorems
term ABS_qtrm'
term REP_qtrm'
thm QUOT_TYPE_qtrm'
thm QUOTIENT_qtrm'
thm Rep_qtrm'
text {* a test with lists of terms *}
datatype t =
vr "string"
| ap "t list"
| lm "string" "t"
consts Rt :: "t \<Rightarrow> t \<Rightarrow> bool"
axioms t_eq: "EQUIV Rt"
quotient qt = "t" / "Rt"
by (rule t_eq)
section {* lifting of constants *}
text {* information about map-functions for type-constructor *}
ML {*
type typ_info = {mapfun: string, relfun: string}
local
structure Data = TheoryDataFun
(type T = typ_info Symtab.table
val empty = Symtab.empty
val copy = I
val extend = I
fun merge _ = Symtab.merge (K true))
in
val lookup = Symtab.lookup o Data.get
fun update k v = Data.map (Symtab.update (k, v))
end
*}
(* mapfuns for some standard types *)
setup {*
update @{type_name "list"} {mapfun = @{const_name "map"}, relfun = @{const_name "LIST_REL"}} #>
update @{type_name "*"} {mapfun = @{const_name "prod_fun"}, relfun = "???"} #>
update @{type_name "fun"} {mapfun = @{const_name "fun_map"}, relfun = @{const_name "FUN_REL"}}
*}
ML {* lookup @{theory} @{type_name list} *}
ML {*
datatype flag = absF | repF
fun get_fun flag rty qty lthy ty =
let
val qty_name = Long_Name.base_name (fst (dest_Type qty))
fun get_fun_aux s fs_tys =
let
val (fs, tys) = split_list fs_tys
val (otys, ntys) = split_list tys
val oty = Type (s, otys)
val nty = Type (s, ntys)
val ftys = map (op -->) tys
in
(case (lookup (ProofContext.theory_of lthy) s) of
SOME info => (list_comb (Const (#mapfun info, ftys ---> oty --> nty), fs), (oty, nty))
| NONE => raise ERROR ("no map association for type " ^ s))
end
fun get_const absF = (Const ("QuotMain.ABS_" ^ qty_name, rty --> qty), (rty, qty))
| get_const repF = (Const ("QuotMain.REP_" ^ qty_name, qty --> rty), (qty, rty))
fun mk_identity ty = Abs ("", ty, Bound 0)
in
if ty = qty
then (get_const flag)
else (case ty of
TFree _ => (mk_identity ty, (ty, ty))
| Type (_, []) => (mk_identity ty, (ty, ty))
| Type (s, tys) => get_fun_aux s (map (get_fun flag rty qty lthy) tys)
| _ => raise ERROR ("no type variables")
)
end
*}
ML {*
get_fun repF @{typ t} @{typ qt} @{context} @{typ "((t \<Rightarrow> t) list) * nat"}
|> fst
|> Syntax.string_of_term @{context}
|> writeln
*}
ML {*
get_fun absF @{typ t} @{typ qt} @{context} @{typ "t * nat"}
|> fst
|> Syntax.string_of_term @{context}
|> writeln
*}
text {* tyRel takes a type and builds a relation that a quantifier over this
type needs to respect. *}
ML {*
fun tyRel ty rty rel lthy =
if ty = rty then rel
else (case ty of
Type (s, tys) =>
let
val tys_rel = map (fn ty => ty --> ty --> @{typ bool}) tys;
val ty_out = ty --> ty --> @{typ bool};
val tys_out = tys_rel ---> ty_out;
in
(case (lookup (ProofContext.theory_of lthy) s) of
SOME (info) => list_comb (Const (#relfun info, tys_out), map (fn ty => tyRel ty rty rel lthy) tys)
| NONE => Const (@{const_name "op ="}, ty --> ty --> @{typ bool})
)
end
| _ => Const (@{const_name "op ="}, ty --> ty --> @{typ bool}))
*}
definition
"x IN p ==> (Babs p m x = m x)"
print_theorems
thm Babs_def
ML {* type_of @{term Babs } *}
ML {*
cterm_of @{theory} (tyRel @{typ "trm \<Rightarrow> bool"} @{typ "trm"} @{term "RR"} @{context})
*}
ML {* type_of @{term Respects } *}
ML {*
(* trm \<Rightarrow> new_trm *)
fun regularise trm rty rel lthy =
case trm of
Abs (x, T, t) =>
if T = rty then let
val ([x'], lthy2) = Variable.variant_fixes [x] lthy;
val v = Free (x', rty);
val t' = subst_bound (v, t);
val rec_term = regularise t' rty rel lthy2;
val lam_term = Term.lambda_name (x, v) rec_term;
val sub_res_term = tyRel T rty rel lthy;
val respects = Const (@{const_name Respects}, (fastype_of sub_res_term) --> T --> @{typ bool});
val res_term = respects $ sub_res_term;
val ty = fastype_of trm;
val rabs = Const (@{const_name Babs}, (fastype_of res_term) --> ty --> ty);
val rabs_term = rabs $ respects $ lam_term;
in
rabs_term
end else let
val ([x'], lthy2) = Variable.variant_fixes [x] lthy;
val v = Free (x', rty);
val t' = subst_bound (v, t);
val rec_term = regularise t' rty rel lthy2;
in
Term.lambda_name (x, v) rec_term
end
| _ => trm
*}
fun prove_reg trm \<Rightarrow> thm (we might need some facts to do this)
trm == new_trm
*)
text {* produces the definition for a lifted constant *}
ML {*
fun get_const_def nconst oconst rty qty lthy =
let
val ty = fastype_of nconst
val (arg_tys, res_ty) = strip_type ty
val fresh_args = arg_tys |> map (pair "x")
|> Variable.variant_frees lthy [nconst, oconst]
|> map Free
val rep_fns = map (fst o get_fun repF rty qty lthy) arg_tys
val abs_fn = (fst o get_fun absF rty qty lthy) res_ty
in
map (op $) (rep_fns ~~ fresh_args)
|> curry list_comb oconst
|> curry (op $) abs_fn
|> fold_rev lambda fresh_args
end
*}
ML {*
fun exchange_ty rty qty ty =
if ty = rty
then qty
else
(case ty of
Type (s, tys) => Type (s, map (exchange_ty rty qty) tys)
| _ => ty
)
*}
ML {*
fun make_const_def nconst_bname oconst mx rty qty lthy =
let
val oconst_ty = fastype_of oconst
val nconst_ty = exchange_ty rty qty oconst_ty
val nconst = Const (Binding.name_of nconst_bname, nconst_ty)
val def_trm = get_const_def nconst oconst rty qty lthy
in
define (nconst_bname, mx, def_trm) lthy
end
*}
local_setup {*
make_const_def @{binding VR} @{term "vr"} NoSyn @{typ "t"} @{typ "qt"} #> snd #>
make_const_def @{binding AP} @{term "ap"} NoSyn @{typ "t"} @{typ "qt"} #> snd #>
make_const_def @{binding LM} @{term "lm"} NoSyn @{typ "t"} @{typ "qt"} #> snd
*}
term vr
term ap
term lm
thm VR_def AP_def LM_def
term LM
term VR
term AP
text {* a test with functions *}
datatype 'a t' =
vr' "string"
| ap' "('a t') * ('a t')"
| lm' "'a" "string \<Rightarrow> ('a t')"
consts Rt' :: "('a t') \<Rightarrow> ('a t') \<Rightarrow> bool"
axioms t_eq': "EQUIV Rt'"
quotient qt' = "'a t'" / "Rt'"
apply(rule t_eq')
done
print_theorems
local_setup {*
make_const_def @{binding VR'} @{term "vr'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd #>
make_const_def @{binding AP'} @{term "ap'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd #>
make_const_def @{binding LM'} @{term "lm'"} NoSyn @{typ "'a t'"} @{typ "'a qt'"} #> snd
*}
term vr'
term ap'
term ap'
thm VR'_def AP'_def LM'_def
term LM'
term VR'
term AP'
text {* finite set example *}
print_syntax
inductive
list_eq (infix "\<approx>" 50)
where
"a#b#xs \<approx> b#a#xs"
| "[] \<approx> []"
| "xs \<approx> ys \<Longrightarrow> ys \<approx> xs"
| "a#a#xs \<approx> a#xs"
| "xs \<approx> ys \<Longrightarrow> a#xs \<approx> a#ys"
| "\<lbrakk>xs1 \<approx> xs2; xs2 \<approx> xs3\<rbrakk> \<Longrightarrow> xs1 \<approx> xs3"
lemma list_eq_refl:
shows "xs \<approx> xs"
apply (induct xs)
apply (auto intro: list_eq.intros)
done
lemma equiv_list_eq:
shows "EQUIV list_eq"
unfolding EQUIV_REFL_SYM_TRANS REFL_def SYM_def TRANS_def
apply(auto intro: list_eq.intros list_eq_refl)
done
quotient fset = "'a list" / "list_eq"
apply(rule equiv_list_eq)
done
print_theorems
typ "'a fset"
thm "Rep_fset"
local_setup {*
make_const_def @{binding EMPTY} @{term "[]"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
*}
term Nil
term EMPTY
thm EMPTY_def
local_setup {*
make_const_def @{binding INSERT} @{term "op #"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
*}
term Cons
term INSERT
thm INSERT_def
local_setup {*
make_const_def @{binding UNION} @{term "op @"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
*}
term append
term UNION
thm UNION_def
thm QUOTIENT_fset
thm QUOT_TYPE_I_fset.thm11
fun
membship :: "'a \<Rightarrow> 'a list \<Rightarrow> bool" (infix "memb" 100)
where
m1: "(x memb []) = False"
| m2: "(x memb (y#xs)) = ((x=y) \<or> (x memb xs))"
lemma mem_respects:
fixes z
assumes a: "list_eq x y"
shows "(z memb x) = (z memb y)"
using a by induct auto
fun
card1 :: "'a list \<Rightarrow> nat"
where
card1_nil: "(card1 []) = 0"
| card1_cons: "(card1 (x # xs)) = (if (x memb xs) then (card1 xs) else (Suc (card1 xs)))"
local_setup {*
make_const_def @{binding card} @{term "card1"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
*}
term card1
term card
thm card_def
(* text {*
Maybe make_const_def should require a theorem that says that the particular lifted function
respects the relation. With it such a definition would be impossible:
make_const_def @{binding CARD} @{term "length"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
*}*)
lemma card1_rsp:
fixes a b :: "'a list"
assumes e: "a \<approx> b"
shows "card1 a = card1 b"
using e apply induct
apply (simp_all add:mem_respects)
done
lemma card1_0:
fixes a :: "'a list"
shows "(card1 a = 0) = (a = [])"
apply (induct a)
apply (simp)
apply (simp_all)
apply meson
apply (simp_all)
done
lemma not_mem_card1:
fixes x :: "'a"
fixes xs :: "'a list"
shows "~(x memb xs) \<Longrightarrow> card1 (x # xs) = Suc (card1 xs)"
by simp
lemma mem_cons:
fixes x :: "'a"
fixes xs :: "'a list"
assumes a : "x memb xs"
shows "x # xs \<approx> xs"
using a
apply (induct xs)
apply (auto intro: list_eq.intros)
done
lemma card1_suc:
fixes xs :: "'a list"
fixes n :: "nat"
assumes c: "card1 xs = Suc n"
shows "\<exists>a ys. ~(a memb ys) \<and> xs \<approx> (a # ys)"
using c
apply(induct xs)
apply (metis Suc_neq_Zero card1_0)
apply (metis QUOT_TYPE_I_fset.R_trans QuotMain.card1_cons list_eq_refl mem_cons)
done
lemma cons_preserves:
fixes z
assumes a: "xs \<approx> ys"
shows "(z # xs) \<approx> (z # ys)"
using a by (rule QuotMain.list_eq.intros(5))
lemma fs1_strong_cases:
fixes X :: "'a list"
shows "(X = []) \<or> (\<exists>a. \<exists> Y. (~(a memb Y) \<and> (X \<approx> a # Y)))"
apply (induct X)
apply (simp)
apply (metis QUOT_TYPE_I_fset.thm11 list_eq_refl mem_cons QuotMain.m1)
done
text {*
Unabs_def converts a definition given as
c \<equiv> %x. %y. f x y
to a theorem of the form
c x y \<equiv> f x y
This function is needed to rewrite the right-hand
side to the left-hand side.
*}
ML {*
fun unabs_def ctxt def =
let
val (lhs, rhs) = Thm.dest_equals (cprop_of def)
val xs = strip_abs_vars (term_of rhs)
val (_, ctxt') = Variable.add_fixes (map fst xs) ctxt
val thy = ProofContext.theory_of ctxt'
val cxs = map (cterm_of thy o Free) xs
val new_lhs = Drule.list_comb (lhs, cxs)
fun get_conv [] = Conv.rewr_conv def
| get_conv (x::xs) = Conv.fun_conv (get_conv xs)
in
get_conv xs new_lhs |>
singleton (ProofContext.export ctxt' ctxt)
end
*}
local_setup {*
make_const_def @{binding IN} @{term "membship"} NoSyn @{typ "'a list"} @{typ "'a fset"} #> snd
*}
term membship
term IN
thm IN_def
(* unabs_def tests *)
ML {* (Conv.fun_conv (Conv.fun_conv (Conv.rewr_conv @{thm IN_def}))) @{cterm "IN x y"} *}
ML {* MetaSimplifier.rewrite_rule @{thms IN_def} @{thm IN_def}*}
ML {* @{thm IN_def}; unabs_def @{context} @{thm IN_def} *}
lemmas a = QUOT_TYPE.ABS_def[OF QUOT_TYPE_fset]
thm QUOT_TYPE.thm11[OF QUOT_TYPE_fset, THEN iffD1, simplified a]
lemma yy:
shows "(False = x memb []) = (False = IN (x::nat) EMPTY)"
unfolding IN_def EMPTY_def
apply(rule_tac f="(op =) False" in arg_cong)
apply(rule mem_respects)
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
apply(rule list_eq.intros)
done
lemma
shows "IN (x::nat) EMPTY = False"
using m1
apply -
apply(rule yy[THEN iffD1, symmetric])
apply(simp)
done
lemma
shows "((x=y) \<or> (IN x xs) = (IN (x::nat) (INSERT y xs))) =
((x=y) \<or> x memb REP_fset xs = x memb (y # REP_fset xs))"
unfolding IN_def INSERT_def
apply(rule_tac f="(op \<or>) (x=y)" in arg_cong)
apply(rule_tac f="(op =) (x memb REP_fset xs)" in arg_cong)
apply(rule mem_respects)
apply(rule list_eq.intros(3))
apply(unfold REP_fset_def ABS_fset_def)
apply(simp only: QUOT_TYPE.REP_ABS_rsp[OF QUOT_TYPE_fset])
apply(rule list_eq_refl)
done
lemma append_respects_fst:
assumes a : "list_eq l1 l2"
shows "list_eq (l1 @ s) (l2 @ s)"
using a
apply(induct)
apply(auto intro: list_eq.intros)
apply(simp add: list_eq_refl)
done
lemma yyy:
shows "
(
(UNION EMPTY s = s) &
((UNION (INSERT e s1) s2) = (INSERT e (UNION s1 s2)))
) = (
((ABS_fset ([] @ REP_fset s)) = s) &
((ABS_fset ((e # (REP_fset s1)) @ REP_fset s2)) = ABS_fset (e # (REP_fset s1 @ REP_fset s2)))
)"
unfolding UNION_def EMPTY_def INSERT_def
apply(rule_tac f="(op &)" in arg_cong2)
apply(rule_tac f="(op =)" in arg_cong2)
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
apply(rule append_respects_fst)
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
apply(rule list_eq_refl)
apply(simp)
apply(rule_tac f="(op =)" in arg_cong2)
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
apply(rule append_respects_fst)
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
apply(rule list_eq_refl)
apply(simp only: QUOT_TYPE_I_fset.thm11[symmetric])
apply(rule list_eq.intros(5))
apply(simp only: QUOT_TYPE_I_fset.REP_ABS_rsp)
apply(rule list_eq_refl)
done
lemma
shows "
(UNION EMPTY s = s) &
((UNION (INSERT e s1) s2) = (INSERT e (UNION s1 s2)))"
apply (simp add: yyy)
apply (simp add: QUOT_TYPE_I_fset.thm10)
done
ML {*
fun mk_rep x = @{term REP_fset} $ x;
fun mk_abs x = @{term ABS_fset} $ x;
val consts = [@{const_name "Nil"}, @{const_name "append"},
@{const_name "Cons"}, @{const_name "membship"},
@{const_name "card1"}]
*}
ML {* val qty = @{typ "'a fset"} *}
ML {* val tt = Type ("fun", [Type ("fun", [qty, @{typ "prop"}]), @{typ "prop"}]) *}
ML {* val fall = Const(@{const_name all}, dummyT) *}
ML {*
fun build_goal_term ctxt thm constructors rty qty mk_rep mk_abs =
let
fun mk_rep_abs x = mk_rep (mk_abs x);
fun is_constructor (Const (x, _)) = member (op =) constructors x
| is_constructor _ = false;
fun maybe_mk_rep_abs t =
let
val _ = writeln ("Maybe: " ^ Syntax.string_of_term ctxt t)
in
if fastype_of t = rty then mk_rep_abs t else t
end;
fun is_all (Const ("all", Type("fun", [Type("fun", [ty, _]), _]))) = ty = rty
| is_all _ = false;
fun is_ex (Const ("Ex", Type("fun", [Type("fun", [ty, _]), _]))) = ty = rty
| is_ex _ = false;
fun build_aux ctxt1 tm =
let
val (head, args) = Term.strip_comb tm;
val args' = map (build_aux ctxt1) args;
in
(case head of
Abs (x, T, t) =>
if T = rty then let
val ([x'], ctxt2) = Variable.variant_fixes [x] ctxt1;
val v = Free (x', qty);
val t' = subst_bound (mk_rep v, t);
val rec_term = build_aux ctxt2 t';
val _ = tracing (Syntax.string_of_term ctxt2 t')
val _ = tracing (Syntax.string_of_term ctxt2 (Term.lambda_name (x, v) rec_term))
in
Term.lambda_name (x, v) rec_term
end else let
val ([x'], ctxt2) = Variable.variant_fixes [x] ctxt1;
val v = Free (x', T);
val t' = subst_bound (v, t);
val rec_term = build_aux ctxt2 t';
in Term.lambda_name (x, v) rec_term end
| _ => (* I assume that we never lift 'prop' since it is not of sort type *)
if is_all head then
list_comb (Const(@{const_name all}, dummyT), args') |> Syntax.check_term ctxt1
else if is_ex head then
list_comb (Const(@{const_name Ex}, dummyT), args') |> Syntax.check_term ctxt1
else if is_constructor head then
maybe_mk_rep_abs (list_comb (head, map maybe_mk_rep_abs args'))
else
maybe_mk_rep_abs (list_comb (head, args'))
)
end;
in
build_aux ctxt (Thm.prop_of thm)
end
*}
ML {*
fun build_goal ctxt thm cons rty qty mk_rep mk_abs =
Logic.mk_equals ((build_goal_term ctxt thm cons rty qty mk_rep mk_abs), (Thm.prop_of thm))
*}
ML {*
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm m1}))
*}
ML {*
m1_novars |> prop_of
|> Syntax.string_of_term @{context}
|> writeln;
build_goal_term @{context} m1_novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs
|> Syntax.string_of_term @{context}
|> writeln
*}
ML {* val fset_defs = @{thms EMPTY_def IN_def UNION_def card_def INSERT_def} *}
ML {* val fset_defs_sym = map (fn t => symmetric (unabs_def @{context} t)) fset_defs *}
ML {*
fun dest_cbinop t =
let
val (t2, rhs) = Thm.dest_comb t;
val (bop, lhs) = Thm.dest_comb t2;
in
(bop, (lhs, rhs))
end
*}
ML {*
fun dest_ceq t =
let
val (bop, pair) = dest_cbinop t;
val (bop_s, _) = Term.dest_Const (Thm.term_of bop);
in
if bop_s = "op =" then pair else (raise CTERM ("Not an equality", [t]))
end
*}
ML Thm.instantiate
ML {*@{thm arg_cong2}*}
ML {*@{thm arg_cong2[of _ _ _ _ "op ="]} *}
ML {* val cT = @{cpat "op ="} |> Thm.ctyp_of_term |> Thm.dest_ctyp |> hd *}
ML {*
Toplevel.program (fn () =>
Drule.instantiate' [SOME cT, SOME cT, SOME @{ctyp bool}] [NONE, NONE, NONE, NONE, SOME (@{cpat "op ="})] @{thm arg_cong2}
)
*}
ML {*
fun split_binop_conv t =
let
val (lhs, rhs) = dest_ceq t;
val (bop, _) = dest_cbinop lhs;
val [clT, cr2] = bop |> Thm.ctyp_of_term |> Thm.dest_ctyp;
val [cmT, crT] = Thm.dest_ctyp cr2;
in
Drule.instantiate' [SOME clT, SOME cmT, SOME crT] [NONE, NONE, NONE, NONE, SOME bop] @{thm arg_cong2}
end
*}
ML {*
fun split_arg_conv t =
let
val (lhs, rhs) = dest_ceq t;
val (lop, larg) = Thm.dest_comb lhs;
val [caT, crT] = lop |> Thm.ctyp_of_term |> Thm.dest_ctyp;
in
Drule.instantiate' [SOME caT, SOME crT] [NONE, NONE, SOME lop] @{thm arg_cong}
end
*}
ML {*
fun split_binop_tac n thm =
let
val concl = Thm.cprem_of thm n;
val (_, cconcl) = Thm.dest_comb concl;
val rewr = split_binop_conv cconcl;
in
rtac rewr n thm
end
handle CTERM _ => Seq.empty
*}
ML {*
fun split_arg_tac n thm =
let
val concl = Thm.cprem_of thm n;
val (_, cconcl) = Thm.dest_comb concl;
val rewr = split_arg_conv cconcl;
in
rtac rewr n thm
end
handle CTERM _ => Seq.empty
*}
(* Has all the theorems about fset plugged in. These should be parameters to the tactic *)
lemma trueprop_cong:
shows "(a \<equiv> b) \<Longrightarrow> (Trueprop a \<equiv> Trueprop b)"
by auto
ML {*
Cong_Tac.cong_tac
*}
thm QUOT_TYPE_I_fset.R_trans2
ML {*
fun foo_tac' ctxt =
REPEAT_ALL_NEW (FIRST' [
(* rtac @{thm trueprop_cong},*)
rtac @{thm list_eq_refl},
rtac @{thm cons_preserves},
rtac @{thm mem_respects},
rtac @{thm card1_rsp},
rtac @{thm QUOT_TYPE_I_fset.R_trans2},
CHANGED o (simp_tac ((Simplifier.context ctxt HOL_ss) addsimps @{thms QUOT_TYPE_I_fset.REP_ABS_rsp})),
Cong_Tac.cong_tac @{thm cong},
rtac @{thm ext}
(* rtac @{thm eq_reflection},*)
(* CHANGED o (ObjectLogic.full_atomize_tac)*)
])
*}
ML {*
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm m1}))
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs
val cgoal = cterm_of @{theory} goal
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false fset_defs_sym cgoal)
*}
(*notation ( output) "prop" ("#_" [1000] 1000) *)
notation ( output) "Trueprop" ("#_" [1000] 1000)
lemma atomize_eqv[atomize]:
shows "(Trueprop A \<equiv> Trueprop B) \<equiv> (A \<equiv> B)"
proof
assume "A \<equiv> B"
then show "Trueprop A \<equiv> Trueprop B" by unfold
next
assume *: "Trueprop A \<equiv> Trueprop B"
have "A = B"
proof (cases A)
case True
have "A" by fact
then show "A = B" using * by simp
next
case False
have "\<not>A" by fact
then show "A = B" using * by auto
qed
then show "A \<equiv> B" by (rule eq_reflection)
qed
prove {* (Thm.term_of cgoal2) *}
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
apply (atomize(full))
apply (tactic {* foo_tac' @{context} 1 *})
done
thm length_append (* Not true but worth checking that the goal is correct *)
ML {*
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm length_append}))
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs
val cgoal = cterm_of @{theory} goal
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false fset_defs_sym cgoal)
*}
prove {* Thm.term_of cgoal2 *}
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
apply (atomize(full))
apply (tactic {* foo_tac' @{context} 1 *})
sorry
thm m2
ML {*
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm m2}))
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs
val cgoal = cterm_of @{theory} goal
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false fset_defs_sym cgoal)
*}
prove {* Thm.term_of cgoal2 *}
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
apply (atomize(full))
apply (tactic {* foo_tac' @{context} 1 *})
done
thm list_eq.intros(4)
ML {*
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm list_eq.intros(4)}))
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs
val cgoal = cterm_of @{theory} goal
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite false fset_defs_sym cgoal)
val cgoal3 = Thm.rhs_of (MetaSimplifier.rewrite true @{thms QUOT_TYPE_I_fset.thm10} cgoal2)
*}
(* It is the same, but we need a name for it. *)
prove zzz : {* Thm.term_of cgoal3 *}
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
apply (atomize(full))
apply (tactic {* foo_tac' @{context} 1 *})
done
lemma zzz' :
"(REP_fset (INSERT a (INSERT a (ABS_fset xs))) \<approx> REP_fset (INSERT a (ABS_fset xs)))"
using list_eq.intros(4) by (simp only: zzz)
thm QUOT_TYPE_I_fset.REPS_same
ML {* val zzz'' = MetaSimplifier.rewrite_rule @{thms QUOT_TYPE_I_fset.REPS_same} @{thm zzz'} *}
thm list_eq.intros(5)
ML {*
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm list_eq.intros(5)}))
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs
*}
ML {*
val cgoal =
Toplevel.program (fn () =>
cterm_of @{theory} goal
)
*}
ML {*
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true fset_defs_sym cgoal)
*}
prove {* Thm.term_of cgoal2 *}
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
apply (atomize(full))
apply (tactic {* foo_tac' @{context} 1 *})
done
thm list.induct
ML {*
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm list.induct}))
*}
ML {*
val goal =
Toplevel.program (fn () =>
build_goal @{context} m1_novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs
)
*}
ML {*
val cgoal =
Toplevel.program (fn () =>
cterm_of @{theory} goal
)
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true fset_defs_sym cgoal)
*}
prove {* (Thm.term_of cgoal2) *}
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
apply (atomize(full))
apply (tactic {* foo_tac' @{context} 1 *})
sorry
ML {*
fun lift_theorem_fset_aux thm lthy =
let
val ((_, [novars]), lthy2) = Variable.import true [thm] lthy;
val goal = build_goal @{context} novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs;
val cgoal = cterm_of @{theory} goal;
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true fset_defs_sym cgoal);
val tac = (LocalDefs.unfold_tac @{context} fset_defs) THEN (ObjectLogic.full_atomize_tac 1) THEN (foo_tac' @{context}) 1;
val cthm = Goal.prove_internal [] cgoal2 (fn _ => tac);
val nthm = MetaSimplifier.rewrite_rule [symmetric cthm] (snd (no_vars (Context.Theory @{theory}, thm)))
val nthm2 = MetaSimplifier.rewrite_rule @{thms QUOT_TYPE_I_fset.REPS_same QUOT_TYPE_I_fset.thm10} nthm;
val [nthm3] = ProofContext.export lthy2 lthy [nthm2]
in
nthm3
end
*}
ML {* lift_theorem_fset_aux @{thm m1} @{context} *}
ML {*
fun lift_theorem_fset name thm lthy =
let
val lifted_thm = lift_theorem_fset_aux thm lthy;
val (_, lthy2) = note_thm (name, lifted_thm) lthy;
in
lthy2
end;
*}
local_setup {* lift_theorem_fset @{binding "m1_lift"} @{thm m1} *}
local_setup {* lift_theorem_fset @{binding "leqi4_lift"} @{thm list_eq.intros(4)} *}
local_setup {* lift_theorem_fset @{binding "leqi5_lift"} @{thm list_eq.intros(5)} *}
local_setup {* lift_theorem_fset @{binding "m2_lift"} @{thm m2} *}
(*ML {* Toplevel.program (fn () => lift_theorem_fset @{binding "card_suc"} @{thm card1_suc} @{context}) *}
local_setup {* lift_theorem_fset @{binding "card_suc"} @{thm card1_suc} *}*)
thm m1_lift
thm leqi4_lift
thm leqi5_lift
thm m2_lift
(*thm card_suc*)
thm leqi4_lift
ML {*
val (nam, typ) = hd (Term.add_vars (prop_of @{thm leqi4_lift}) [])
val (_, l) = dest_Type typ
val t = Type ("QuotMain.fset", l)
val v = Var (nam, t)
val cv = cterm_of @{theory} ((term_of @{cpat "REP_fset"}) $ v)
*}
ML {*
Toplevel.program (fn () =>
MetaSimplifier.rewrite_rule @{thms QUOT_TYPE_I_fset.thm10} (
Drule.instantiate' [] [NONE, SOME (cv)] @{thm leqi4_lift}
)
)
*}
(*
thm card_suc
ML {*
val (nam, typ) = hd (tl (Term.add_vars (prop_of @{thm card_suc})) [])
val (_, l) = dest_Type typ
val t = Type ("QuotMain.fset", l)
val v = Var (nam, t)
val cv = cterm_of @{theory} ((term_of @{cpat "REP_fset"}) $ v)
*}
ML {*
Toplevel.program (fn () =>
MetaSimplifier.rewrite_rule @{thms QUOT_TYPE_I_fset.thm10} (
Drule.instantiate' [] [SOME (cv)] @{thm card_suc}
)
)
*}
*)
thm card1_suc
ML {*
val m1_novars = snd(no_vars ((Context.Theory @{theory}), @{thm card1_suc}))
*}
ML {*
val goal = build_goal @{context} m1_novars consts @{typ "'a list"} @{typ "'a fset"} mk_rep mk_abs
*}
ML {* term_of @{cpat "all"} *}
ML {*
val cgoal =
Toplevel.program (fn () =>
cterm_of @{theory} goal
);
val cgoal2 = Thm.rhs_of (MetaSimplifier.rewrite true fset_defs_sym cgoal)
*}
lemma "(Ball (Respects ((op \<approx>) ===> (op =)))
(((REP_fset ---> id) ---> id)
(((ABS_fset ---> id) ---> id)
(\<lambda>P.
(ABS_fset ---> id) ((REP_fset ---> id) P)
(REP_fset (ABS_fset [])) \<and>
Ball (Respects (op \<approx>))
((ABS_fset ---> id)
((REP_fset ---> id)
(\<lambda>t.
((ABS_fset ---> id)
((REP_fset ---> id) P)
(REP_fset (ABS_fset t))) \<longrightarrow>
(!h.
(ABS_fset ---> id)
((REP_fset ---> id) P)
(REP_fset
(ABS_fset
(h #
REP_fset
(ABS_fset t)))))))) \<longrightarrow>
Ball (Respects (op \<approx>))
((ABS_fset ---> id)
((REP_fset ---> id)
(\<lambda>l.
(ABS_fset ---> id)
((REP_fset ---> id) P)
(REP_fset (ABS_fset l)))))))))
= Ball (Respects ((op \<approx>) ===> (op =)))
(\<lambda>P. ((P []) \<and> (Ball (Respects (op \<approx>)) (\<lambda>t. P t \<longrightarrow> (\<forall>h. (P (h # t)))))) \<longrightarrow>
(Ball (Respects (op \<approx>)) (\<lambda>l. P l)))"
term "(\<forall>P. (((P []) \<and> (\<forall>t. (P t \<longrightarrow> (\<forall>h. P (h # t))))) \<longrightarrow> (\<forall>l. (P l))))"
thm LAMBDA_PRS1[symmetric]
(*apply (simp only:LAMBDA_PRS1[symmetric] FUN_QUOTIENT IDENTITY_QUOTIENT QUOT_TYPE_I_fset.QUOTIENT)*)
apply (unfold Ball_def)
apply (simp only: IN_RESPECTS)
apply (simp only:list_eq_refl)
apply (unfold id_def)
(*apply (simp only: FUN_MAP_I)*)
apply (simp)
apply (simp only: QUOT_TYPE_I_fset.thm10)
apply (tactic {* foo_tac' @{context} 1 *})
..
apply (simp add:IN_RESPECTS)
apply (simp add: QUOT_TYPE_I_fset.R_trans2)
apply (rule ext)
apply (simp add:QUOT_TYPE_I_fset.REP_ABS_rsp)
apply (tactic {* Cong_Tac.cong_tac @{thm cong} 1 *} )
apply (simp add:cons_preserves)
(*prove aaa: {* (Thm.term_of cgoal2) *}
apply (tactic {* LocalDefs.unfold_tac @{context} fset_defs *} )
apply (atomize(full))
apply (tactic {* foo_tac' @{context} 1 *})
done*)
(*
datatype obj1 =
OVAR1 "string"
| OBJ1 "(string * (string \<Rightarrow> obj1)) list"
| INVOKE1 "obj1 \<Rightarrow> string"
| UPDATE1 "obj1 \<Rightarrow> string \<Rightarrow> (string \<Rightarrow> obj1)"
*)
end