diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2015-12-11 14:50:03 +0100 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2015-12-11 14:50:03 +0100 |
| commit | 29aaba9386be26d0855c42cbc83d9ba1b9ac26c5 (patch) | |
| tree | 6bab6e8b65589b86c0aa86df7f5852ab41b1bde5 /nestedEither.hs | |
| parent | eefadef0e9d0e34cec75680fddcb4237a2d7c695 (diff) | |
| download | feeds-type-experiments.tar feeds-type-experiments.tar.gz feeds-type-experiments.tar.bz2 feeds-type-experiments.tar.xz feeds-type-experiments.zip | |
somewhat better nametype-experiments
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) | ||
