-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuncType.hs
47 lines (37 loc) · 1.29 KB
/
FuncType.hs
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
{-# LANGUAGE Rank2Types #-}
module FuncType where
import Lens
import Pointed
import MyShow
import qualified Data.Map as M
-- (Pointed a) => a -> k
newtype Func k a = Func { getFunc :: M.Map k a }
deriving (Eq, Show)
instance Pointed (Func k a) where
point = Func M.empty
infixl 9 !
(!) :: (Ord k, Pointed a) => Func k a -> k -> a
f ! k = M.findWithDefault point k (getFunc f)
setValue :: (Ord k, Pointed a, Eq a) => k -> a -> Func k a -> Func k a
setValue k a
| a == point = Func . M.delete k . getFunc
| otherwise = Func . M.insert k a . getFunc
at :: (Ord k, Pointed a, Eq a) => k -> Lens' (Func k a) a
at n h f = fmap (\ x -> setValue n x f) (h (f ! n))
instance (MyShow k, MyShow a, Pointed a, Eq a) => MyShow (Func k a) where
myShowsPrec d f = let
mapstoPrec1 = 0
mapsto = ": " -- " |-> "
showx (k, a) =
myShowsPrec mapstoPrec1 k .
showString mapsto .
myShowsPrec mapstoPrec1 a
showy =
showChar '_' .
showString mapsto .
-- myShowsPrec 11 (point :: a) <<< this does not work! (why?)
myShowsPrec mapstoPrec1 (let pt = point ; b = pt == snd (head l) in pt) -- too bad
l = filter ((/= point) . snd) . M.toList . getFunc $ f
in showChar '{' .
foldr (\ v r -> showx v . showString ", " . r) showy l .
showChar '}'