Skip to content

Instantly share code, notes, and snippets.

@relrod
Created December 23, 2016 23:11
Show Gist options
  • Save relrod/d2ed243c8471a90e7631fa4799826605 to your computer and use it in GitHub Desktop.
Save relrod/d2ed243c8471a90e7631fa4799826605 to your computer and use it in GitHub Desktop.
{-# LANGUAGE FlexibleContexts #-}
module SpecializedIO where
import Control.Monad.Free
data OnlyPrintLine a = OnlyPrintLine String a
instance Functor OnlyPrintLine where
fmap f (OnlyPrintLine s a) = OnlyPrintLine s (f a)
type OnlyPrintLineF = Free OnlyPrintLine
-- | OnlyPrintLineF a ~> IO a
interpret :: OnlyPrintLineF a -> IO a
interpret (Free (OnlyPrintLine s a)) = putStrLn s >> interpret a
interpret (Pure a) = return a
-- | Pretty interface
pp :: MonadFree OnlyPrintLine m => String -> m ()
pp s = liftF (OnlyPrintLine s ())
main :: OnlyPrintLineF ()
main = do
pp "hello world!"
pp "I can't do anything except println now!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment