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 --- nestedEither.hs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 nestedEither.hs (limited to 'nestedEither.hs') 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