Patsy
Patsy is a library that provides a set of quasiquoters for declaring/using
constructor/pattern synonyms in Haskell. You'll need GHC 7 or later to use
it.
Code
You can find the latest version here.Examples
You could use Patsy to define your own lists:
{-# LANGUAGE QuasiQuotes #-}
module PatsyLists where
import Language.Haskell.Patsy
data List a
= Nil
| Cons a (List a)
deriving (Eq, Ord, Show)
[c| [] = Nil |]
[c| x : xs = Cons x xs |]
myLength :: List a -> Int
myLength [c| [] |] = 0
myLength [c| x : xs |] = 1 + myLength xs
instance Functor List where
fmap f [c| [] |] = [c| [] |]
fmap f [c| x : xs |] = [c| f x : fmap f xs |]
picky :: Num a => List a -> Int
picky [c| [1, 2] |] = 42
picky _ = 0
Notes and known issues
- Patsy cheekily uses haskell-src-meta, so you should be able to get away with infix synonyms, type operators, the lot.
-
List literals (such as
[1,2,3]) are expanded using whatever definitions you've provided for(:)and[]. If either is missing, the literal will be left alone (i.e. produce a normal list). -
The implementation is horrible. Seriously horrible. Try loading
up the example and typing
:browseif you don't believe me. Blame it on some of Template Haskell's limitations. - Due to the horrible implementation mentioned above, you have to be careful with export lists. Specifically, if you want a set of synonyms to work across multiple modules (which have been precompiled in particular), you should put them in a module on their own that has no export list.