Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 17407c0

Browse files
author
Patrick Thomson
committed
Initial port from machines to streaming.
1 parent 669ee58 commit 17407c0

File tree

13 files changed

+148
-114
lines changed

13 files changed

+148
-114
lines changed

semantic.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ common dependencies
5656
, fused-effects-exceptions ^>= 0.1.1.0
5757
, hashable ^>= 1.2.7.0
5858
, tree-sitter ^>= 0.1.0.0
59-
, machines ^>= 0.6.4
6059
, mtl ^>= 2.2.2
6160
, network ^>= 2.8.0.0
6261
, process ^>= 1.6.3.0
@@ -65,6 +64,7 @@ common dependencies
6564
, safe-exceptions ^>= 0.1.7.0
6665
, semilattices ^>= 0.0.0.3
6766
, shelly >= 1.5 && <2
67+
, streaming ^>= 0.2.2.0
6868
, text ^>= 1.2.3.1
6969
, these >= 0.7 && <1
7070
, unix ^>= 2.7.2.2

src/Data/Reprinting/Fragment.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ module Data.Reprinting.Fragment
77
, defer
88
) where
99

10-
import Data.Machine
1110
import Data.Text (Text)
11+
import Streaming
12+
import Streaming.Prelude (yield)
1213

1314
import Data.Reprinting.Scope
1415
import Data.Reprinting.Token
@@ -25,13 +26,13 @@ data Fragment
2526
deriving (Eq, Show)
2627

2728
-- | Copy along some original, un-refactored 'Text'.
28-
copy :: Text -> Plan k Fragment ()
29+
copy :: Monad m => Text -> Stream (Of Fragment) m ()
2930
copy = yield . Verbatim
3031

3132
-- | Insert some new 'Text'.
32-
insert :: Element -> [Scope] -> Text -> Plan k Fragment ()
33+
insert :: Monad m => Element -> [Scope] -> Text -> Stream (Of Fragment) m ()
3334
insert el c = yield . New el c
3435

3536
-- | Defer processing an element to a later stage.
36-
defer :: Element -> [Scope] -> Plan k Fragment ()
37+
defer :: Monad m => Element -> [Scope] -> Stream (Of Fragment) m ()
3738
defer el = yield . Defer el

src/Data/Reprinting/Splice.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module Data.Reprinting.Splice
1818

1919
import Prologue hiding (Element)
2020

21-
import Data.Machine
21+
import Streaming
22+
import Streaming.Prelude (yield)
2223

2324
import Data.Reprinting.Fragment
2425

@@ -29,29 +30,29 @@ data Splice
2930
deriving (Eq, Show)
3031

3132
-- | Emit some 'Text' as a 'Splice'.
32-
emit :: Text -> Plan k Splice ()
33+
emit :: Monad m => Text -> Stream (Of Splice) m ()
3334
emit = yield . Emit
3435

3536
-- | Emit the provided 'Text' if the given predicate is true.
36-
emitIf :: Bool -> Text -> Plan k Splice ()
37+
emitIf :: Monad m => Bool -> Text -> Stream (Of Splice) m ()
3738
emitIf p = when p . emit
3839

3940
-- | Construct a layout 'Splice'.
40-
layout :: Whitespace -> Plan k Splice ()
41+
layout :: Monad m => Whitespace -> Stream (Of Splice) m ()
4142
layout = yield . Layout
4243

4344
-- | @indent w n@ emits @w@ 'Spaces' @n@ times.
44-
indent :: Int -> Int -> Plan k Splice ()
45+
indent :: Monad m => Int -> Int -> Stream (Of Splice) m ()
4546
indent width times
4647
| times > 0 = replicateM_ times (layout (Indent width Spaces))
4748
| otherwise = pure ()
4849

4950
-- | Construct multiple layouts.
50-
layouts :: [Whitespace] -> Plan k Splice ()
51+
layouts :: Monad m => [Whitespace] -> Stream (Of Splice) m ()
5152
layouts = traverse_ (yield . Layout)
5253

5354
-- | Single space.
54-
space :: Plan k Splice ()
55+
space :: Monad m => Stream (Of Splice) m ()
5556
space = yield (Layout Space)
5657

5758
-- | Indentation, spacing, and other whitespace.

