-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSizedMain.hs
157 lines (131 loc) · 3.83 KB
/
SizedMain.hs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import Sized
import Data.Proxy
import qualified Prelude as P
-- import Prelude hiding ((>>=), (>>), return, (<*>), (<$>), fmap, pure)
import GHC.TypeLits
import Prelude (fail, Functor, IO, ($), putStrLn, id)
fmap :: (SizedFunctor f) => (a -> b) -> f i a -> f i b
fmap = sfmap
(<$>) :: (SizedFunctor f) => (a -> b) -> f i a -> f i b
(<$>) = fmap
(<*>) :: (SizedApplicative f, k ~ Max i j) => f i (a -> b) -> f j a -> f k b
(<*>) = (|<*>)
pure :: (SizedApplicative f) => a -> f 0 a
pure = spure
return :: SizedMonad m => a -> m 0 a
return = sreturn
(>>=) :: SizedMonad m => m i a -> (a -> m j b) -> m (i + j) b
(>>=) = (|>>=)
(*>) :: (SizedApplicative f) => f s a -> f 0 b -> f s b
(*>) = (|*>)
-- For regular applicatives, (>>) = (*>). But for us,
-- (>>) :: (SizedMonad m) => m i a -> m j b -> m (i+j) b
-- but
-- (*>) :: (SizedApplicative f) => f s a -> f 0 b -> f s b
-- If ApplicativeDo is on, it desugars to do {one (); two (); return ()} to
-- fmap (\_ _ -> return ()) <*> (one () >> return ()) <*> (two () >> return ())
-- using -- (>>) and not (*>), as it should. I.e. the expected output should be
-- fmap (\_ _ -> return ()) <*> (one () *> pure ()) <*> (two () *> pure ())
-- It usually works, but in our case (|*>) and (|>>) aren't actually equivalent!
(>>) = (*>)
-- This works, but
t1a :: SizedIO 3 ()
t1a = one () |>> two ()
t1b :: SizedIO 2 ()
t1b = do {one (); two (); return ();}
-- This is wrong
-- t1b :: SizedIO 3 ()
-- t1b = one () >> two ()
one :: () -> SizedIO 1 ()
one _ = wrapWithCost (Proxy :: Proxy 1) $ putStrLn "one"
two :: () -> SizedIO 2 ()
two _ = wrapWithCost (Proxy :: Proxy 2) $ putStrLn "two"
three :: () -> SizedIO 3 ()
three _ = wrapWithCost (Proxy :: Proxy 3) $ putStrLn "three"
t1 :: SizedIO 6 ((),(),())
t1 = do { x <- one ();
y <- two x;
z <- three y;
return (x,y,z);
}
t2 :: SizedIO 3 ((),(),())
t2 = do {x <- one ();
y <- two ();
z <- three ();
return (x,y,z)
}
t3 :: SizedIO 5 ((), (), ())
t3 = isAtMost (Proxy :: Proxy 5) t2
t4 :: SizedIO 3 ()
t4 = do { one (); two (); three (); return ()}
-- Desugars to
{- t4 =
<*>
$dSizedApplicative_a168
($d~_a28U `cast` ...)
(<*>
$dSizedApplicative_a168
($d~_a28P `cast` ...)
(fmap
$dSizedFunctor_a15J
(\ ds_d2k7 ds_d2k8 ds_d2k9 ->
case ds_d2k7 of _ { () ->
case ds_d2k8 of _ { () -> case ds_d2k9 of _ { () -> () } }
})
((>>
$dSizedMonad_a15s
($f~kab (Eq# @~ ...))
$dKnownNat_a17e
(one ())
(return $fMonadSizedT ()))
`cast` ...))
((>>
$dSizedMonad_a15s
($f~kab (Eq# @~ ...))
$dKnownNat_a17e
(two ())
(return $fMonadSizedT ()))
`cast` ...))
((>>
$dSizedMonad_a15s
($f~kab (Eq# @~ ...))
$dKnownNat_a17e
(three ())
(return $fMonadSizedT ()))
`cast` ...)
-}
--
-- -- This one doesn't fail!
-- t4 :: SizedIO 3 ()
-- t4 = do { _ <- one (); _ <- two (); _ <- three (); return ()}
--
-- The latter desugars to
{-
t4 =
<*>
$dSizedApplicative_a15d
($d~_a18H `cast` ...)
(<*>
$dSizedApplicative_a15d
($d~_a18K `cast` ...)
(fmap $dSizedFunctor_a14O (\ _ _ _ -> ()) (one ()))
(two ()))
(three ())
-}
void :: Functor f => f a -> f ()
void x = () P.<$ x
main :: IO ()
main = void $ runSizedT $ t4