Hatena::Grouphaskell

suztomoの日記

 | 

2011-12-28

できた(?)Template Haskell

22:54

できる!Template Haskell (完) - はてな使ったら負けだと思っている deriving Haskell - haskellをやっている。

"$()"の演算子が見つからないなと思っていたら

*TupTH> $(varE $ mkName "pi")
<interactive>:1:1: parse error on input `$'

言語の大切な部分にさわる関数なので(?)、"-XTemplateHaskell"が必要なのでした。

~/Documents/.../Haskell/TemplateHaskell $ ghci -XTemplateHaskell -XQuasiQuotes TupTH.hs 
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling TupTH            ( TupTH.hs, interpreted )
Ok, modules loaded: TupTH.
*TupTH> $(varE $ mkName "pi")
Loading package array-0.3.0.2 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package pretty-1.0.1.2 ... linking ... done.
Loading package template-haskell ... linking ... done.
3.141592653589793

そしてsplice(接合)について、

$(varE $ mkName "pi")

$(varE =<< newName "pi")

って中の型が違うんですけどいいのかな。後者はQもなどの中に入っているけれども、前者はQもなどには入っていないただのExp型である。


。。。と思ったらVarEだと思っていたのはvarEでこれはQモナドに入ったものを作ってくれるユーティリティ関数らしいです。

*TupTH> :t varE
varE :: Name -> ExpQ
*TupTH> :t VarE
VarE :: Name -> Exp
~/Documents/.../Haskell/TemplateHaskell $ ghci -XTemplateHaskell TupTH.hs
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
[1 of 1] Compiling TupTH            ( TupTH.hs, interpreted )
Ok, modules loaded: TupT
*TupTH> $(sel 10 7) (1,1,1,1,1,1,5,1,1,1)
5

うごいた。



この調子でYesodのmkMessageがどんなものなのかをしらべてみる。

~/Documents/.../Yesod/testyesod $ ghci -cpp -XTemplateHaskell -XOverloadedStrings Foundation.hs
GHCi, version 7.0.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Ok, modules loaded: Foundation, Settings, Model, Settings.StaticFiles.
Prelude Foundation> let e = mkMessage "Test" "messages" "ja"
Loading package transformers-0.2.2.0 ... linking ... done.
Loading package bytestring-0.9.1.10 ... linking ... done.
Loading package mtl-2.0.1.0 ... linking ... done.
Loading package parsec-3.1.1 ... linking ... done.
Loading package array-0.3.0.2 ... linking ... done.
Loading package containers-0.4.0.0 ... linking ... done.
Loading package deepseq-1.1.0.2 ... linking ... done.
Loading package pretty-1.0.1.2 ... linking ... done.
Loading package filepath-1.2.0.0 ... linking ... done.
Loading package old-locale-1.0.0.2 ... linking ... done.
Loading package old-time-1.0.0.6 ... linking ... done.
Loading package unix-2.4.2.0 ... linking ... done.
Loading package directory-1.1.0.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
Loading package text-0.11.1.12 ... linking ... done.
Loading package shakespeare-0.10.2 ... linking ... done.
Loading package shakespeare-i18n-0.0.0 ... linking ... done.
Prelude Foundation> :m Language.Haskell.TH
Prelude Language.Haskell.TH> runQ e
[DataD [] TestMessage [] [RecC MsgHello []] [],InstanceD [] (AppT (AppT (ConT Text.Shakespeare.I18N.RenderMessage) (ConT Test)) (ConT TestMessage)) [FunD renderMessage [Clause [WildP,ConP : [VarP lang_0,WildP],RecP MsgHello []] (GuardedB [(NormalG (InfixE (Just (VarE lang_0)) (VarE GHC.Classes.==) (Just (AppE (VarE Data.Text.pack) (LitE (StringL "en"))))),AppE (VarE Data.Text.pack) (SigE (LitE (StringL "Hell")) (ConT GHC.Base.String)))]) [],Clause [WildP,ConP : [VarP lang_1,WildP],RecP MsgHello []] (GuardedB [(NormalG (InfixE (Just (VarE lang_1)) (VarE GHC.Classes.==) (Just (AppE (VarE Data.Text.pack) (LitE (StringL "ja"))))),AppE (VarE Data.Text.pack) (SigE (LitE (StringL "Hel")) (ConT GHC.Base.String)))]) [],Clause [WildP,ConP [] [],RecP MsgHello []] (NormalB (AppE (VarE Data.Text.pack) (SigE (LitE (StringL "Hel")) (ConT GHC.Base.String)))) [],Clause [VarP sub_2,ConP : [WildP,VarP langs_3],VarP msg_4] (NormalB (AppE (AppE (AppE (VarE Text.Shakespeare.I18N.renderMessage) (VarE sub_2)) (VarE langs_3)) (VarE msg_4))) []]]]

読めない。。。

mr_konnmr_konn2011/12/28 22:57>中の型が違うんですけどいいのかな。

前者がExpQ に対して後者が Q Exp である、と云うことであれば、 ExpQ は Q Exp のエイリアスみたいなものなので大丈夫なはずです。

インモラルインモラル2013/09/23 11:21淫インモラル:http://immoral.hellokanpo.com
インモラル:http://immoral.hellokanpo.com
媚薬 淫インモラル:http://immoral.hellokanpo.com
媚薬 インモラル:http://immoral.hellokanpo.com
妖姫:http://youhi.hellokanpo.com
媚薬 妖姫:http://youhi.hellokanpo.com



威哥王:http://www.hellokanpo.com/view/weigewang.html
巨人倍増:http://www.hellokanpo.com/view/jurenbeiceng.html
三便宝:http://www.hellokanpo.com/view/satibo-capsules.html
D10 媚薬:http://www.hellokanpo.com/view/D10-meiyao.html
RU486:http://www.hellokanpo.com/view/beijing-ru486.html
花痴:http://www.hellokanpo.com/view/huachi.html
紅蜘蛛:http://www.hellokanpo.com/view/hongzhizhu1.html
狼一号:http://www.hellokanpo.com/view/langyihao.html
三體牛鞭:http://www.hellokanpo.com/view/santiniubian.html
蟻力神:http://www.hellokanpo.com/view/yilishen.html
D10 催情剤:http://www.hellokanpo.com/view/D10-meiyao.html
三体牛鞭:http://www.hellokanpo.com/view/santiniubian.html
天天素:http://www.hellokanpo.com/view/tiantiansu.html
福源春:http://www.hellokanpo.com/view/fuyuanchun.html
韓国痩身1号:http://www.hellokanpo.com/view/hanguoshou-575.html


五便宝:http://www.hellokanpo.com/view/wodibo-capsules.html
さんべんぼう:http://www.hellokanpo.com/view/satibo-capsules.html
キョジンバイゾウ:http://www.hellokanpo.com/view/jurenbeiceng.html
蒼蝿水:http://www.hellokanpo.com/view/FLY-D5.html
VigRx:http://www.hellokanpo.com/view/VigRx.html
ウェイカワン:http://www.hellokanpo.com/view/weigewang.html
VVK:http://www.hellokanpo.com/view/VVK-Wenickman.html
狼1号:http://www.hellokanpo.com/view/langyihao.html
男宝:http://www.hellokanpo.com/view/nanbao.html
vigRx oil:http://www.hellokanpo.com/view/Oil.html
V26:http://www.hellokanpo.com/view/V26.html
曲美:http://www.hellokanpo.com/view/qumei.html
SPANISCHE FLIEGE:http://www.hellokanpo.com/view/spanische-flieged5-1.html
巨根カプセル:http://www.hellokanpo.com/view/jugen.html
絶對高潮:http://www.hellokanpo.com/view/jueduigaochao.html
SEX DROPS:http://www.hellokanpo.com/view/sex-drops.html
終極痩身:http://www.hellokanpo.com/view/zhongjishoushen.html
福源春カプセル:http://www.hellokanpo.com/view/fuyuanchun.html
超級脂肪燃焼弾:http://www.hellokanpo.com/view/FATBURNING.html
SUPER FAT BURNING:http://www.hellokanpo.com/view/SUPERFATBURNING.html
D5 原液:http://www.hellokanpo.com/view/FLYD5yuan.html
イーリーシン:http://www.hellokanpo.com/view/yilishen.html
黒倍王:http://www.hellokanpo.com/view/heibeiwang.html
新一粒神:http://www.hellokanpo.com/view/xinyilishen.html
女性催情剤:http://www.hellokanpo.com/view/d10-meiyao.html
蒼蝿粉:http://www.hellokanpo.com/view/inverma.html
ビグレックス:http://www.hellokanpo.com/view/Oil.html
蔵八宝:http://www.hellokanpo.com/view/zhangbabao.html
小情人:http://www.hellokanpo.com/view/sex-drops5.html
御秀堂:http://www.hellokanpo.com/view/yuxiutang.html
威哥王 販売:http://www.hellokanpo.com/view/weigewang.html
紅蜘蛛 液体:http://www.hellokanpo.com/view/hong-zhi-zhu.html
紅蜘蛛 激安:http://www.hellokanpo.com/view/hong-zhi-zhu-meiyao.html

 |