diff options
-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) | ||