Ticket #2664: overflow.hs

File overflow.hs, 1.0 KB (added by ryani, 5 years ago)
Line 
1
2{-# LANGUAGE EmptyDataDecls, TypeFamilies, TypeOperators, ScopedTypeVariables #-}
3module Overflow where
4import Control.Concurrent
5
6data (:*:) a b
7data (:+:) a b
8
9data family PChan a
10data    instance PChan (a :+: b) = E (IO (PChan a)) (IO (PChan b))
11newtype instance PChan (a :*: b) = O (IO (Either (PChan a) (PChan b)))
12
13type family Dual a
14type instance Dual (a :+: b) = Dual a :*: Dual b
15type instance Dual (a :*: b) = Dual a :+: Dual b
16
17class Connect s where
18    newPChan :: (s ~ Dual c, c ~ Dual s) => IO (PChan s, PChan c)
19
20pchoose :: (t -> a) -> MVar a -> IO (t,b) -> IO b
21pchoose = undefined
22
23instance (Connect a, Connect b) => Connect (a :*: b) where
24    newPChan = do
25        v <- newEmptyMVar
26        -- correct implementation:
27        -- return (O $ takeMVar v, E (pchoose Left v newPChan) (pchoose Right v newPChan))
28       
29        -- type error leads to stack overflow (even without UndecidableInstances!)
30        return (O $ takeMVar v, E (pchoose Right v newPChan) (pchoose Left v newPChan))