src/Language/JSON/PrettyPrint.hs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import Prologue
1111
import Control.Effect
1212
import Control.Effect.Error
1313
import Control.Monad.Trans (lift)
14-
import Data.Machine
14+
import Streaming
15+
import qualified Streaming.Prelude as Streaming
1516

1617
import Data.Reprinting.Errors
1718
import Data.Reprinting.Splice
@@ -20,16 +21,19 @@ import Data.Reprinting.Scope
2021

2122
-- | Default printing pipeline for JSON.
2223
defaultJSONPipeline :: (Member (Error TranslationError) sig, Carrier sig m)
23-
=> ProcessT m Fragment Splice
24+
=> Stream (Of Fragment) m a
25+
-> Stream (Of Splice) m a
2426
defaultJSONPipeline
25-
= printingJSON
26-
~> beautifyingJSON defaultBeautyOpts
27+
= beautifyingJSON defaultBeautyOpts
28+
. printingJSON
2729

2830
-- | Print JSON syntax.
29-
printingJSON :: Monad m => ProcessT m Fragment Fragment
30-
printingJSON = repeatedly (await >>= step) where
31+
printingJSON :: Monad m
32+
=> Stream (Of Fragment) m a
33+
-> Stream (Of Fragment) m a
34+
printingJSON = Streaming.map step where
3135
step s@(Defer el cs) =
32-
let ins = yield . New el cs
36+
let ins = New el cs
3337
in case (el, listToMaybe cs) of
3438
(Truth True, _) -> ins "true"
3539
(Truth False, _) -> ins "false"
@@ -44,8 +48,8 @@ printingJSON = repeatedly (await >>= step) where
4448
(Sep, Just Pair) -> ins ":"
4549
(Sep, Just Hash) -> ins ","
4650

47-
_ -> yield s
48-
step x = yield x
51+
_ -> s
52+
step x = x
4953

5054
-- TODO: Fill out and implement configurable options like indentation count,
5155
-- tabs vs. spaces, etc.
@@ -57,8 +61,10 @@ defaultBeautyOpts = JSONBeautyOpts 2 False
5761

5862
-- | Produce JSON with configurable whitespace and layout.
5963
beautifyingJSON :: (Member (Error TranslationError) sig, Carrier sig m)
60-
=> JSONBeautyOpts -> ProcessT m Fragment Splice
61-
beautifyingJSON _ = repeatedly (await >>= step) where
64+
=> JSONBeautyOpts
65+
-> Stream (Of Fragment) m a
66+
-> Stream (Of Splice) m a
67+
beautifyingJSON _ s = Streaming.for s step where
6268
step (Defer el cs) = lift (throwError (NoTranslation el cs))
6369
step (Verbatim txt) = emit txt
6470
step (New el cs txt) = case (el, cs) of
@@ -71,8 +77,9 @@ beautifyingJSON _ = repeatedly (await >>= step) where
7177

7278
-- | Produce whitespace minimal JSON.
7379
minimizingJSON :: (Member (Error TranslationError) sig, Carrier sig m)
74-
=> ProcessT m Fragment Splice
75-
minimizingJSON = repeatedly (await >>= step) where
80+
=> Stream (Of Fragment) m a
81+
-> Stream (Of Splice) m a
82+
minimizingJSON s = Streaming.for s step where
7683
step (Defer el cs) = lift (throwError (NoTranslation el cs))
7784
step (Verbatim txt) = emit txt
7885
step (New _ _ txt) = emit txt

src/Language/Python/PrettyPrint.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Language.Python.PrettyPrint ( printingPython ) where
55
import Control.Effect
66
import Control.Effect.Error
77
import Control.Monad.Trans (lift)
8-
import Data.Machine
8+
import Streaming
9+
import qualified Streaming.Prelude as Streaming
910

1011
import Data.Reprinting.Errors
1112
import Data.Reprinting.Splice
@@ -14,10 +15,12 @@ import Data.Reprinting.Scope
1415
import Data.Reprinting.Operator
1516

1617
-- | Print Python syntax.
17-
printingPython :: (Member (Error TranslationError) sig, Carrier sig m) => ProcessT m Fragment Splice
18-
printingPython = repeatedly (await >>= step)
18+
printingPython :: (Member (Error TranslationError) sig, Carrier sig m)
19+
=> Stream (Of Fragment) m a
20+
-> Stream (Of Splice) m a
21+
printingPython s = Streaming.for s step
1922

