Theory Derive_Encode
section "Encoding"
theory Derive_Encode
imports Main "../Derive" Derive_Datatypes
begin
class encodeable =
fixes encode :: "'a ⇒ bool list"
instantiation nat and unit:: encodeable
begin
fun encode_nat :: "nat ⇒ bool list" where
"encode_nat 0 = []" |
"encode_nat (Suc n) = True # (encode n)"
definition encode_unit: "encode (x::unit) = []"
instance ..
end
instantiation prod and sum :: (encodeable, encodeable) encodeable
begin
definition encode_prod_def: "encode x = append (encode (fst x)) (encode (snd x))"
definition encode_sum_def: "encode x = (case x of Inl a ⇒ False # encode a
| Inr a ⇒ True # encode a)"
instance ..
end
derive_generic encodeable simple .
derive_generic encodeable either .
lemma "encode (B 3 4) = [True, False, True, True, True, True, True, True, True]" by eval
lemma "encode C = [True, True]" by eval
lemma "encode (R (3::nat)) = [True, True, True, True]" by code_simp