Skip to content

Commit 1b06cbe

Browse files
committed
Updates for 1.0 core libraries
1 parent 5cbad13 commit 1b06cbe

File tree

4 files changed

+43
-57
lines changed

4 files changed

+43
-57
lines changed

bower.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
"url": "git://github.com/paf31/purescript-freet.git"
1515
},
1616
"dependencies": {
17-
"purescript-console": "^0.1.0",
18-
"purescript-control": "~0.3.0",
19-
"purescript-tailrec": "~0.3.0",
20-
"purescript-transformers": "~0.8.1",
21-
"purescript-exists": "~0.2.0"
17+
"purescript-console": "^1.0.0-rc.1",
18+
"purescript-control": "^1.0.0-rc.1",
19+
"purescript-tailrec": "^1.0.0-rc.1",
20+
"purescript-transformers": "^1.0.0-rc.1",
21+
"purescript-exists": "^1.0.0-rc.1"
2222
}
2323
}

docs/Control/Monad/Free/Trans.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ The free monad transformer for the functor `f`.
1212

1313
##### Instances
1414
``` purescript
15-
instance functorFreeT :: (Functor f, Functor m) => Functor (FreeT f m)
16-
instance applyFreeT :: (Functor f, Monad m) => Apply (FreeT f m)
17-
instance applicativeFreeT :: (Functor f, Monad m) => Applicative (FreeT f m)
18-
instance bindFreeT :: (Functor f, Monad m) => Bind (FreeT f m)
19-
instance monadFreeT :: (Functor f, Monad m) => Monad (FreeT f m)
20-
instance monadTransFreeT :: (Functor f) => MonadTrans (FreeT f)
21-
instance monadRecFreeT :: (Functor f, Monad m) => MonadRec (FreeT f m)
15+
(Functor f, Functor m) => Functor (FreeT f m)
16+
(Functor f, Monad m) => Apply (FreeT f m)
17+
(Functor f, Monad m) => Applicative (FreeT f m)
18+
(Functor f, Monad m) => Bind (FreeT f m)
19+
(Functor f, Monad m) => Monad (FreeT f m)
20+
(Functor f) => MonadTrans (FreeT f)
21+
(Functor f, Monad m) => MonadRec (FreeT f m)
2222
```
2323

2424
#### `freeT`
@@ -48,23 +48,23 @@ Lift an action from the functor `f` to a `FreeT` action.
4848
#### `hoistFreeT`
4949

5050
``` purescript
51-
hoistFreeT :: forall f m n a. (Functor f, Functor n) => (forall a. m a -> n a) -> FreeT f m a -> FreeT f n a
51+
hoistFreeT :: forall f m n a. (Functor f, Functor n) => (forall b. m b -> n b) -> FreeT f m a -> FreeT f n a
5252
```
5353

5454
Change the underlying `Monad` for a `FreeT` action.
5555

5656
#### `interpret`
5757

5858
``` purescript
59-
interpret :: forall f g m a. (Functor f, Functor m) => (forall a. f a -> g a) -> FreeT f m a -> FreeT g m a
59+
interpret :: forall f g m a. (Functor f, Functor m) => (forall b. f b -> g b) -> FreeT f m a -> FreeT g m a
6060
```
6161

6262
Change the base functor `f` for a `FreeT` action.
6363

6464
#### `bimapFreeT`
6565

6666
``` purescript
67-
bimapFreeT :: forall f g m n a. (Functor f, Functor n) => (forall a. f a -> g a) -> (forall a. m a -> n a) -> FreeT f m a -> FreeT g n a
67+
bimapFreeT :: forall f g m n a. (Functor f, Functor n) => (forall b. f b -> g b) -> (forall b. m b -> n b) -> FreeT f m a -> FreeT g n a
6868
```
6969

7070
Change the base functor `f` and the underlying `Monad` for a `FreeT` action.

src/Control/Monad/Free/Trans.purs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import Data.Either (Either(..))
1818
import Data.Bifunctor (bimap)
1919

2020
import Control.Bind ((<=<))
21-
import Control.Monad.Rec.Class (MonadRec, tailRecM)
22-
import Control.Monad.Trans (MonadTrans)
21+
import Control.Monad.Rec.Class (class MonadRec, tailRecM)
22+
import Control.Monad.Trans (class MonadTrans)
2323

2424
-- | Instead of implementing `bind` directly, we capture the bind using this data structure, to
2525
-- | evaluate later.
@@ -42,14 +42,14 @@ resume = tailRecM go
4242
where
4343
go :: FreeT f m a -> m (Either (FreeT f m a) (Either a (f (FreeT f m a))))
4444
go (FreeT f) = map Right (f unit)
45-
go (Bind e) = runExists (\(Bound m f) ->
46-
case m unit of
45+
go (Bind e) = runExists (\(Bound bound f) ->
46+
case bound unit of
4747
FreeT m -> do
4848
e <- m unit
4949
case e of
50-
Left a -> return (Left (f a))
51-
Right fc -> return (Right (Right (map (\h -> h >>= f) fc)))
52-
Bind e1 -> runExists (\(Bound m1 f1) -> return (Left (bind (m1 unit) (\z -> f1 z >>= f)))) e1) e
50+
Left a -> pure (Left (f a))
51+
Right fc -> pure (Right (Right (map (\h -> h >>= f) fc)))
52+
Bind e1 -> runExists (\(Bound m1 f1) -> pure (Left (bind (m1 unit) (\z -> f1 z >>= f)))) e1) e
5353

