diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-12-11 14:48:54 +0100 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-12-11 14:48:54 +0100 |
commit | eefadef0e9d0e34cec75680fddcb4237a2d7c695 (patch) | |
tree | 24fe443c05e94657d2dd6f877bae2f85019e54fc | |
parent | 047f6c71c6c22b69df8abf30662827033aa1bdcb (diff) | |
download | feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.tar feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.tar.gz feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.tar.bz2 feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.tar.xz feeds-eefadef0e9d0e34cec75680fddcb4237a2d7c695.zip |
"generalized" either
-rw-r--r-- | genEither.hs | 32 |
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 | |||
9 | type (:||:) = Either | ||
10 | infixr 1 :||: | ||
11 | |||
12 | class Contains h n where | ||
13 | extract :: h -> Maybe n | ||
14 | insert :: n -> h | ||
15 | |||
16 | instance Contains (a :||: b) a where | ||
17 | extract (Left a) = Just a | ||
18 | extract _ = Nothing | ||
19 | insert = Left | ||
20 | |||
21 | instance Contains (a :||: b) b where | ||
22 | extract (Right a) = Just a | ||
23 | extract _ = Nothing | ||
24 | insert = Right | ||
25 | |||
26 | instance {-# OVERLAPS #-} Contains h n => Contains (a :||: h) n where | ||
27 | extract (Right a) = extract a | ||
28 | extract _ = Nothing | ||
29 | insert = Right . insert | ||
30 | |||
31 | test :: Maybe String | ||
32 | test = extract (insert "test" :: String :||: Integer :||: Float) | ||