summaryrefslogtreecommitdiff
path: root/genEither.hs
blob: 4f6f7ae0c2c6e3f4607dfff3e4adb39415ad0ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#! /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)