[Haskell-cafe] Template Haskell sees into abstract data types

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Sat Jul 3 18:50:56 EDT 2010


Serguey Zefirov <sergueyz at gmail.com> writes:

> Data.Map.Map and Data.Set.Set are exported abstractly, without
> exposing knowledge about their internal structure.
>
> I cannot directly create my own class instances for them because of
> that. But I found that I can write Template Haskell code that could do
> that - those data types could be reified just fine.

Huh?  Sure you can write class instances for them.

,----
| 
| import qualified Data.Map as Map
| import Data.Map(Map)
| import qualified Data.Set as Set
| import Data.Set(Set)
| 
| class SizeOf x where
|   sizeOf :: x -> Int
| 
| instance SizeOf [a] where
|   sizeOf = length
| 
| instance SizeOf (Set a) where
|   sizeOf = Set.size
| 
| instance SizeOf (Map k v) where
|   sizeOf = Map.size
`----

> This is somewhat strange situation.
>
> Was it a design decision?

The reason that they are exported abstractly is so that you don't see
the internals of the data structure, because 1) you don't need to, and
2) to stop you from doing anything stupid with them.

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com


More information about the Haskell-Cafe mailing list