Questions tagged [scrap-your-boilerplate]

This Haskell package contains the generics system described in the Scrap Your Boilerplate papers. It defines the Data class of types permitting folding and unfolding of constructor applications, instances of this class for primitive types, and a variety of traversals.

This package contains the generics system described in the Scrap Your Boilerplate papers (see http://www.cs.uu.nl/wiki/GenericProgramming/SYB).

It defines the Data class of types permitting folding and unfolding of constructor applications, instances of this class for primitive types, and a variety of traversals.

http://hackage.haskell.org/package/syb

39 questions
4
votes
1 answer

Recursive Type Lensing

I'm trying to create some code that can take any recursive grammar data type and any expression of that data type and produce a list of all sub-expressions of the same type, built up, kind of like a scan on the recursion of the type. I've written…
Julian Leviston
  • 1,646
  • 10
  • 21
4
votes
2 answers

Deriving functor instance, not on last type argument

Related to this question I asked earlier today. I have an AST data type with a large number of cases, which is parameterized by an "annotation" type data Expr ann def var = Plus a Int Int | ... | Times a Int Int deriving (Data, Typeable,…
jmite
  • 8,171
  • 6
  • 40
  • 81
3
votes
2 answers

Traversing polymorphic structures and performing a transformation only in few cases

Suppose we represent a company hierarchy in the following way: {-# LANGUAGE DeriveDataTypeable #-} import Data.Data import Data.Generics.Aliases import Data.Generics.Schemes data CompanyAsset = Employee Name Salary …
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
3
votes
2 answers

SYB: can a map over the result of listify be rewritten with a gfoldl?

Can I use SYB's gfoldl to do the map over the result of listify in one go? Consider for example the following code: extractNums :: Expr -> [Int] extractNums e = map numVal $ listify isNum e where isNum :: Expr -> Bool isNum (Num _) =…
3
votes
2 answers

Convert from type `T a` to `T b` without boilerplate

So, I have an AST data type with a large number of cases, which is parameterized by an "annotation" type data Expr a = Plus a Int Int | ... | Times a Int Int I have annotation types S and T, and some function f :: S -> T. I want to take an…
jmite
  • 8,171
  • 6
  • 40
  • 81
3
votes
1 answer

Generic data constructor for Data instance

Given a datatype data Foo = IFoo Int | SFoo String deriving (Data, Typeable) what is a simple definition of gconstr :: (Typeable a, Data t) => a -> t such that gconstr (5 :: Int) :: Foo == IFoo 5 gconstr "asdf" :: Foo == SFoo "asdf" gconstr True…
Dan
  • 12,409
  • 3
  • 50
  • 87
2
votes
1 answer

Matching higher-kinded types in SYB

In general, I'm wondering if there's a way to write a generic fold that generalizes a function that applies a forall type like: f :: forall a. Data (D a) => D a -> b given some datatype D for which instance Data (D a) (possibly with constraints on…
2
votes
0 answers

Scrapping the boilerplate of doing variable substitution in an expression

Suppose we have the following data structure, whose values represent expressions on some language: data BExpr = Stop | Guard Expr BExpr | Choice BExpr BExpr | Parallel BExpr BExpr | Disable BExpr BExpr …
Damian Nadales
  • 4,907
  • 1
  • 21
  • 34
2
votes
2 answers

Data.Data -- producing dataCast1 for an arity 2 type constructor (partially specialized)

So Data.Map has dataCast2 defined, which makes sense, as it has an arity 2 type constructor. dataCast1 defaults to const Nothing. dataCast2 is easily defined as gcast2. For reference: class Typeable a => Data a where dataCast1 :: Typeable1 t =>…
sclv
  • 38,665
  • 7
  • 99
  • 204
2
votes
0 answers

How to traverse a Seq with the Scrap Your Boileplate combinator "everything" in Shapeless?

I want to use the Scrap Your Boilerplate implementation in Shapeless to operate on some case classes but i'm stuck trying to figure out how to make the SYB combinator everything traverse a field of type Seq[...]. For example, defining trees…
2
votes
1 answer

Visiting GHC AST with SYB

I wrote a program that visited the AST with Haskell-src-exts. I'm trying to convert it to use the GHC API. The former uses Uniplate, while for the latter it seems that unfortunately I'm forced with SYB (the documentation is horribly scarce). Here is…
rubik
  • 8,814
  • 9
  • 58
  • 88
2
votes
0 answers

Deriving Typeable for Text.PrettyPrint.Doc

I have an AST type that I want to derive as Typeable, so that I can do Scrap-your-boilerplate generic traversals of it. However, the tree is annotated with messages in the Doc type of the Text.PrettyPrint library from the pretty package. To derive…
jmite
  • 8,171
  • 6
  • 40
  • 81
2
votes
1 answer

Understanding the type of the cast operator in Scrap Your Boilerplate

In the 2003 Scrap Your Boilerplate paper by Laemmel and SPJ there is a code snippet on page 3 mkT :: (Typeable a, Typeable b) => (b -> b) -> a -> a mkT f = case cast f of Just g -> g Nothing -> id and then the paper explains That is, mkT f x…
afsmi
  • 167
  • 6
1
vote
2 answers

SYB `mkT` function in Scala

Continuing on from a previous question of mine, I am attempting to implement Scrap Your Boilerplate in scala 3 and am running into an issue now with the mkT function described in the paper. Given the following definition of cast: trait Cast[A,…
anqit
  • 780
  • 3
  • 12
1
vote
2 answers

Haskell syb Data.Generics not working as expected

On a ghci prompt everywhere (mkT (\x -> 2 * x)) (8.7, 21, "word") evaluates to (8.7, 42, "word"). I expected the 8.7 to be doubled as well. Why am I wrong?
not-a-user
  • 4,088
  • 3
  • 21
  • 37