Last active
December 4, 2020 04:00
-
-
Save lgastako/a74470ac8d528a9fae05a9fa7122f0fc to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE DeriveDataTypeable #-} | |
{-# LANGUAGE TypeApplications #-} | |
module Biplate where | |
import Control.Lens | |
import Data.Data | |
import Data.Data.Lens | |
data B = C | D | E | |
deriving (Data, Enum, Show) | |
newtype X = X B | |
deriving (Data, Show) | |
newtype Y = Y B | |
deriving (Data, Show) | |
data Foo = N | I | Bar X | Baz Y | |
deriving (Data, Show) | |
n :: Foo | |
n = N | |
i :: Foo | |
i = I | |
bar :: Foo | |
bar = Bar (X C) | |
baz :: Foo | |
baz = Baz (Y D) | |
n' :: Foo | |
n' = n & (biplate :: Traversal' Foo B) %~ succ | |
i' :: Foo | |
i' = i & biplate %~ (succ :: B -> B) | |
bar' :: Foo | |
bar' = bar & biplate %~ (succ :: B -> B) | |
baz' :: Foo | |
baz' = baz & biplate %~ succ @B | |
bif :: [Foo] | |
bif = [bar, baz] | |
-- λ> (n, i, bar, baz) | |
-- (N,I,Bar (X C),Baz (Y D)) | |
-- λ> (n', i', bar', baz') | |
-- (N,I,Bar (X D),Baz (Y E)) | |
-- λ> bif | |
-- [Bar (X C),Baz (Y D)] | |
-- λ> bif & biplate %~ succ @B | |
-- [Bar (X D),Baz (Y E)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment