scrap-your-boilerplate

Does the current SYB permit extension of generic functions with new types?

不想你离开。 提交于 2021-01-28 12:35:33
问题 The first two Scrap Your Boilerplate papers describe a way of writing generic functions that work for general types, but have special cases for specific types. For instance, fromJSON from the aeson package, defines a generic function for converting from JSON, but provides special cases for types like list or Int : parseJSON :: (Data a) => Value -> Parser a parseJSON j = parseJSON_generic j `ext1R` list `ext1R` vector `ext2R'` mapAny `ext2R'` hashMapAny -- Use the standard encoding for all

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

こ雲淡風輕ζ 提交于 2019-12-10 19:46:07
问题 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 Expr S and convert it to an Expr T using my conversion f on each S which occurs within an Expr value. Is there a way to do this using SYB or generics and avoid having to pattern match on every case? It seems like the type of thing that this is suited for.

How to construct generic Functor instances using GHC.Generics (or other similar frameworks)?

二次信任 提交于 2019-12-10 02:50:05
问题 I'm trying to learn GHC Generics. After reviewing several examples, I wanted to try to create a generic Functor instances (disregarding that GHC can derive them automatically for me). However, I realized I have no idea how to work with a parametrized data types with Generics, all the examples I've seen were of kind * . Is this possible, and if yes, how? (I'm also interested in other similar frameworks, such as SYB.) 回答1: The best place to look for lots of example functions using GHC Generics

How to construct generic Functor instances using GHC.Generics (or other similar frameworks)?

岁酱吖の 提交于 2019-12-05 03:43:26
I'm trying to learn GHC Generics. After reviewing several examples, I wanted to try to create a generic Functor instances (disregarding that GHC can derive them automatically for me). However, I realized I have no idea how to work with a parametrized data types with Generics, all the examples I've seen were of kind * . Is this possible, and if yes, how? (I'm also interested in other similar frameworks, such as SYB.) The best place to look for lots of example functions using GHC Generics is the generic-deriving package . There's a generic definition of the Functor class in there. Copying

What is Haskell's Data.Typeable?

孤人 提交于 2019-12-04 07:29:51
问题 I've come across references to Haskell's Data.Typeable , but it's not clear to me why I would want to use it in my code. What problem does it solve, and how? 回答1: Data.Typeable is an encoding of an well known approach (see e.g. Harper) to implementing delayed (dynamic) type checking in a statically typed language -- using a universal type. Such a type wraps code for which type checking would not succeed until a later phase. Rather than reject the program as ill-typed, the compiler passes it

Boilerplate-free annotation of ASTs in Haskell?

独自空忆成欢 提交于 2019-11-29 22:19:03
I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "annotation" to certain nodes, such as tail-calls, etc. I know I can use SYB or uniplate to do the traversal, but I'm wondering if there's a boilerplate-free way to deal with the types. So, suppose we have a bunch of algebraic types for our AST: data Expr = PlusExpr Expr Expr ... data Def = TypeAlias String [String] Type ... If I were writing the boilerplate, I'd make new types like this: data

Boilerplate-free annotation of ASTs in Haskell?

核能气质少年 提交于 2019-11-28 19:56:30
问题 I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "annotation" to certain nodes, such as tail-calls, etc. I know I can use SYB or uniplate to do the traversal, but I'm wondering if there's a boilerplate-free way to deal with the types. So, suppose we have a bunch of algebraic types for our AST: data Expr = PlusExpr Expr Expr ... data Def = TypeAlias