Theory Lazy_List_Monad

section ‹Lazy list monad›

theory Lazy_List_Monad
imports Monad_Zero_Plus
begin

text ‹To illustrate the general process of defining a new type
constructor, we formalize the datatype of lazy lists. Below are the
Haskell datatype definition and class instances.›

text_raw ‹
\begin{verbatim}
data List a = Nil | Cons a (List a)

instance Functor List where
  fmap f Nil = Nil
  fmap f (Cons x xs) = Cons (f x) (fmap f xs)

instance Monad List where
  return x        = Cons x Nil
  Nil       >>= k = Nil
  Cons x xs >>= k = mplus (k x) (xs >>= k)

instance MonadZero List where
  mzero = Nil

instance MonadPlus List where
  mplus Nil         ys = ys
  mplus (Cons x xs) ys = Cons x (mplus xs ys)
\end{verbatim}
›

subsection ‹Type definition›

text ‹The first step is to register the datatype definition with
tycondef›.›

tycondef 'allist = LNil | LCons (lazy "'a") (lazy "'allist")

text ‹The tycondef› command generates lots of theorems
automatically, but there are a few more involving coerce› and
fmapU› that we still need to prove manually. These proofs could
be automated in a later version of tycondef›.›

lemma coerce_llist_abs [simp]: "coerce(llist_absx) = llist_abs(coercex)"
apply (simp add: llist_abs_def coerce_def)
apply (simp add: emb_prj_emb prj_emb_prj DEFL_eq_llist)
done

lemma coerce_LNil [simp]: "coerceLNil = LNil"
unfolding LNil_def by simp

lemma coerce_LCons [simp]: "coerce(LConsxxs) = LCons(coercex)(coercexs)"
unfolding LCons_def by simp

lemma fmapU_llist_simps [simp]:
  "fmapUf(::udomllist) = "
  "fmapUfLNil = LNil"
  "fmapUf(LConsxxs) = LCons(fx)(fmapUfxs)"
unfolding fmapU_llist_def llist_map_def
apply (subst fix_eq, simp)
apply (subst fix_eq, simp add: LNil_def)
apply (subst fix_eq, simp add: LCons_def)
done

subsection ‹Class instances›

text ‹The tycondef› command defines fmapU› for us and
proves a prefunctor› class instance automatically. For the
functor› instance we only need to prove the composition law,
which we can do by induction.›

instance llist :: "functor"
proof
  fix f g and xs :: "udomllist"
  show "fmapUf(fmapUgxs) = fmapU(Λ x. f(gx))xs"
    by (induct xs rule: llist.induct) simp_all
qed

text ‹For the other class instances, we need to provide definitions
for a few constants: returnU›, bindU› zeroU›, and
plusU›. We can use ordinary commands like definition›
and fixrec› for this purpose. Finally we prove the class
axioms, along with a few helper lemmas, using ordinary proof
procedures like induction.›

instantiation llist :: monad_zero_plus
begin

fixrec plusU_llist :: "udomllist  udomllist  udomllist"
  where "plusU_llistLNilys = ys"
  | "plusU_llist(LConsxxs)ys = LConsx(plusU_llistxsys)"

lemma plusU_llist_strict [simp]: "plusUys = (::udomllist)"
by fixrec_simp

fixrec bindU_llist :: "udomllist  (udom  udomllist)  udomllist"
  where "bindU_llistLNilk = LNil"
  | "bindU_llist(LConsxxs)k = plusU(kx)(bindU_llistxsk)"

lemma bindU_llist_strict [simp]: "bindUk = (::udomllist)"
by fixrec_simp

definition zeroU_llist_def:
  "zeroU = LNil"

definition returnU_llist_def:
  "returnU = (Λ x. LConsxLNil)"

lemma plusU_LNil_right: "plusUxsLNil = xs"
by (induct xs rule: llist.induct) simp_all

lemma plusU_llist_assoc:
  fixes xs ys zs :: "udomllist"
  shows "plusU(plusUxsys)zs = plusUxs(plusUyszs)"
by (induct xs rule: llist.induct) simp_all

lemma bindU_plusU_llist:
  fixes xs ys :: "udomllist" shows
  "bindU(plusUxsys)f = plusU(bindUxsf)(bindUysf)"
by (induct xs rule: llist.induct) (simp_all add: plusU_llist_assoc)

instance proof
  fix x :: "udom"
  fix f :: "udom  udom"
  fix h k :: "udom  udomllist"
  fix xs ys zs :: "udomllist"
  show "fmapUfxs = bindUxs(Λ x. returnU(fx))"
    by (induct xs rule: llist.induct, simp_all add: returnU_llist_def)
  show "bindU(returnUx)k = kx"
    by (simp add: returnU_llist_def plusU_LNil_right)
  show "bindU(bindUxsh)k = bindUxs(Λ x. bindU(hx)k)"
    by (induct xs rule: llist.induct)
       (simp_all add: bindU_plusU_llist)
  show "bindU(plusUxsys)k = plusU(bindUxsk)(bindUysk)"
    by (induct xs rule: llist.induct)
       (simp_all add: plusU_llist_assoc)
  show "plusU(plusUxsys)zs = plusUxs(plusUyszs)"
    by (rule plusU_llist_assoc)
  show "bindUzeroUk = zeroU"
    by (simp add: zeroU_llist_def)
  show "fmapUf(plusUxsys) = plusU(fmapUfxs)(fmapUfys)"
    by (induct xs rule: llist.induct) simp_all
  show "fmapUfzeroU = (zeroU :: udomllist)"
    by (simp add: zeroU_llist_def)
  show "plusUzeroUxs = xs"
    by (simp add: zeroU_llist_def)
  show "plusUxszeroU = xs"
    by (simp add: zeroU_llist_def plusU_LNil_right)
qed

end

subsection ‹Transfer properties to polymorphic versions›

text ‹After proving the class instances, there is still one more
step: We must transfer all the list-specific lemmas about the
monomorphic constants (e.g., fmapU› and bindU›) to the
corresponding polymorphic constants (fmap› and bind›).
These lemmas primarily consist of the defining equations for each
constant. The polymorphic constants are defined using coerce›,
so the proofs proceed by unfolding the definitions and simplifying
with the coerce_simp› rules.›

lemma fmap_llist_simps [simp]:
  "fmapf(::'allist) = "
  "fmapfLNil = LNil"
  "fmapf(LConsxxs) = LCons(fx)(fmapfxs)"
unfolding fmap_def by simp_all

lemma mplus_llist_simps [simp]:
  "mplus(::'allist)ys = "
  "mplusLNilys = ys"
  "mplus(LConsxxs)ys = LConsx(mplusxsys)"
unfolding mplus_def by simp_all

lemma bind_llist_simps [simp]:
  "bind(::'allist)f = "
  "bindLNilf = LNil"
  "bind(LConsxxs)f = mplus(fx)(bindxsf)"
unfolding bind_def mplus_def
by (simp_all add: coerce_simp)

lemma return_llist_def:
  "return = (Λ x. LConsxLNil)"
unfolding return_def returnU_llist_def
by (simp add: coerce_simp)

lemma mzero_llist_def:
  "mzero = LNil"
unfolding mzero_def zeroU_llist_def
by simp

lemma join_llist_simps [simp]:
  "join(::'allistllist) = "
  "joinLNil = LNil"
  "join(LConsxsxss) = mplusxs(joinxss)"
unfolding join_def by simp_all

end