Skip to content

Instantly share code, notes, and snippets.

@hatashiro
Created November 9, 2016 01:23
Show Gist options
  • Select an option

  • Save hatashiro/2c3fd6a9f73e93c87fba85af3a1383e4 to your computer and use it in GitHub Desktop.

Select an option

Save hatashiro/2c3fd6a9f73e93c87fba85af3a1383e4 to your computer and use it in GitHub Desktop.
Sum of records bottom in Haskell

Let me guess there is a data PL as a sum type of records:

data PL = Functional { name :: String, pure :: Bool }
        | ObjectOriented { name :: String, multipleInheritance :: Bool }

The type of pure is:

λ> :type pure
pure :: PL -> Bool

You may have already noticed the problem.

λ> let haskell = Functional "Haskell" True
λ> pure haskell
True
λ> let cpp = ObjectOriented "CPP" True
λ> pure cpp
*** Exception: No match in record selector pure

The problem is that it's a runtime error. Because pure has the type PL -> Bool, there is no way to enforce pure to be applied only to Functionals in compile time.

A possible solution will be like below.

data Fuctional = Functional { name :: String, pure :: Bool }
data ObjectOriented = ObjectOriented { name :: String, multipleInheritance :: Bool }

data PL = Functional' Functional
        | ObjectOriented' ObjectOriented

Then the type of pure will be Fuctional -> Bool.

@gclaramunt
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment