Skip to content

Commit

Permalink
Add support for infix applications
Browse files Browse the repository at this point in the history
This was released on hackage in version 0.1.0.3 by accident.

It's fairly simplistic; effects are sequenced operator-left-right,
somewhat arbitrarily.
  • Loading branch information
bmillwood committed Dec 12, 2011
1 parent 6092052 commit 5b8347a
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions Control/Applicative/QQ/Idiom.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

module Control.Applicative.QQ.Idiom (i) where

import Control.Applicative
import Control.Applicative ((<*>), pure)
import Control.Monad ((<=<))
import Language.Haskell.Meta (parseExp)
import Language.Haskell.TH.Lib
import Language.Haskell.TH.Quote
Expand All @@ -13,18 +14,12 @@ import Language.Haskell.TH.Syntax
-- ghci> [$i| (,) "foo" "bar" |]
-- [('f','b'),('f','a'),('f','r'),('o','b'),('o','a'),('o','r'),('o','b'),('o','a'),('o','r')]
i :: QuasiQuoter
i = QuasiQuoter { quoteExp = applicateQ }
i = QuasiQuoter { quoteExp = applicate <=< either fail return . parseExp }

applicateQ :: String -> ExpQ
applicateQ s = case either fail unwindE (parseExp s) of
x:y:xs -> foldl
(\e e' -> [|$e <*> $e'|])
[|$(return x) <$> $(return y)|]
(fmap return xs)
_ -> fail "applicateQ fails."

unwindE :: Exp -> [Exp]
unwindE = go []
where go acc (e `AppE` e') = go (e':acc) e
go acc e = e:acc
applicate :: Exp -> ExpQ
applicate (AppE f x) =
[| $(applicate f) <*> $(return x) |]
applicate (InfixE (Just left) op (Just right)) =
[| pure $(return op) <*> $(return left) <*> $(return right) |]
applicate x = [| pure $(return x) |]

0 comments on commit 5b8347a

Please sign in to comment.