Skip to content

Instantly share code, notes, and snippets.

@johannes-riecken
Last active January 20, 2026 11:01
Show Gist options
  • Select an option

  • Save johannes-riecken/f3d60c62f3484a5b524e97f0ea473d70 to your computer and use it in GitHub Desktop.

Select an option

Save johannes-riecken/f3d60c62f3484a5b524e97f0ea473d70 to your computer and use it in GitHub Desktop.
Totality checking failure for mutually recursive type class instances
%default total
data Tree a = Leaf a | Fork (List (Tree a))
interface Encode a where
encode : a -> List Bool
mutual
Encode a => Encode (List a) where
encode [] = [False]
encode (x :: xs) = True :: encode x ++ encode xs
Encode a => Encode (Tree a) where
encode (Leaf x) = [False]
encode (Fork []) = [True, False]
encode (Fork (x :: xs)) = True :: True :: encode x ++ encode xs
%default total
data Tree a = Leaf a | Fork (List (Tree a))
interface Encode a where
encode : a -> List Bool
mutual
encodeList : Encode a => List (Tree a) -> List Bool
encodeList [] = [False]
encodeList (x :: xs) = True :: encodeTree x ++ encodeList xs
encodeTree : Encode a => Tree a -> List Bool
encodeTree (Leaf x) = [False]
encodeTree (Fork []) = [True, False]
encodeTree (Fork (x :: xs)) = True :: True :: encodeTree x ++ encodeList xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment