summaryrefslogtreecommitdiff
path: root/nestedEither.hs
diff options
context:
space:
mode:
Diffstat (limited to 'nestedEither.hs')
-rw-r--r--nestedEither.hs34
1 files changed, 34 insertions, 0 deletions
diff --git a/nestedEither.hs b/nestedEither.hs
new file mode 100644
index 0000000..64b9412
--- /dev/null
+++ b/nestedEither.hs
@@ -0,0 +1,34 @@
1#! /usr/bin/env nix-shell
2#! nix-shell -i ghci shell.nix
3
4-- Type indexed nested Eithers
5
6{-# LANGUAGE TypeOperators #-}
7{-# LANGUAGE MultiParamTypeClasses #-}
8{-# LANGUAGE FlexibleInstances #-}
9{-# LANGUAGE TypeSynonymInstances #-}
10
11type (:||:) = Either
12infixr 1 :||:
13
14class Contains h n where
15 extract :: h -> Maybe n
16 insert :: n -> h
17
18instance Contains (a :||: b) a where
19 extract (Left a) = Just a
20 extract _ = Nothing
21 insert = Left
22
23instance Contains (a :||: b) b where
24 extract (Right a) = Just a
25 extract _ = Nothing
26 insert = Right
27
28instance {-# OVERLAPS #-} Contains h n => Contains (a :||: h) n where
29 extract (Right a) = extract a
30 extract _ = Nothing
31 insert = Right . insert
32
33test :: Maybe String
34test = extract (insert "test" :: String :||: Integer :||: Float)