summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2015-12-11 14:48:54 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2015-12-11 14:48:54 +0100
commiteefadef0e9d0e34cec75680fddcb4237a2d7c695 (patch)
tree24fe443c05e94657d2dd6f877bae2f85019e54fc
parent047f6c71c6c22b69df8abf30662827033aa1bdcb (diff)
downloadfeeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.tar
feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.tar.gz
feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.tar.bz2
feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.tar.xz
feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.zip
"generalized" either
-rw-r--r--genEither.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/genEither.hs b/genEither.hs
new file mode 100644
index 0000000..4f6f7ae
--- /dev/null
+++ b/genEither.hs
@@ -0,0 +1,32 @@
1#! /usr/bin/env nix-shell
2#! nix-shell -i ghci shell.nix
3
4{-# LANGUAGE TypeOperators #-}
5{-# LANGUAGE MultiParamTypeClasses #-}
6{-# LANGUAGE FlexibleInstances #-}
7{-# LANGUAGE TypeSynonymInstances #-}
8
9type (:||:) = Either
10infixr 1 :||:
11
12class Contains h n where
13 extract :: h -> Maybe n
14 insert :: n -> h
15
16instance Contains (a :||: b) a where
17 extract (Left a) = Just a
18 extract _ = Nothing
19 insert = Left
20
21instance Contains (a :||: b) b where
22 extract (Right a) = Just a
23 extract _ = Nothing
24 insert = Right
25
26instance {-# OVERLAPS #-} Contains h n => Contains (a :||: h) n where
27 extract (Right a) = extract a
28 extract _ = Nothing
29 insert = Right . insert
30
31test :: Maybe String
32test = extract (insert "test" :: String :||: Integer :||: Float)