Skip to content

Instantly share code, notes, and snippets.

@lgastako
Last active December 4, 2020 04:00
Show Gist options
  • Save lgastako/a74470ac8d528a9fae05a9fa7122f0fc to your computer and use it in GitHub Desktop.
Save lgastako/a74470ac8d528a9fae05a9fa7122f0fc to your computer and use it in GitHub Desktop.
{-# 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