Created
February 21, 2017 08:59
-
-
Save as-capabl/08d568d34c5a91d7fdb2f8820b475d24 to your computer and use it in GitHub Desktop.
LensとPrismで解きたい問題
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 与えるもの:あるレコード型とその上のLens | |
data Hoge = Hoge { _hoge1 :: ..., _hoge2 :: ..., ... } | |
makeLenses ''Hoge -- hoge1, hoge2, ...が定義される | |
-- ↓ ここからTemplate Haskellで生成するか、あるいは... | |
-- 欲しいもの1:直和型とその上のPrism | |
data CoHoge = CoHoge1 ... | CoHoge2 ... | |
makePrisms ''CoHoge -- _CoHoge1, _CoHoge2, ...が定義される | |
-- CoHogeのコンストラクタはなくてもいい。型とPrismが必要 | |
-- 欲しいもの2:Hogeのフィールドのうち、CoHogeにセットされたものを更新する関数 | |
applyCoHoge :: CoHoge -> Hoge -> Hoge | |
-- Law: 各hogeXと_CoHogeXのペアについて、全ての値xに対し | |
-- applyCoHoge (review _CoHogeX x) === set hogeX x | |
-- ↑ ここまで | |
-- 以下の型と関数が定義できれば、TH不要で解決。 | |
data Var s = ... | |
l2p :: Lens' s a -> Prism' (Var s) a | |
p2l :: Prism' (Var s) a -> Lens' s a | |
applyVar :: Var s -> s -> s | |
-- Law: 全てのl, xについて applyVar (review (l2p l) x) === set l x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment