diff options
Diffstat (limited to 'nestedEither.hs')
| -rw-r--r-- | nestedEither.hs | 34 |
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 | |||
| 11 | type (:||:) = Either | ||
| 12 | infixr 1 :||: | ||
| 13 | |||
| 14 | class Contains h n where | ||
| 15 | extract :: h -> Maybe n | ||
| 16 | insert :: n -> h | ||
| 17 | |||
| 18 | instance Contains (a :||: b) a where | ||
| 19 | extract (Left a) = Just a | ||
| 20 | extract _ = Nothing | ||
| 21 | insert = Left | ||
| 22 | |||
| 23 | instance Contains (a :||: b) b where | ||
| 24 | extract (Right a) = Just a | ||
| 25 | extract _ = Nothing | ||
| 26 | insert = Right | ||
| 27 | |||
| 28 | instance {-# OVERLAPS #-} Contains h n => Contains (a :||: h) n where | ||
| 29 | extract (Right a) = extract a | ||
| 30 | extract _ = Nothing | ||
| 31 | insert = Right . insert | ||
| 32 | |||
| 33 | test :: Maybe String | ||
| 34 | test = extract (insert "test" :: String :||: Integer :||: Float) | ||
