Giter Club home page Giter Club logo

dependent-sum's Introduction

dependent-sum Build Status

This library defines a dependent sum type:

data DSum tag f = forall a. !(tag a) :=> f a

By analogy to the key => value construction for dictionary entries in many dynamic languages, we use :=> as the constructor for dependent sums. The key is a tag that specifies the type of the value; for example, think of a GADT such as:

data Tag a where
   StringKey    :: Tag String
   IntKey       :: Tag Int

Then, we have the following valid expressions of type DSum Tag []:

StringKey   :=> ["hello!"]
IntKey      :=> [42]

And we can write functions that consume DSum Tag values by matching, such as:

toString :: DSum Tag [] -> [String]
toString (StringKey :=> strs) = strs
toString (IntKey    :=> ints) = show ints

The :=> and ==> operators have very low precedence and bind to the right, so if the Tag GADT is extended with an additional constructor Rec :: Tag (DSum Tag Identity), then Rec ==> AnInt ==> 3 + 4 is parsed as would be expected (Rec ==> (AnInt ==> (3 + 4))) and has type DSum Identity Tag. Its precedence is just above that of $, so foo bar $ AString ==> "eep" is equivalent to foo bar (AString ==> "eep").

In order to support basic type classes from the Prelude for DSum, there are also several type classes defined for "tag" types:

  • GShow tag means that tag a has (the equivalent of) a Show instance.
  • ShowTag tag f means that if tag a is inhabited (as witnessed by providing an instance), then f a has (the equivalent of) a Show instance.

There are similar classes for the Prelude's Eq, Ord and Read classes. Together, they provide the following instances for DSum:

instance ShowTag tag f => Show (DSum tag f)
instance ReadTag tag f => Read (DSum tag f)
instance EqTag   tag f => Eq   (DSum tag f)
instance OrdTag  tag f => Ord  (DSum tag f)

For example implementations of these classes, see the generated Haddock docs or the code in the examples directory. There is a fair amount of boilerplate. A few of the more common classes (GEq, GCompare, and GShow) can be automatically derived by Template Haskell code in the dependent-sum-template package. It would be nice to implement more derivations (and it should be nearly as straightforward as deriving the Prelude classes they support), but I haven't done so yet.

dependent-sum's People

Contributors

alexfmpe avatar cyrbon avatar int-index avatar kmicklas avatar lambdaknight avatar mokus0 avatar tommd avatar treeowl avatar

Watchers

 avatar  avatar  avatar

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.