Skip to content

Instantly share code, notes, and snippets.

@Magicloud
Created November 18, 2019 20:03
Show Gist options
  • Save Magicloud/e1673f9542138976bc3c1082c81e7fd9 to your computer and use it in GitHub Desktop.
Save Magicloud/e1673f9542138976bc3c1082c81e7fd9 to your computer and use it in GitHub Desktop.
Invertible syntax Read/Show.
-- Copied from https://www.howtobuildsoftware.com/index.php/how-do/fWl/haskell-template-haskell-deriving-custom-derivingreadshow-for-enum-type
import Prelude hiding (Applicative(..), print)
import Data.Maybe (fromJust)
import Text.Syntax
import Text.Syntax.Parser.Naive
import Text.Syntax.Printer.Naive
data TVShow = BobsBurgers | MrRobot | BatmanTAS deriving (Eq, Ord)
tvShow :: Syntax f => f TVShow
tvShow = pure BobsBurgers <* text "Bob's Burgers"
<|> pure MrRobot <* text "Mr. Robot"
<|> pure BatmanTAS <* text "Batman: The Animated Series"
runParser (Parser p) = p
instance Read TVShow where readsPrec _ = runParser tvShow
instance Show TVShow where show = fromJust . print tvShow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment