Giter VIP home page Giter VIP logo

higher's Introduction

higher

Higher-kinded data (HKD) types with less pain.

This library takes your regular Haskell data types and produces additional higher-kinded versions, along with term- and type-level conversion functions.

I wanted to leverage the power of higher-kinded data as an internal implementation detail of my code, without burdening others with the extra type parameter, Identity wrapping (or poor type inference from the type family unwrapping Identity), and overall increase in complexity.

With this library, I can work with my data in a higher-kinded fashion temporarily, and then convert back to regular Haskell data types when I'm done.

In other words, this:

import Higher

data Person = Person
  { name :: Text
  , age :: Word8
  }

$(higher ''Person)

...turns into this:

import Higher

data Person = Person
  { name :: Text
  , age :: Word8
  }

data PersonB f = PersonB
  { nameB :: f Text
  , ageB :: f Word8
  }

instance Higher Person PersonB where
  toHKD :: Person -> PersonB Identity
  toHKD (Person x0 x1) = PersonB (Identity x0) (Identity x1)

  fromHKD :: PersonB Identity -> Person
  fromHKD (PersonB x0 x1) = Person (runIdentity x0) (runIdentity x1)

instance FunctorB PersonB where
  bmap :: (forall a. f a -> g a) -> PersonB f -> PersonB g
  bmap f (PersonB x0 x1) = PersonB (f x0) (f x1)

instance TraversableB PersonB where
  btraverse :: Applicative e => (forall a. f a -> e (g a)) -> PersonB f -> e (PersonB g)
  btraverse f (PersonB x0 x1) = pure PersonB <*> f x0 <*> f x1

instance ConstraintsB PersonB where
  type AllB c PersonB = (c Text, c Word8)

  baddDicts :: AllB c PersonB => PersonB f -> PersonB (Product (Dict c) f)
  baddDicts (PersonB x0 x1) = PersonB (Pair Dict x0) (Pair Dict x1)

-- Other `barbies` type class instances coming soon

You can customize how the type constructor, data constructors, and fields are named with higherWith.

Take a look at the test suite for examples.

Check out higgledy if you prefer using Generic over Template Haskell.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.