Skip to content

Instantly share code, notes, and snippets.

@lgastako
Created December 4, 2020 03:57
Show Gist options
  • Save lgastako/feadd7b82130ce7c071d1e76dc719f38 to your computer and use it in GitHub Desktop.
Save lgastako/feadd7b82130ce7c071d1e76dc719f38 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
-- λ> (n, i, bar, baz)
-- (N,I,Bar (X C),Baz (Y D))
-- λ> (n', i', bar', baz')
-- (N,I,Bar (X D),Baz (Y E))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment