1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
-- from https://ocharles.org.uk/blog/guest-posts/2014-12-19-existential-quantification.html
data HashMap k v = HM -- ... -- actual implementation
class Hashable v where
h :: v -> Int
data HashMapM hm = HashMapM
{ empty :: forall k v . hm k v
, lookup :: Hashable k => k -> hm k v -> Maybe v
, insert :: Hashable k => k -> v -> hm k v -> hm k v
, union :: Hashable k => hm k v -> hm k v -> hm k v
}
data HashMapE = forall hm . HashMapE (HashMapM hm)
-- public
mkHashMapE :: Int -> HashMapE
mkHashMapE = HashMapE . mkHashMapM
-- private
mkHashMapM :: Int -> HashMapM HashMap
mkHashMapM salt = HashMapM { {- implementation -} }
instance Hashable String where
type Name = String
data Gift = G String
giraffe :: Gift
giraffe = G "giraffe"
addGift :: HashMapM hm -> hm Name Gift -> hm Name Gift
addGift mod gifts =
let
HashMapM{..} = mod
in
insert "Ollie" giraffe gifts
-- -------------------------------
santa'sSecretSalt = undefined
sendGiftToOllie = undefined
traverse_ = undefined
sendGifts =
case mkHashMapE santa'sSecretSalt of
HashMapE (mod@HashMapM{..}) ->
let
gifts = addGift mod empty
in
traverse_ sendGiftToOllie $ lookup "Ollie" gifts
|