FW: [Template-haskell] Records and generating splices
Rene de Visser
rene_de_visser at hotmail.com
Mon Jul 11 12:39:20 EDT 2005
>From: "Rene de Visser" <rene_de_visser at hotmail.com>
>test = [| add_rel1 value = modify (\db -> db{ rel1 = Set.insert value (rel1
>db)}) |]
>{-# OPTIONS -fglasgow-exts #-}
>module DataBase where
>
>import Control.Monad.State
>import qualified Data.Set as Set
>-- Does this really need to be extensible???
>-- Entries need to be based on sets, or something similar...
>data SmallDB = SmallDB { rel1 :: Set.Set String
> , rel2 :: Set.Set Integer } deriving Show
>
>emptyDB = SmallDB Set.empty Set.empty
>
>add_rel1 :: (MonadState SmallDB m) => String -> m ()
>add_rel1 value = modify (\db -> db{ rel1 = Set.insert value (rel1 db)})
>
>add_rel2 :: (MonadState SmallDB m) => Integer -> m ()
>add_rel2 value = modify (\db -> db{ rel2 = Set.insert value (rel2 db)})
>
So I've made a tiny bit of progress.
I have:
test9 = [d| add_rel1 value = $(exp) value |] where
exp = [| (\value -> modify (\db -> $(updater) db (Set.insert value
(rel1 db)))) |] where
updater = [|(\db value -> db{rel1 = value}) |]
--updater = LamE [VarP db_2,VarP value_3] (RecUpdE (VarE db_2)
[(DataBase.rel1,VarE value_3)]) where
db_2 = mkName "db_2" ; value_3 = mkName "value_3"
However if I replace updater by its expansion, I get a type error. Why? It
looks like an expansion of a quasi quote is not identical to the quasi
quote?
Rene.
More information about the template-haskell
mailing list