summaryrefslogtreecommitdiff
path: root/genEither.hs
diff options
context:
space:
mode:
Diffstat (limited to 'genEither.hs')
-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)