Skip to content

Instantly share code, notes, and snippets.

@nbenns
Created April 26, 2018 16:08
Show Gist options
  • Save nbenns/9b47398a6669d60118c65e5a55031578 to your computer and use it in GitHub Desktop.
Save nbenns/9b47398a6669d60118c65e5a55031578 to your computer and use it in GitHub Desktop.
{-# LANGUAGE GADTs, DataKinds, TypeFamilies, FlexibleInstances #-}
-- from my stackoverflow question: https://stackoverflow.com/questions/40939508/translate-a-scala-type-example-to-haskell
data Status = Open | Closed deriving(Show)
data Door (status :: Status) = Door
--OpenDoor :: Door Open
--ClosedDoor :: Door Closed
instance Show (Door Open) where
show _ = "Door Open"
instance Show (Door Closed) where
show _ = "Door Closed"
open :: Door Closed -> Door Open
open _ = Door
close :: Door Open -> Door Closed
close _ = Door
type family Toggle (s :: Status) where
Toggle Open = Closed
Toggle Closed = Open
toggle :: Door s -> Door (Toggle s)
toggle _ = Door
--toggle OpenDoor = ClosedDoor
--toggle ClosedDoor = OpenDoor
main :: IO ()
main = print openedAgainDoor
where
closedDoor = Door :: Door Closed
openDoor = open closedDoor
closedAgainDoor = close openDoor
openedAgainDoor = toggle closedAgainDoor
-- closeClosedDoor = close closedDoor
-- openOpenedDoor = open openDoor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment