Theory Tree_Relabelling
subsection ‹Tree relabelling›
theory Tree_Relabelling imports
Applicative_State
Applicative_Option
Applicative_PMF
"HOL-Library.Stream"
begin
unbundle applicative_syntax
adhoc_overloading Applicative.pure pure_option
adhoc_overloading Applicative.pure State_Monad.return
adhoc_overloading Applicative.ap State_Monad.ap
text ‹ Hutton and Fulger \<^cite>‹"HuttonFulger2008TFP"› suggested the following tree relabelling problem
as an example for reasoning about effects. Given a binary tree with labels at the leaves, the
relabelling assigns a unique number to every leaf. Their correctness property states that the
list of labels in the obtained tree is distinct. As observed by Gibbons and Bird \<^cite>‹"backwards"›,
this breaks the abstraction of the state monad, because the relabeling function must be run.
Although Hutton and Fulger are careful to reason in point-free style, they nevertheless unfold
the implementation of the state monad operations. Gibbons and Hinze \<^cite>‹"GibbonsHinze2011ICFP"›
suggest to state the correctness in an effectful way using an exception-state monad. Thereby, they
lose the applicative structure and have to resort to a full monad.
Here, we model the tree relabelling function three times. First, we state correctness in pure
terms following Hutton and Fulger. Second, we take Gibbons' and Bird's approach of considering
traversals. Third, we state correctness effectfully, but only using the applicative functors.
›