20-
step :: (Member (Error TranslationError) sig, Carrier sig m) => Fragment -> PlanT k Splice m ()
23+
step :: (Member (Error TranslationError) sig, Carrier sig m) => Fragment -> Stream (Of Splice) m ()
2124
step (Verbatim txt) = emit txt
2225
step (New _ _ txt) = emit txt
2326
step (Defer el cs) = case (el, cs) of

src/Language/Ruby/PrettyPrint.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module Language.Ruby.PrettyPrint ( printingRuby ) where
55
import Control.Effect
66
import Control.Effect.Error
77
import Control.Monad.Trans (lift)
8-
import Data.Machine
8+
import Streaming
9+
import qualified Streaming.Prelude as Streaming
910

1011
import Data.Reprinting.Scope
1112
import Data.Reprinting.Errors
@@ -14,10 +15,14 @@ import Data.Reprinting.Splice
1415
import Data.Reprinting.Token as Token
1516

1617
-- | Print Ruby syntax.
17-
printingRuby :: (Member (Error TranslationError) sig, Carrier sig m) => ProcessT m Fragment Splice
18-
printingRuby = repeatedly (await >>= step)
19-
20-
step :: (Member (Error TranslationError) sig, Carrier sig m) => Fragment -> PlanT k Splice m ()
18+
printingRuby :: (Member (Error TranslationError) sig, Carrier sig m)
19+
=> Stream (Of Fragment) m a
20+
-> Stream (Of Splice) m a
21+
printingRuby s = Streaming.for s step
22+
23+
step :: (Member (Error TranslationError) sig, Carrier sig m)
24+
=> Fragment
25+
-> Stream (Of Splice) m ()
2126
step (Verbatim txt) = emit txt
2227
step (New _ _ txt) = emit txt
2328
step (Defer el cs) = case (el, cs) of

src/Reprinting/Pipeline.hs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ stages of the pipeline follows:
9595
9696
-}
9797

