HaskellWiki

Haskell | Wiki community | Recent changes
Random page | Special pages

 

Not logged in
Log in | Help

Global keys

Categories: Idioms

Contents

1 Introduction

This page is meant to contain discussion about global state and "things with identity". It's a companion to Adrian's http://www.haskell.org/hawiki/GlobalMutableState, but that's in the old Wiki, so I thought it better to start a new page.

See also Ian Stark's ACIO message http://www.haskell.org/pipermail/haskell-cafe/2004-November/007664.html.

2 Examples

As Brian Hulley put it, at the moment, there is a strange unnatural discrepancy between the fixed set of built-in privileged operations such as Data.Unique.newUnique which are "allowed" to make use of global state, and user defined operations which have to rely on a shaky hack in order to preserve natural abstraction barriers between components such as a user-defined Unique, Atom, and anything involving memoisation or device management etc.

The kind of applications we have in mind (please add more) are:

3 A straw man

The basic idea is this:

(I don't know how to put code inside a bullet, and carry on the bullet afterwards, so the formatting below is a mess.)

More concretely:

  lookup :: Dict -> Key a -> Maybe a
  insert :: Dict -> Key a -> a -> Dict
  union  :: Dict -> Dict -> Dict
  etc

Yes, it'll need a massive interface, like Data.Map.

  newKey :: IO (Key a)
  key rng :: Key StdGen

This is useful independent of mutability. For example, GHC has lots of code going

  thenIdKey = mkPreludeIdKey 3
  returnIdName = mkPreludeIdKey 4

etc, where we hand out unique identifiers by hand. Easy to screw up.

  getDict :: IO Dict
  setDict :: Dict  -> IO a -> IO a

(Think of myThreadId.) Notice that setDict does not mutate the dictionary; it just sets the implicit dictionary for a nested sub-computation.

4 Initialisation

Initialisation is the big question here. A library may want to allocate a key, and an initialiser to be run the first time the key is accessed, without the client of the library needing to know about it at all. A second issue that we may sometimes want to implicitly (?) re-initialise (or clear) all or part of the dictionary when forking a thread.

Retrieved from "http://www.haskell.org/haskellwiki/Global_keys"

This page has been accessed 1,825 times. This page was last modified 10:02, 17 September 2006. Recent content is available under a simple permissive license.