Skip to content

Instantly share code, notes, and snippets.

@xenophobia
Created February 25, 2013 15:05
Show Gist options
  • Select an option

  • Save xenophobia/5030402 to your computer and use it in GitHub Desktop.

Select an option

Save xenophobia/5030402 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverlappingInstances #-}
import Control.Comonad
import Language.Haskell.Codo
type Option = String
data Config = MakeConfig [Option] deriving (Show)
type Builder = (->) [Option]
type Setter a = Builder a -> a
instance Comonad Builder where
extract bld = bld []
extend setter builder = \opts2 -> setter (\opts1 -> builder (opts1 ++ opts2))
defaultConfig :: Builder Config
defaultConfig opts = MakeConfig (["-Wall"] ++ opts)
profile :: Setter Config
profile bld = bld ["-prof", "-auto-all"]
goFaster :: Setter Config
goFaster bld = bld ["-O2"]
(#) :: a -> (a -> b) -> b
x # f = f x
config :: Config
config = defaultConfig #
[codo| config0 => config1 <- profile config0
config2 <- goFaster config1
extract config2 |]
-- >>> config
-- MakeConfig ["-Wall","-prof, -auto-all","-O2"]
{-
Quotation from "http://www.haskellforall.com/2013/02/you-could-have-invented-comonads.html"
config :: Config
config = defaultConfig # method
this # profile -- no apostrophes, these are setters
this # goFaster
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment