Created
April 26, 2018 16:08
-
-
Save nbenns/9b47398a6669d60118c65e5a55031578 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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