snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
152 stars 63 forks source link

Need class for containers of elements that have their own keys #129

Open AshleyYakeley opened 7 years ago

AshleyYakeley commented 7 years ago

Currently, there's the IsMap class:

class (MonoTraversable map, SetContainer map) => IsMap map where
    type MapValue map
    lookup :: ContainerKey map -> map -> Maybe (MapValue map)
    insertMap :: ContainerKey map -> MapValue map -> map -> map
    -- etc.

In most (all?) instances of IsMap, we have Element map ~ (ContainerKey map, MapValue map), that is, there's some MapValue type that is "the element minus the key".

However, for some containers, there isn't a suitable MapValue type because the key cannot easily be removed from the Element type. Instead, we'd want a class that looks more like this:

class (MonoTraversable map, SetContainer map) => KeyContainer map where
    elementKey :: Element map -> ContainerKey map
    lookupElement :: ContainerKey map -> map -> Maybe (Element map)
    insertElement :: Element map -> map -> map
    -- etc.
snoyberg commented 7 years ago

Can you give an example of one of these other container types you're talking about?

On Fri, Apr 14, 2017, 2:08 AM Ashley Yakeley notifications@github.com wrote:

Currently, there's the IsMap class:

class (MonoTraversable map, SetContainer map) => IsMap map where type MapValue map lookup :: ContainerKey map -> map -> Maybe (MapValue map) insertMap :: ContainerKey map -> MapValue map -> map -> map -- etc.

In most (all?) instances of IsMap, we have Element map ~ (ContainerKey map, MapValue map), that is, there's some MapValue type that is "the element minus the key".

However, for some containers, there isn't a suitable MapValue type because the key cannot easily be removed from the Element type. Instead, we'd want a class that looks more like this:

class (MonoTraversable map, SetContainer map) => KeyContainer map where elementKey :: Element map -> ContainerKey map lookupElement :: ContainerKey map -> map -> Maybe (Element map) insertElement :: Element map -> map -> map -- etc.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/snoyberg/mono-traversable/issues/129, or mute the thread https://github.com/notifications/unsubscribe-auth/AADBB5AFjQXY7ZJzj5uWFAcHK_fNFt6sks5rvqr_gaJpZM4M9TTt .

AshleyYakeley commented 7 years ago

I don't know if such types already exist, but:

  1. A hash-addressable store, where Element is ByteString and ContainerKey is a SHA-256 hash.
  2. An in-memory typed SQL table, where Element is a row, and ContainerKey is the primary key.
snoyberg commented 7 years ago

I'm sorry, but at this level of abstraction I'm having a really hard time understanding the request here.

AshleyYakeley commented 7 years ago

Fair enough. Given that I don't actually know of any types in existing Haskell packages that would implement this and not IsMap, it might not be worth it.