98-
{-# LANGUAGE AllowAmbiguousTypes, ScopedTypeVariables, RankNTypes #-}
98+
{-# LANGUAGE AllowAmbiguousTypes, PartialTypeSignatures, RankNTypes, ScopedTypeVariables #-}
99+
{-# OPTIONS_GHC -Wno-partial-type-signatures #-}
99100
module Reprinting.Pipeline
100101
( runReprinter
101102
, runTokenizing
@@ -106,10 +107,10 @@ module Reprinting.Pipeline
106107
import Control.Effect as Effect
107108
import Control.Effect.Error as Effect
108109
import Control.Effect.State as Effect
109-
import Data.Machine hiding (Source)
110-
import Data.Machine.Runner
111110
import Data.Text.Prettyprint.Doc
112111
import Data.Text.Prettyprint.Doc.Render.Text
112+
import Streaming
113+
import qualified Streaming.Prelude as Streaming
113114

114115
import Data.Reprinting.Errors
115116
import Data.Reprinting.Scope
@@ -121,57 +122,58 @@ import Reprinting.Tokenize
121122
import Reprinting.Translate
122123
import Reprinting.Typeset
123124

124-
125-
-- | Run the reprinting pipeline given the original 'Source', a language
126-
-- specific machine (`ProcessT`) and the provided 'Term'.
125+
-- | Run the reprinting pipeline given the original 'Source', a language specific
126+
-- translation function (as a function over 'Stream's) and the provided 'Term'.
127127
runReprinter :: Tokenize a
128-
=> Source.Source
129-
-> ProcessT Translator Fragment Splice
130-
-> Term a History
131-
-> Either TranslationError Source.Source
132-
runReprinter src translating tree
128+
=> Source.Source
129+
-> (Stream (Of Fragment) _ () -> Stream (Of Splice) _ ())
130+
-> Term a History
131+
-> Either TranslationError Source.Source
132+
runReprinter src translating
133133
= fmap go
134134
. Effect.run
135135
. Effect.runError
136-
. fmap snd
137-
. runState (mempty :: [Scope])
138-
. foldT $ source (tokenizing src tree)
139-
~> contextualizing
140-
~> translating
141-
~> typesetting
136+
. evalState @[Scope] mempty
137+
. Streaming.mconcat_
138+
. typesetting
139+
. translating
140+
. contextualizing
141+
. tokenizing src
142142
where go = Source.fromText . renderStrict . layoutPretty defaultLayoutOptions
143143

144144
-- | Run the reprinting pipeline up to tokenizing.
145145
runTokenizing :: Tokenize a
146-
=> Source.Source
147-
-> Term a History
148-
-> [Token]
149-
runTokenizing src tree
150-
= Data.Machine.run $ source (tokenizing src tree)
146+
=> Source.Source
147+
-> Term a History
148+
-> [Token]
149+
runTokenizing src
150+
= runIdentity
151+
. Streaming.toList_
152+
. tokenizing src
151153

152154
-- | Run the reprinting pipeline up to contextualizing.
153155
runContextualizing :: Tokenize a
154156
=> Source.Source
155157
-> Term a History
156158
-> Either TranslationError [Fragment]
157-
runContextualizing src tree
159+
runContextualizing src
158160
= Effect.run
159161
. Effect.runError
160-
. fmap snd
161-
. runState (mempty :: [Scope])
162-
. runT $ source (tokenizing src tree)
163-
~> contextualizing
162+
. evalState @[Scope] mempty
163+
. Streaming.toList_
164+
. contextualizing
165+
. tokenizing src
164166

165167
runTranslating :: Tokenize a
166168
=> Source.Source
167-
-> ProcessT Translator Fragment Splice
169+
-> (Stream (Of Fragment) _ () -> Stream (Of Splice) _ ())
168170
-> Term a History
169171
-> Either TranslationError [Splice]
170-
runTranslating src translating tree
172+
runTranslating src translating
171173
= Effect.run
172174
. Effect.runError
173-
. fmap snd
174-
. runState (mempty :: [Scope])
175-
. runT $ source (tokenizing src tree)
176-
~> contextualizing
177-
~> translating
175+
. evalState @[Scope] mempty
176+
. Streaming.toList_
177+
. translating
178+
. contextualizing
179+
. tokenizing src

src/Reprinting/Tokenize.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ module Reprinting.Tokenize
2929

3030
import Prelude hiding (fail, log, filter)
3131
import Prologue hiding (Element, hash)
32+
import Streaming hiding (Sum)
33+
import qualified Streaming.Prelude as Streaming
3234

3335
import Data.History
3436
import Data.List (intersperse)
35-
import qualified Data.Machine as Machine
3637
import Data.Range
3738
import Data.Reprinting.Scope (Scope)
3839
import qualified Data.Reprinting.Scope as Scope
@@ -55,15 +56,14 @@ data Tokenizer a where
5556
Get :: Tokenizer State
5657
Put :: State -> Tokenizer ()
5758

58-
-- Tokenizers are compiled into a Plan capable of being converted
59-
-- to a Source. Note that the state parameter is internal to the
60-
-- tokenizer being run: the invoker of 'tokenizing' doesn't need
61-
-- to keep track of it at all.
62-
compile :: State -> Tokenizer a -> Machine.Plan k Token (State, a)
59+
-- Tokenizers are compiled directly into Stream values. Note that the
60+
-- state parameter is internal to the tokenizer being run: the invoker
61+
-- of 'tokenizing' doesn't need to keep track of it at all.
62+
compile :: Monad m => State -> Tokenizer a -> Stream (Of Token) m (State, a)
6363
compile p = \case
6464
Pure a -> pure (p, a)
6565
Bind a f -> compile p a >>= (\(new, v) -> compile new (f v))
66-
Tell t -> Machine.yield t $> (p, ())
66+
Tell t -> Streaming.yield t $> (p, ())
6767
Get -> pure (p, p)
6868
Put p' -> pure (p', ())
6969

@@ -229,12 +229,12 @@ class (Show1 constr, Traversable constr) => Tokenize constr where
229229
-- | Should emit control and data tokens.
230230
tokenize :: FAlgebra constr (Tokenizer ())
231231

232-
tokenizing :: Tokenize a
232+
tokenizing :: (Monad m, Tokenize a)
233233
=> Source
234234
-> Term a History
235-
-> Machine.Source Token
235+
-> Stream (Of Token) m ()
236236
tokenizing src term = pipe
237-
where pipe = Machine.construct . fmap snd $ compile state go
237+
where pipe = fmap snd $ compile state go
238238
state = State src (termAnnotation term) Reprinting 0 ForbidData
239239
go = forbidData *> foldSubterms descend term <* finish
240240

0 commit comments

Comments
 (0)