From 29aaba9386be26d0855c42cbc83d9ba1b9ac26c5 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 11 Dec 2015 14:50:03 +0100 Subject: somewhat better name --- genEither.hs | 32 -------------------------------- nestedEither.hs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 32 deletions(-) delete mode 100644 genEither.hs create mode 100644 nestedEither.hs diff --git a/genEither.hs b/genEither.hs deleted file mode 100644 index 4f6f7ae..0000000 --- a/genEither.hs +++ /dev/null @@ -1,32 +0,0 @@ -#! /usr/bin/env nix-shell -#! nix-shell -i ghci shell.nix - -{-# LANGUAGE TypeOperators #-} -{-# LANGUAGE MultiParamTypeClasses #-} -{-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE TypeSynonymInstances #-} - -type (:||:) = Either -infixr 1 :||: - -class Contains h n where - extract :: h -> Maybe n - insert :: n -> h - -instance Contains (a :||: b) a where - extract (Left a) = Just a - extract _ = Nothing - insert = Left - -instance Contains (a :||: b) b where - extract (Right a) = Just a - extract _ = Nothing - insert = Right - -instance {-# OVERLAPS #-} Contains h n => Contains (a :||: h) n where - extract (Right a) = extract a - extract _ = Nothing - insert = Right . insert - -test :: Maybe String -test = extract (insert "test" :: String :||: Integer :||: Float) diff --git a/nestedEither.hs b/nestedEither.hs new file mode 100644 index 0000000..64b9412 --- /dev/null +++ b/nestedEither.hs @@ -0,0 +1,34 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i ghci shell.nix + +-- Type indexed nested Eithers + +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE MultiParamTypeClasses #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE TypeSynonymInstances #-} + +type (:||:) = Either +infixr 1 :||: + +class Contains h n where + extract :: h -> Maybe n + insert :: n -> h + +instance Contains (a :||: b) a where + extract (Left a) = Just a + extract _ = Nothing + insert = Left + +instance Contains (a :||: b) b where + extract (Right a) = Just a + extract _ = Nothing + insert = Right + +instance {-# OVERLAPS #-} Contains h n => Contains (a :||: h) n where + extract (Right a) = extract a + extract _ = Nothing + insert = Right . insert + +test :: Maybe String +test = extract (insert "test" :: String :||: Integer :||: Float) -- cgit v1.2.3