5454
instance functorFreeT :: (Functor f, Functor m) => Functor (FreeT f m) where
5555
map f (FreeT m) = FreeT \_ -> map (bimap f (map (map f))) (m unit)
@@ -77,11 +77,11 @@ instance monadRecFreeT :: (Functor f, Monad m) => MonadRec (FreeT f m) where
7777
e <- f s
7878
case e of
7979
Left s1 -> go s1
80-
Right a -> return a
80+
Right a -> pure a
8181

8282
-- | Lift an action from the functor `f` to a `FreeT` action.
8383
liftFreeT :: forall f m a. (Functor f, Monad m) => f a -> FreeT f m a
84-
liftFreeT fa = FreeT \_ -> return (Right (map pure fa))
84+
liftFreeT fa = FreeT \_ -> pure (Right (map pure fa))
8585

8686
-- | Change the underlying `Monad` for a `FreeT` action.
8787
hoistFreeT :: forall f m n a. (Functor f, Functor n) => (forall b. m b -> n b) -> FreeT f m a -> FreeT f n a
@@ -101,7 +101,7 @@ runFreeT :: forall f m a. (Functor f, MonadRec m) => (f (FreeT f m a) -> m (Free
101101
runFreeT interp = tailRecM (go <=< resume)
102102
where
103103
go :: Either a (f (FreeT f m a)) -> m (Either (FreeT f m a) a)
104-
go (Left a) = return (Right a)
104+
go (Left a) = pure (Right a)
105105
go (Right fc) = do
106106
c <- interp fc
107-
return (Left c)
107+
pure (Left c)

test/Main.purs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,36 @@ import Control.Apply
66
import Control.Monad.Eff
77
import Control.Monad.Eff.Class
88
import Control.Monad.Eff.Console
9-
import Control.Monad.Aff
109
import Control.Monad.Trans
1110
import Control.Monad.Free.Trans
1211
import Control.Monad.Rec.Class
1312

14-
import Node.ReadLine
15-
16-
data TeletypeF a = WriteLine String a | ReadLine (String -> a)
13+
data TeletypeF a
14+
= WriteLine String a
15+
| ReadLine (String -> a)
1716

1817
instance functorTeletypeF :: Functor TeletypeF where
1918
map f (WriteLine s a) = WriteLine s (f a)
20-
map f (ReadLine k) = ReadLine (f <<< k)
19+
map f (ReadLine k) = ReadLine (f <<< k)
2120

2221
type Teletype = FreeT TeletypeF
2322

24-
writeLine :: forall m. (Monad m) => String -> FreeT TeletypeF m Unit
23+
writeLine :: forall m. Monad m => String -> FreeT TeletypeF m Unit
2524
writeLine s = liftFreeT (WriteLine s unit)
2625

27-
readLine :: forall m. (Monad m) => FreeT TeletypeF m String
26+
readLine :: forall m. Monad m => FreeT TeletypeF m String
2827
readLine = liftFreeT (ReadLine id)
2928

30-
runTeletype :: forall a eff. Teletype (Aff (console :: CONSOLE | eff)) a -> Aff (console :: CONSOLE | eff) a
31-
runTeletype = runFreeT interp
29+
mockTeletype :: forall a eff. Teletype (Eff (console :: CONSOLE | eff)) a -> Eff (console :: CONSOLE | eff) a
30+
mockTeletype = runFreeT interp
3231
where
33-
interp :: TeletypeF (Teletype (Aff (console :: CONSOLE | eff)) a) ->
34-
Aff (console :: CONSOLE | eff) (Teletype (Aff (console :: CONSOLE | eff)) a)
35-
interp (WriteLine s next) = do
36-
liftEff (log s)
37-
return next
38-
interp (ReadLine k) = do
39-
s <- readLine
40-
return (k s)
41-
42-
readLine :: forall eff. Aff (console :: CONSOLE | eff) String
43-
readLine = makeAff \_ k -> void do
44-
interface <- createInterface noCompletion
45-
setPrompt "> " 2 interface
46-
setLineHandler interface \s -> close interface *> k s
47-
line <- prompt interface
48-
return line
49-
50-
main = runAff print return $ runTeletype $ forever do
32+
interp (WriteLine s next) = do
33+
liftEff (log s)
34+
pure next
35+
interp (ReadLine k) = do
36+
pure (k "Fake input")
37+
38+
main = mockTeletype $ forever do
39+
lift $ log "Enter some input:"
5140
s <- readLine
52-
lift $ do
53-
liftEff $ log "Please wait..."
54-
later' 1000 $ return unit
55-
writeLine ("You typed: " ++ s)
41+
writeLine ("You typed: " <> s)

0 commit comments

Comments
 (0)