Git Product home page Git Product logo

prim-uniq's Introduction

Build Status

Unique values and an ad-hoc "unique-tag GADT"

This library defines 2 types - Uniq and Tag.

Uniq is a traditional "unique value" type, extended with a state-token type parameter so it works in both IO and ST.

Tag is like Uniq with the addition of a phantom type parameter. The type of that parameter is fixed at the time the Tag is created, so the uniqueness of the tag means that equality of tag values witnesses equality of their phantom types. In other words, given two Tags, if they are equal then their phantom type parameters are the same - just like pattern matching on a GADT constructor. The GEq and GCompare classes from the dependent-sum package can be used to discover that type equality, allowing Tag to be used for a pretty cool semi-dynamic typing scheme. For example (using the dependent-map library):

import Data.Unique.Tag
import Data.Dependent.Map

main = do
    x <- newTag
    y <- newTag
    z <- newTag
    let m1 = fromList [x :=> 3,  y :=> "hello"]
        m2 = fromList [x :=> 17, z :=> (True, x)]
        -- the type checker would (rightly) reject this line:
        -- m3 = singleton y ("foo", "bar")
    
    print (m1 ! x)
    print (m1 ! y)
    print (m2 ! x)
    print (m1 ! snd (m2 ! z))

Which would print:

3
"hello"
17
3

prim-uniq's People

Contributors

int-e avatar mokus0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

prim-uniq's Issues

getUnique should not use atomicModifyIORef

atomicModifyIORef is overly lazy. If the uniques aren't inspected promptly, this can leak. The smallest fix is

getUniq = unsafePrimToPrim (atomicModifyIORef' nextUniq (\u -> (u + 1, Uniq u)))

atomicModifyIORef' takes care of the forcing you were trying to do by hand, and more importantly forces the actual function result "from the outside" to limit the leak.

I am rather curious whether there's an even better way, at least for 64-bit systems. The fact that uniques are not guaranteed to be consecutive seems to suggest that there may be a decent way to do it using a two-word counter where the high word may be incremented an arbitrary (but typically small) number of times each time the low word overflows. Such an approach, if feasible, would reduce memory allocation while maintaining over 96 bits of effective uniqueness.

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.