Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a shallow traversal (biplate) #163

Open
aavogt opened this issue Apr 27, 2024 · 0 comments
Open

add a shallow traversal (biplate) #163

aavogt opened this issue Apr 27, 2024 · 0 comments

Comments

@aavogt
Copy link

aavogt commented Apr 27, 2024

I couldn't find an equivalent to biplate which just traverses the immediate children.

class Biplate s a where
  biplate :: Traversal' s a

instance HasTypesUsing (BiplateT s a) s s a a => Biplate s a where
  biplate f = typesUsing @(BiplateT s a) @a @s f

data BiplateT b c
type instance Children (BiplateT b c) a = ChildrenBiplate a b c
type family ChildrenBiplate (a :: Type) b c where
  ChildrenBiplate a a c = '[c]
  ChildrenBiplate a b c = '[]

-- example application to VisObject from vis / not-gloss which has Generic but not Data
buildBottom :: VisObject Double
buildBottom = Quad 0 (V3 220 0 0) (V3 220 220 0) (V3 0 220 0) white

-- | >>> buildTop
-- Quad (V3 0.0 0.0 280.0) (V3 220.0 0.0 280.0) (V3 220.0 220.0 280.0) (V3 0.0 220.0 280.0) (RGBA 1.0 1.0 1.0 1.0
-- here types doesn't work because a deep traversal of VisObject needs missing Generic instances
buildTop = buildBottom & biplate @_ @(V3 Double) . _3 .~ 280

Unusually, typesUsing puts the a parameter before the s. If we follow that convention instead, the expression could be biplate @(V3 Double).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant