Haskell Memories

If I have key maps to values, then key sets can be implemented as key maps with fixed dummy values.

There are many candidates for mannequins:

  • data -defined types without constructors
  • other non-residential types (e.g. forall a . a )
  • single types
  • unboxed types

The most obvious solution for me is to use a simple singleton type () , but with case I can distinguish () from below, so I think that the memory view () includes indirect references.

I have 2 questions:

  • Does Map.fromList [(1, ()), (2, ())] more memory than let dummy = () in Map.fromList [(1, dummy), (2, dummy)] ?
  • What value is recommended to use for dummy to build sets from bytestring-trie , given the amount of memory, CPU usage and correctness?
+4
source share
2 answers

All keys will be presented (represented as), pointing to one selected constructor () .

+4
source

Zero constructors are allocated only once. Then all their applications are separated (in GHC, this behavior is not dictated by the Haskell standard).

() is the null constructor of a unit of type () . Therefore, using () universally worth some kind of memory. If you create a parameter of type () , you will still pay for the presence of this parameter. That is why, for example, there is a specialized Set a instead of Map a () .

For a data structure with a key, you need a type with the correct value. Therefore () is the right choice, but empty data types are not. A polymorphic type such as forall a. a forall a. a , must additionally be wrapped in a different data type, or require an offense if it is used as an argument, which is usually not supported properly.

+11
source

All Articles