-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFKeyMap.hs
More file actions
43 lines (31 loc) · 1.02 KB
/
Copy pathFKeyMap.hs
File metadata and controls
43 lines (31 loc) · 1.02 KB
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
{-# LANGUAGE GADTs #-}
module FKeyMap(FBox(..), funlock, FKeyMap, singleton, empty, insert,lookup, (!)) where
import KeyM
import Control.Monad
import Prelude hiding (lookup,(!))
import Data.Maybe
import PFunctor
data FBox s f where
FLock :: Key s a -> f a -> FBox s f
funlock :: Key s a -> FBox s f -> Maybe (f a)
funlock k (FLock k' x) =
case testEquality k k' of
Just Refl -> Just x
Nothing -> Nothing
instance PFunctor (FBox s) where
pfmap f (FLock k x) = FLock k (f x)
newtype FKeyMap s f = FKm [FBox s f]
empty :: FKeyMap s f
empty = FKm []
singleton :: Key s a -> f a -> FKeyMap s f
singleton k v = insert k v empty
insert :: Key s a -> f a -> FKeyMap s f -> FKeyMap s f
insert k v (FKm l) = FKm (FLock k v : l)
lookup :: Key s a -> FKeyMap s f -> Maybe (f a)
lookup k (FKm []) = Nothing
lookup k (FKm (h : t)) =
funlock k h `mplus` lookup k (FKm t)
(!) :: FKeyMap s f -> Key s a -> f a
m ! k = fromJust (lookup k m)
instance PFunctor (FKeyMap s) where
pfmap f (FKm l) = FKm $ map (pfmap f) l