Skip to content

Instantly share code, notes, and snippets.

@pete-murphy
Created December 23, 2019 23:39
Show Gist options
  • Save pete-murphy/e6104ca6f38290b5253b1bac849892ce to your computer and use it in GitHub Desktop.
Save pete-murphy/e6104ca6f38290b5253b1bac849892ce to your computer and use it in GitHub Desktop.
{- SEVEN -}
-- | Here are two data types that may help:
data Empty
data Branch left centre right
-- | a. Using these, and the outline for 'HList' above, build a heterogeneous
-- /tree/. None of the variables should be existential.
data HTree a where
HEmpty :: HTree Empty
HBranch :: HTree l -> a -> HTree r -> HTree (Branch l a r)
-- ???
-- | b. Implement a function that deletes the left subtree. The type should be
-- strong enough that GHC will do most of the work for you. Once you have it,
-- try breaking the implementation - does it type-check? If not, why not?
deleteLeftSubtree :: HTree (Branch l a r) -> HTree (Branch Empty a r)
deleteLeftSubtree (HBranch _ a r) = HBranch HEmpty a r
-- | c. Implement 'Eq' for 'HTree's. Note that you might have to write more
-- than one to cover all possible HTrees. You might also need an extension or
-- two, so look out for something... flexible... in the error messages!
-- Recursion is your friend here - you shouldn't need to add a constraint to
-- the GADT!
instance Eq a => Eq (HTree a) where
HEmpty == HEmpty = True
_ == HEmpty = False
HEmpty == _ = False
HBranch l a r == HBranch l' a' r' = a == a' && l == l' && r == r'
-- ^^^^^^^ <-- This says "Could not deduce (Eq a1) arising from a use of ‘==’"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment