% Extensible lists % [Public domain] \input birdstyle \birdleftrule=1pt \emergencystretch=1em \def\hugebreak{\penalty-600\vskip 30pt plus 8pt minus 4pt\relax} \newcount\chapno \def\: #1.{\advance\chapno by 1\relax\hugebreak{\bf\S\the\chapno. #1. }} \: Introduction. This module implements extensible lists. The values in the list can be extensible even in other modules. NB: In order to use this module, you need to enable the {\tt ScopedTypeVariables} extension, because it generates patterns with type signatures.
> {-# LANGUAGE MultiParamTypeClasses, TemplateHaskell #-}
> {-# LANGUAGE FunctionalDependencies #-}
> module Data.Extensible.List (
>   ExtList(..), extList
> ) where {
> import Control.Applicative;
> import Data.Extensible.TH__;
> import Data.List;
The following is the TH splicer function; it is given a name of a type which is the {\tt v} parameter of the class above, and produces an expression which evaluates into the list of all values of all instances that have that {\tt v} which are in scope (example: {\tt\$(extList ''List1)}). There is no guarantee to the ordering of the values, except that values in a single instance will be in the same order relative to each other and contiguous. > extList :: Name -> Q Exp; > extList x = liftM2 (\n i -> extListInst n (ConT x) i) > (newName "x") (thClassInstances ''ExtList); > extListInst :: Name -> Type -> [[Type]] -> Exp; > extListInst _ _ [] = ListE []; > extListInst n x ([v, p] : t) = bool id > (InfixE (Just e) (VarE '(++)) . Just) (v == x) (extListInst n x t) > where { > e :: Exp; > e = AppE (LamE [ConP '(,) [SigP WildP p, VarP n]]$ VarE n)