-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.hs
43 lines (31 loc) · 992 Bytes
/
embed.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
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
module Embed(
embed,
innerHJoin,
outterHJoin,
) where
import Data.Map
import Control.Monad
class Embed f g where
embed :: f -> g
innerHJoin :: (Embed (f a) (g a),
Monad g, Functor g) => g (f a) -> g a
innerHJoin = join . fmap embed
outterHJoin :: (Embed (f (g a)) (g (g a)),
Monad g, Functor g) => f (g a) -> g a
outterHJoin = join . embed
instance Embed a a where
embed = id
--instance (Embed a b, Embed b c) => Embed a c where
-- embed = embed . embed
instance Embed (Maybe a) (Either () a) where
embed (Just x) = Right x
embed Nothing = Left ()
instance Embed (Maybe a) [a] where
embed (Just x) = [x]
embed Nothing = []
instance Embed [a] (Map Int a) where
embed = Data.Map.fromList . zip [0..]
instance Embed (Map Int a) ((->) Int (Maybe a)) where
embed = flip Data.Map.lookup