GithubHelp home page GithubHelp logo

sdzx-1 / dependent-map Goto Github PK

View Code? Open in Web Editor NEW

This project forked from obsidiansystems/dependent-map

0.0 0.0 0.0 163 KB

Dependently-typed finite maps (partial dependent products)

License: Other

Haskell 100.00%

dependent-map's Introduction

dependent-map Build Status Hackage

This library defines a dependently-typed finite map type. It is derived from Data.Map.Map in the containers package, but rather than (conceptually) storing pairs indexed by the first component, it stores DSums (from the dependent-sum package) indexed by tag. For example

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Example where

import Data.Constraint.Extras.TH (deriveArgDict)
import Data.Dependent.Map (DMap, fromList, singleton, union, unionWithKey)
import Data.Dependent.Sum ((==>))
import Data.Functor.Identity (Identity(..))
import Data.GADT.Compare.TH (deriveGCompare, deriveGEq)
import Data.GADT.Show.TH (deriveGShow)

data Tag a where
  StringKey :: Tag String
  IntKey    :: Tag Int
  DoubleKey :: Tag Double
deriveGEq ''Tag
deriveGCompare ''Tag
deriveGShow ''Tag
deriveArgDict ''Tag

x :: DMap Tag Identity
x = fromList [DoubleKey ==> pi, StringKey ==> "hello there"]

y :: DMap Tag Identity
y = singleton IntKey (Identity 42)

z :: DMap Tag Identity
z = y `union` fromList [DoubleKey ==> -1.1415926535897931]

addFoo :: Tag v -> Identity v -> Identity v -> Identity v
addFoo IntKey (Identity x) (Identity y) = Identity $ x + y
addFoo DoubleKey (Identity x) (Identity y) = Identity $ x + y
addFoo _ x _ = x

main :: IO ()
main = mapM_ print
  [ x, y, z
  , unionWithKey addFoo x z
  ]

dependent-map's People

Contributors

mokus0 avatar 3noch avatar cgibbard avatar ali-abrar avatar mankykitty avatar ericson2314 avatar s9gf4ult avatar joecrayne avatar srid avatar alexfmpe avatar chris-martin avatar treeowl avatar felixonmars avatar hgoes avatar phadej avatar infinity0 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.