git.iko.soy/rowtype-yoga/purescm
on bytevector help sign in
Add conversion for uncurried effect functions (#109)
commita65fdd2235376855083d2739809065694c91fa50 authorJustin Garcia <purefunctor@pm.me> dateThu 6 Apr 2023, 21:41 (4 years ago) parenta2c3f76
7 files changed+103−37
src/PureScript/Backend/Chez/Convert.purs+10−15 src/PureScript/Backend/Chez/Syntax.purs+5−1 test-snapshots/spago.yaml+16−1 test-snapshots/src/snapshots-input/Snapshot.UncurriedFunction.purs+33−2 test-snapshots/src/snapshots-output/Snapshot.UncurriedFunction.ss+19−5 test/Main.purs+16−12 test/Utils.purs+4−1
src/PureScript/Backend/Chez/Convert.purs+10−15
@@ -141,21 +141,16 @@ codegenExpr codegenEnv@{ currentModule } s = case unwrap s of
141141 S.chezCurriedApplication (codegenExpr codegenEnv f) (codegenExpr codegenEnv <$> p)
142142 Abs a e -> do
143143 S.chezCurriedFunction (uncurry S.toChezIdent <$> a) (codegenExpr codegenEnv e)
144−
145− UncurriedApp fn args ->
146− S.List $ Array.cons (codegenExpr codegenEnv fn) $
147− (codegenExpr codegenEnv <$> args)
148− UncurriedAbs args body ->
149− S.List
150− [ S.Identifier $ scmPrefixed "lambda"
151− , S.List $ (S.Identifier <<< uncurry S.toChezIdent) <$> args
152− , S.List [ codegenExpr codegenEnv body ]
153− ]
154−
155− UncurriedEffectApp _ _ ->
156− S.Identifier "uncurried-effect-app"
157− UncurriedEffectAbs _ _ ->
158− S.Identifier "uncurried-effect-ab"
144+ UncurriedApp f p ->
145+ S.chezUncurriedApplication (codegenExpr codegenEnv f) (codegenExpr codegenEnv <$> p)
146+ UncurriedAbs a e ->
147+ S.chezUncurriedFunction (uncurry S.toChezIdent <$> a) (codegenExpr codegenEnv e)
148+ UncurriedEffectApp f p ->
149+ S.chezThunk $ S.chezUncurriedApplication (codegenExpr codegenEnv f)
150+ (codegenExpr codegenEnv <$> p)
151+ UncurriedEffectAbs a e ->
152+ S.chezUncurriedFunction (uncurry S.toChezIdent <$> a)
153+ (codegenChain effectChainMode codegenEnv e)
159154
160155 Accessor e (GetProp i) ->
161156 S.chezUncurriedApplication
src/PureScript/Backend/Chez/Syntax.purs+5−1
@@ -439,7 +439,11 @@ chezCond b o =
439439 List $ [ Identifier $ scmPrefixed "cond" ] <> b' <> o'
440440
441441chezUncurriedApplication :: ChezExpr -> Prim.Array ChezExpr -> ChezExpr
442−chezUncurriedApplication f args = List $ Array.cons f args
442+chezUncurriedApplication f s = List $ Array.cons f s
443+
444+chezUncurriedFunction :: Prim.Array Prim.String -> ChezExpr -> ChezExpr
445+chezUncurriedFunction a e = List
446+ [ Identifier $ scmPrefixed "lambda", List $ Identifier <$> a, e ]
443447
444448chezCurriedApplication :: ChezExpr -> NonEmptyArray ChezExpr -> ChezExpr
445449chezCurriedApplication f s = NonEmptyArray.foldl1 app $ NonEmptyArray.cons f s
test-snapshots/spago.yaml+16−1
@@ -1,6 +1,8 @@
11package:
22 name: purescm-snapshots
33 dependencies:
4+ - assert
5+ - console
46 - effect
57 - functions
68 - partial
@@ -19,7 +21,7 @@ workspace:
1921 dependencies: []
2022 functions:
2123 git: https://github.com/purescm/purescript-functions.git
22− ref: a43d127384ab568575fb9ee9e4a7a1844b21d39b
24+ ref: 94e0a9a3b7f831abebcb1bf65587fca03f04e034
2325 dependencies:
2426 - prelude
2527 effect:
@@ -27,3 +29,16 @@ workspace:
2729 ref: 8aee2c2fbf5425bf8fa4537a5ad2070170622acf
2830 dependencies:
2931 - prelude
32+ console:
33+ git: https://github.com/purescm/purescript-console.git
34+ ref: 4a30427e490b6f38be16bb5e0bc8f68ecc1c9999
35+ dependencies:
36+ - effect
37+ - prelude
38+ assert:
39+ git: https://github.com/purescm/purescript-assert.git
40+ ref: 348b91f4542cf75efab232e8eb52dd5d3e6a9435
41+ dependencies:
42+ - console
43+ - effect
44+ - prelude
test-snapshots/src/snapshots-input/Snapshot.UncurriedFunction.purs+33−2
@@ -1,15 +1,46 @@
1+-- @inline Snapshot.UncurriedFunction.pure' never
12module Snapshot.UncurriedFunction where
23
3−import Data.Function.Uncurried (mkFn0, mkFn2, runFn0, runFn2)
4+import Prelude
45
6+import Data.Function.Uncurried (Fn0, Fn2, mkFn0, mkFn2, runFn0, runFn2)
7+import Data.Unit (Unit)
8+import Effect (Effect)
9+import Effect.Console (log)
10+import Effect.Uncurried (EffectFn1, mkEffectFn1, runEffectFn1)
11+import Test.Assert (assert)
12+
13+test1a :: Fn0 Int
514test1a = mkFn0 \_ -> 1
615
7−test1b = runFn0
16+test1b :: Int
17+test1b = runFn0 test1a
818
19+test2a :: forall a b. Fn2 a b a
920test2a = mkFn2 \a _ -> a
1021
22+test2b :: Int
1123test2b = runFn2 test2a 1 2
1224
25+test3a :: forall a b. Fn2 a b b
1326test3a = mkFn2 \_ b -> b
1427
28+test3b :: Int
1529test3b = runFn2 test3a 1 2
30+
31+test4a :: EffectFn1 String String
32+test4a = mkEffectFn1 $ \a -> do
33+ log a
34+ pure a
35+
36+test4b :: Effect String
37+test4b = runEffectFn1 test4a "test4b"
38+
39+main :: Effect Unit
40+main = do
41+ assert $ test1b == 1
42+ assert $ test2b == 1
43+ assert $ test3b == 2
44+
45+ v <- test4b
46+ assert $ v == "test4b"
test-snapshots/src/snapshots-output/Snapshot.UncurriedFunction.ss+19−5
@@ -3,16 +3,27 @@
33(library
44 (Snapshot.UncurriedFunction lib)
55 (export
6+ main
67 test1a
78 test1b
89 test2a
910 test2b
1011 test3a
11− test3b)
12+ test3b
13+ test4a
14+ test4b)
1215 (import
1316 (prefix (chezscheme) scm:)
1417 (prefix (_Chez_Runtime lib) rt:)
15− (prefix (Data.Function.Uncurried lib) Data.Function.Uncurried.))
18+ (prefix (Data.Function.Uncurried lib) Data.Function.Uncurried.)
19+ (prefix (Effect.Console lib) Effect.Console.)
20+ (prefix (Test.Assert lib) Test.Assert.))
21+
22+ (scm:define test4a
23+ (scm:lambda (a0) (scm:let* ((_1 (Effect.Console.log a0)) (_ (_1))) a0)))
24+
25+ (scm:define test4b
26+ (scm:lambda () (test4a "test4b")))
1627
1728 (scm:define test3a
1829 (scm:lambda (v0 b1)
@@ -28,8 +39,11 @@
2839 (scm:define test2b
2940 (test2a 1 2))
3041
42+ (scm:define test1a
43+ (Data.Function.Uncurried.mkFn0 (scm:lambda (v0) 1)))
44+
3145 (scm:define test1b
32− Data.Function.Uncurried.runFn0)
46+ (Data.Function.Uncurried.runFn0 test1a))
3347
34− (scm:define test1a
35− (Data.Function.Uncurried.mkFn0 (scm:lambda (v0) 1))))
48+ (scm:define main
49+ (scm:let* ((_0 (Test.Assert.assert (scm:fx=? test1b 1)))) (scm:lambda () (scm:let* ((_ (_0)) (_ ((Test.Assert.assert (scm:fx=? test2b 1)))) (_ ((Test.Assert.assert (scm:fx=? test3b 2)))) (v4 (test4b))) ((Test.Assert.assert (scm:string=? v4 "test4b"))))))))
test/Main.purs+16−12
@@ -159,16 +159,20 @@ runSnapshotTests { accept, filter } = do
159159 , moduleName: name
160160 }
161161 case result of
162− Left err | matchesFail err.message failsWith -> do
163− Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <> " failed."
164− Console.log err.message
165− pure false
166− Right _ | isJust failsWith -> do
167− Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <>
168− " succeeded when it should have failed."
169− pure false
170− _ ->
171− pure true
162+ Left { message }
163+ | matchesFail message failsWith ->
164+ pure true
165+ | otherwise -> do
166+ Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <> " failed."
167+ Console.log message
168+ pure false
169+ Right _
170+ | isJust failsWith -> do
171+ Console.log $ withGraphics (foreground Red) "✗" <> " " <> name <>
172+ " succeeded when it should have failed."
173+ pure false
174+ | otherwise ->
175+ pure true
172176 attempt (FS.readTextFile UTF8 snapshotFilePath) >>= case _ of
173177 Left _ -> do
174178 Console.log $ withGraphics (foreground Yellow) "✓" <> " " <> name <> " saved."
@@ -203,9 +207,9 @@ hasFails = findMap go <<< _.comments
203207matchesFail :: String -> Maybe String -> Boolean
204208matchesFail errMsg = case _ of
205209 Just msg ->
206− not $ String.contains (Pattern msg) errMsg
210+ String.contains (Pattern msg) errMsg
207211 Nothing ->
208− true
212+ false
209213
210214getSchemeBinary :: Aff String
211215getSchemeBinary = do
test/Utils.purs+4−1
@@ -94,7 +94,10 @@ loadModuleMain options = do
9494 spawned <- execa options.scheme arguments identity
9595 when options.hasMain do
9696 spawned.stdin.writeUtf8End $ Array.fold
97− [ "(import (", options.moduleName, " lib)) (main)" ]
97+ [ "(import ("
98+ , options.moduleName
99+ , " lib)) (with-exception-handler (lambda (e) (display-condition e (console-error-port)) (newline (console-error-port)) (exit -1)) (lambda () (main)))"
100+ ]
98101 spawned.result
99102
100103copyFile :: FilePath -> FilePath -> Aff Unit