Skip to content

Instantly share code, notes, and snippets.

@lgastako
Created December 3, 2020 23:30
Show Gist options
  • Save lgastako/690d6bfb4678c5f715b5d47218c6edfc to your computer and use it in GitHub Desktop.
Save lgastako/690d6bfb4678c5f715b5d47218c6edfc to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveDataTypeable #-}
module Biplate where
import Control.Lens
import Data.Data
import Data.Data.Lens
newtype X a = X a
deriving (Data, Show)
newtype Y a = Y a
deriving (Data, Show)
data Foo b = N | I | Bar (X b) | Baz (Y b)
deriving (Data, Show)
n :: Foo Int
n = N
i :: Foo Int
i = I
bar :: Foo Int
bar = Bar (X 5)
baz :: Foo Int
baz = Baz (Y 10)
n' :: Foo Int
n' = n & (biplate :: Traversal' (Foo Int) Int) *~ 2
i' :: Foo Int
i' = i & biplate *~ (2 :: Int)
bar' :: Foo Int
bar' = bar & biplate *~ (2 :: Int)
baz' :: Foo Int
baz' = baz & biplate *~ (2 :: Int)
-- λ> (n, i, bar, baz)
-- (N,I,Bar (X 5),Baz (Y 10))
-- λ> (n', i', bar', baz')
-- (N,I,Bar (X 10),Baz (Y 20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment