git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
More Prelude work, run tests in nodejs
commitf1f9bc17db83cda1265f5e03128d98b7f148c677 authorPhil Freeman <paf31@cantab.net> dateSun 26 Jan 2014, 15:52 (13 years ago) parent5159dfd
55 files changed+607−281
examples/passing/ArrayType.purs+4 examples/passing/Arrays.purs+5−1 examples/passing/Assign.purs+4 examples/passing/Auto.purs+4 examples/passing/Church.purs+4 examples/passing/Conditional.purs+4 examples/passing/Cons.purs+6−3 examples/passing/Console.purs+13−25 examples/passing/DataAndType.purs+4 examples/passing/DataImport.purs+4 examples/passing/Do.purs+17−15 examples/passing/Dollar.purs+4 examples/passing/Eff.purs+18−62 examples/passing/ExternData.purs+5−1 examples/passing/ExternRaw.purs+4 examples/passing/FFI.purs+5−1 examples/passing/Functions.purs+4 examples/passing/Guards.purs+4 examples/passing/IfThenElse.purs+4 examples/passing/Import.purs+4 examples/passing/List.purs+3 examples/passing/Match.purs+4 examples/passing/Maybe.purs+9−9 examples/passing/Module.purs+4 examples/passing/ModuleDeps.purs+4 examples/passing/Monad.purs+4 examples/passing/MutRec.purs+4 examples/passing/NamedPatterns.purs+4 examples/passing/Nested.purs+4 examples/passing/ObjectUpdate.purs+4 examples/passing/Objects.purs+4 examples/passing/OneConstructor.purs+4 examples/passing/Operators.purs+26 examples/passing/OptimizerBug.purs+4 examples/passing/Patterns.purs+3 examples/passing/Person.purs+4 examples/passing/Rank2Data.purs+4 examples/passing/Rank2Types.purs+5−1 examples/passing/Recursion.purs+4 examples/passing/RedefinedFixity.purs+4 examples/passing/TailCall.purs+3 examples/passing/TopLevelCase.purs+4 examples/passing/TypeClasses.purs+11−7 examples/passing/TypeDecl.purs+4 examples/passing/TypeSynonyms.purs+13−2 examples/passing/iota.purs+4 examples/passing/s.purs+4 libraries/prelude/Makefile−3 libraries/prelude/prelude.js−42 libraries/prelude/prelude.purs+288−83 libraries/prelude/tests/.gitignore+1 libraries/prelude/tests/Eff.purs+11−11 libraries/prelude/tests/build.sh+2 purescript.cabal+1−1 tests/Main.hs+34−14
examples/passing/ArrayType.purs+4
@@ -11,3 +11,7 @@ foreign import (:) :: forall a. a -> [a] -> [a]
1111instance Functor [] where
1212 fmap _ [] = []
1313 fmap f (x:xs) = f x : fmap f xs
14+
15+module Main where
16+
17+main = Trace.trace "Done"
examples/passing/Arrays.purs+5−1
@@ -1,4 +1,4 @@
1−module Arrays where
1+module ArraysTests where
22
33test1 = \arr -> arr !! 0 + arr !! 1
44
@@ -18,4 +18,8 @@ test4 = \arr -> case arr of
1818 [] -> 0
1919 [_] -> 0
2020 x : y : xs -> x * y + test4 xs
21+
22+module Main where
23+
24+main = Trace.trace "Done"
2125
examples/passing/Assign.purs+4
@@ -7,3 +7,7 @@ module Assign where
77 }
88 return x;
99 }
10+
11+module Main where
12+
13+main = Trace.trace "Done"
examples/passing/Auto.purs+4
@@ -9,3 +9,7 @@ module Auto where
99
1010 run :: forall i o. SomeAuto i o -> i -> o
1111 run = \s i -> s (\a -> case a of Auto a -> a.step a.state i)
12+
13+module Main where
14+
15+main = Trace.trace "Done"
examples/passing/Church.purs+4
@@ -12,3 +12,7 @@ module Church where
1212 append = \l1 l2 r f -> l2 (l1 r f) f
1313
1414 test = append (cons 1 empty) (cons 2 empty)
15+
16+module Main where
17+
18+main = Trace.trace "Done"
examples/passing/Conditional.purs+4
@@ -5,3 +5,7 @@ module Conditional where
55 bin = \x -> if x && !x then x else x
66
77 not = \x -> if x then false else true
8+
9+module Main where
10+
11+main = Trace.trace "Done"
examples/passing/Cons.purs+6−3
@@ -1,9 +1,12 @@
11module Cons where
22
3− infixr 6 :
4−
5− foreign import (:) :: forall a. a -> [a] -> [a]
3+ import Prelude
4+ import Arrays
65
76 test1 = \xs -> 1 : xs
87
98 test2 = 1 : 2 : 3 : []
9+
10+module Main where
11+
12+main = Trace.trace "Done"
examples/passing/Console.purs+13−25
@@ -1,25 +1,13 @@
1−module Console where
2−
3− foreign import data IO :: * -> *
4−
5− foreign import ret :: forall a. a -> IO a
6−
7− foreign import (>>=) :: forall a b. IO a -> (a -> IO b) -> IO b
8−
9− (*>) = \a -> \b -> a >>= \x -> b
10−
11− foreign import putStrLn :: String -> IO {}
12−
13− replicateM_ :: forall a. Number -> IO a -> IO {}
14− replicateM_ = \n -> \x ->
15− {
16− var io = ret {};
17− for (i <- 0 until n)
18− {
19− io = x *> io;
20− }
21− return io;
22− }
23−
24− main = replicateM_ 10 (putStrLn "Hello World!")
25−
1+module Main where
2+
3+import Prelude
4+import Eff
5+import Trace
6+
7+replicateM_ :: forall m a. (Monad m) => Number -> m a -> m {}
8+replicateM_ 0 _ = ret {}
9+replicateM_ n act = do
10+ act
11+ replicateM_ (n - 1) act
12+
13+main = replicateM_ 10 (trace "Hello World!")
examples/passing/DataAndType.purs+4
@@ -3,3 +3,7 @@ module DataAndType where
33data A = A B
44
55type B = A
6+
7+module Main where
8+
9+main = Trace.trace "Done"
examples/passing/DataImport.purs+4
@@ -10,3 +10,7 @@ test :: Test -> M1.Test
1010test t = t
1111
1212testCtor = Test
13+
14+module Main where
15+
16+main = Trace.trace "Done"
examples/passing/Do.purs+17−15
@@ -1,9 +1,3 @@
1−module Prelude where
2−
3−class Monad m where
4− ret :: forall a. a -> m a
5− (>>=) :: forall a b. m a -> (a -> m b) -> m b
6−
71module Do where
82
93import Prelude
@@ -15,34 +9,38 @@ instance Prelude.Monad Maybe where
159 (>>=) Nothing _ = Nothing
1610 (>>=) (Just a) f = f a
1711
18−test1 = do Just "abc"
12+test1 = \ -> do
13+ Just "abc"
1914
20−test3 = do
15+test2 = \ -> do
2116 (x : _) <- Just [1, 2, 3]
2217 (y : _) <- Just [4, 5, 6]
2318 Just (x + y)
2419
25−test4 = do
20+test3 = \ -> do
2621 Just 1
2722 Nothing :: Maybe Number
2823 Just 2
2924
30−test5 mx my = do
25+test4 mx my = do
3126 x <- mx
3227 y <- my
3328 Just (x + y)
3429
35−test6 mx my mz = do
30+test5 mx my mz = do
3631 x <- mx
3732 y <- my
3833 let sum = x + y
3934 z <- mz
4035 Just (z + sum)
4136
42−test7 mx = do let Just x = mx
43− Just x
37+test6 mx = \ -> do
38+ let Just x = mx
39+ Just x
4440
45−test8 = do Just (do Just 1)
41+test8 = \ -> do
42+ Just (do
43+ Just 1)
4644
4745(<$>) :: forall a b. (a -> b) -> Maybe a -> Maybe b
4846(<$>) f m = do
@@ -62,4 +60,8 @@ forever a = do
6260 a
6361 forever a
6462
65−test9 = foo <$> Just 1 <*> Just 2 <*> Just 3
63+test9 = \ -> foo <$> Just 1 <*> Just 2 <*> Just 3
64+
65+module Main where
66+
67+main = Trace.trace "Done"
examples/passing/Dollar.purs+4
@@ -10,3 +10,7 @@ id x = x
1010test1 x = id $ id $ id $ id $ x
1111
1212test2 x = id id $ id x
13+
14+module Main where
15+
16+main = Trace.trace "Done"
examples/passing/Eff.purs+18−62
@@ -1,71 +1,27 @@
1−module Prelude where
2−
3−class Monad m where
4− ret :: forall a. a -> m a
5− (>>=) :: forall a b. m a -> (a -> m b) -> m b
6−
7−module Eff where
1+module TestEff where
82
93import Prelude
4+import Eff
5+import ST
6+import Errors
7+import Trace
108
11−-- Eff Monad
12−
13−foreign import data Eff :: # ! -> * -> *
14−
15−foreign import retEff :: forall e a. a -> Eff e a
16−
17−foreign import bindEff :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
18−
19−instance Prelude.Monad (Eff e) where
20− ret = retEff
21− (>>=) = bindEff
22−
23−type Pure a = forall e. Eff e a
24−
25−foreign import runPure :: forall a. Pure a -> a
26−
27−-- Errors
28−
29−foreign import data Error :: * -> !
30−
31−foreign import throwError :: forall e r. e -> Eff (err :: Error e | r) {}
32−
33−foreign import catchError :: forall e r a. (e -> Eff r a) -> Eff (err :: Error e | r) a -> Eff r a
34−
35−-- State
36−
37−foreign import data State :: * -> !
38−
39−foreign import get :: forall s r. Eff (state :: State s | r) s
40−
41−foreign import set :: forall s r. s -> Eff (state :: State s | r) {}
42−
43−foreign import runState :: forall s r a. s -> Eff (state :: State s | r) a -> Eff r { value :: a, state :: s }
44−
45−-- ST
46−
47−foreign import data ST :: * -> !
48−
49−foreign import data STRef :: * -> * -> *
50−
51−foreign import newSTRef :: forall a h r. a -> Eff (st :: ST h | r) (STRef h a)
52−
53−foreign import readSTRef :: forall a h r. STRef h a -> Eff (st :: ST h | r) a
54−
55−foreign import modifySTRef :: forall a h r. (a -> a) -> STRef h a -> Eff (st :: ST h | r) {}
56−
57−foreign import runST :: forall a r. (forall h. Eff (st :: ST h | r) a) -> Eff r a
58−
59−-- Tests
60−
61−test1 = runPure (runState 0 (catchError (\s -> ret 0)
62− (do s <- get
63− throwError "Error!"
64− set (s + 1)
65− ret s)))
9+test1 = catchError (\s -> ret 0)
10+ (do trace "Testing"
11+ throwError "Error!")
6612
6713test2 = runPure (runST (do
6814 ref <- newSTRef 0
6915 modifySTRef (\n -> n + 1) ref
7016 readSTRef ref))
7117
18+module Main where
19+
20+import Eff
21+import TestEff
22+import Global
23+
24+main = do
25+ n <- test1
26+ Trace.print n
27+ Trace.print test2
examples/passing/ExternData.purs+5−1
@@ -12,4 +12,8 @@ module ExternData where
1212
1313 foreign import prompt :: IO String
1414
15− main = prompt `bind` \s -> showMessage s
15+ main = \ -> prompt `bind` \s -> showMessage s
16+
17+module Main where
18+
19+main = Trace.trace "Done"
examples/passing/ExternRaw.purs+4
@@ -11,3 +11,7 @@ foreign import concat "function concat(xs) { \
1111 \ return xs.concat(ys); \
1212 \ };\
1313 \}" :: forall a. [a] -> [a] -> [a]
14+
15+module Main where
16+
17+main = Trace.trace "Done"
examples/passing/FFI.purs+5−1
@@ -9,4 +9,8 @@ module FFIModuleTest where
99
1010import FFI
1111
12−baz = foo "test"
12+baz = \ -> foo "test"
13+
14+module Main where
15+
16+main = Trace.trace "Done"
examples/passing/Functions.purs+4
@@ -11,3 +11,7 @@ module Functions where
1111 test5 = \(%%) -> 1 %% 2
1212
1313 test6 = \((+++), (***)) -> 1 +++ 2 *** 3
14+
15+module Main where
16+
17+main = Trace.trace "Done"
examples/passing/Guards.purs+4
@@ -8,3 +8,7 @@ module Guards where
88 collatz2 = \x y -> case x of
99 z | y > 0 -> z / 2
1010 z -> z * 3 + 1
11+
12+module Main where
13+
14+main = Trace.trace "Done"
examples/passing/IfThenElse.purs+4
@@ -34,3 +34,7 @@ module IfThenElse where
3434 }
3535 return y;
3636 }
37+
38+module Main where
39+
40+main = Trace.trace "Done"
examples/passing/Import.purs+4
@@ -10,3 +10,7 @@ module M2 where
1010 import M1
1111
1212 main = \() -> foo 42
13+
14+module Main where
15+
16+main = Trace.trace "Done"
examples/passing/List.purs+3
@@ -18,4 +18,7 @@ module List where
1818
1919 flatten Nil = Nil
2020 flatten (Cons cons) = concat cons.head (flatten cons.tail)
21+
22+module Main where
2123
24+main = Trace.trace "Done"
examples/passing/Match.purs+4
@@ -3,3 +3,7 @@ module Match where
33 data Foo a = Foo
44
55 foo = \f -> case f of Foo -> "foo"
6+
7+module Main where
8+
9+main = Trace.trace "Done"
examples/passing/Maybe.purs+9−9
@@ -1,11 +1,11 @@
1−module Maybe where
1+module Main where
22
3− data Maybe a = Nothing | Just a
3+import Prelude
4+import Maybe
45
5− maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
6− maybe b _ Nothing = b
7− maybe _ f (Just a) = f a
8−
9− fmap m f = maybe Nothing (\a -> Just (f a)) m
10−
11− bind m f = maybe Nothing f m
6+main = do
7+ let test1 = fromMaybe $ do
8+ a <- Just 1
9+ b <- Just 2
10+ ret (a + b)
11+ Trace.trace "Done"
examples/passing/Module.purs+4
@@ -18,3 +18,7 @@ module M2 where
1818
1919 match :: M1.Foo -> String
2020 match = \f -> case f of M1.Foo s -> s ++ "foo"
21+
22+module Main where
23+
24+main = Trace.trace "Done"
examples/passing/ModuleDeps.purs+4
@@ -11,3 +11,7 @@ bar = M3.baz
1111module M3 where
1212
1313baz = 1
14+
15+module Main where
16+
17+main = Trace.trace "Done"
examples/passing/Monad.purs+4
@@ -26,3 +26,7 @@ module Monad where
2626 test1 = test id
2727
2828 test2 = test maybe
29+
30+module Main where
31+
32+main = Trace.trace "Done"
examples/passing/MutRec.purs+4
@@ -13,3 +13,7 @@ module MutRec where
1313 evenToNumber (Even n) = oddToNumber n + 1
1414
1515 oddToNumber (Odd n) = evenToNumber n + 1
16+
17+module Main where
18+
19+main = Trace.trace "Done"
examples/passing/NamedPatterns.purs+4
@@ -3,3 +3,7 @@ module NamedPatterns where
33 foo = \x -> case x of
44 y@{ foo = "Foo" } -> y
55 y -> y
6+
7+module Main where
8+
9+main = Trace.trace "Done"
examples/passing/Nested.purs+4
@@ -3,3 +3,7 @@ module Nested where
33 data Extend r a = Extend { prev :: r a, next :: a }
44
55 data Matrix r a = Square (r (r a)) | Bigger (Matrix (Extend r) a)
6+
7+module Main where
8+
9+main = Trace.trace "Done"
examples/passing/ObjectUpdate.purs+4
@@ -14,3 +14,7 @@ module ObjectUpdate where
1414 polyUpdate = \o -> o { foo = "Foo" }
1515
1616 inferPolyUpdate = \o -> o { foo = "Foo" }
17+
18+module Main where
19+
20+main = Trace.trace "Done"
examples/passing/Objects.purs+4
@@ -12,3 +12,7 @@ module Objects where
1212
1313 typed :: { foo :: Number }
1414 typed = { foo: 0 }
15+
16+module Main where
17+
18+main = Trace.trace "Done"
examples/passing/OneConstructor.purs+4
@@ -3,3 +3,7 @@ module OneConstructor where
33data One a = One a
44
55one (One a) = a
6+
7+module Main where
8+
9+main = Trace.trace "Done"
examples/passing/Operators.purs+26
@@ -44,3 +44,29 @@ module Operators2 where
4444 test11 = [1, 2, 3] ... [4, 5, 6]
4545
4646 test12 (<%>) a b = a <%> b
47+
48+module Main where
49+
50+import Operators1
51+import Operators2
52+
53+import Prelude
54+import Eff
55+import Trace
56+import Global
57+import Arrays
58+
59+main = do
60+ print (test1 (1, 2, \x y -> x + y))
61+ print test2
62+ print test3
63+ print test4
64+ print test5
65+ print test6
66+ print test7
67+ print test8
68+ print test9
69+ print test10
70+ print test11
71+ print (test12 k 1 2)
72+
examples/passing/OptimizerBug.purs+4
@@ -3,3 +3,7 @@ module OptimizerBug where
33x = 1 + y
44
55y = x
6+
7+module Main where
8+
9+main = Trace.trace "Done"
examples/passing/Patterns.purs+3
@@ -23,3 +23,6 @@ module Patterns where
2323 [x, y] | x > y -> true
2424 _ -> false
2525
26+module Main where
27+
28+main = Trace.trace "Done"
examples/passing/Person.purs+4
@@ -7,3 +7,7 @@ module Person where
77 showPerson :: Person -> String
88 showPerson = \p -> case p of
99 Person o -> o.name ++ ", aged " ++ itoa(o.age)
10+
11+module Main where
12+
13+main = Trace.trace "Done"
examples/passing/Rank2Data.purs+4
@@ -23,3 +23,7 @@ module Rank2Data where
2323 two = succ zero
2424 four = add two two
2525 fourNumber = runNat four
26+
27+module Main where
28+
29+main = Trace.trace "Done"
examples/passing/Rank2Types.purs+5−1
@@ -9,7 +9,7 @@ module Rank2Types where
99
1010 foreign import exampleST :: forall s. ST s Number
1111
12− testST = runST exampleST
12+ testST = \ -> runST exampleST
1313
1414 foreign import push :: forall el. el -> [el] -> [el]
1515
@@ -17,3 +17,7 @@ module Rank2Types where
1717 replicateM = \ret bind n m -> case n of
1818 0 -> ret []
1919 n -> bind m (\x -> bind (replicateM ret bind (n - 1) m) (\xs -> ret (push x xs)))
20+
21+module Main where
22+
23+main = Trace.trace "Done"
examples/passing/Recursion.purs+4
@@ -4,3 +4,7 @@ module Recursion where
44 0 -> 1
55 1 -> 1
66 n -> fib (n - 1) + fib (n - 2)
7+
8+module Main where
9+
10+main = Trace.trace "Done"
examples/passing/RedefinedFixity.purs+4
@@ -12,3 +12,7 @@ module M3 where
1212
1313import M1
1414import M2
15+
16+module Main where
17+
18+main = Trace.trace "Done"
examples/passing/TailCall.purs+3
@@ -7,4 +7,7 @@ loop x = loop (x + 1)
77
88notATailCall = \x ->
99 (\notATailCall -> notATailCall x) (\x -> x)
10+
11+module Main where
1012
13+main = Trace.trace "Done"
examples/passing/TopLevelCase.purs+4
@@ -12,3 +12,7 @@ module TopLevelCase where
1212 data A = A
1313
1414 parseTest A 0 = 0
15+
16+module Main where
17+
18+main = Trace.trace "Done"
examples/passing/TypeClasses.purs+11−7
@@ -6,19 +6,19 @@ class Show a where
66instance Show String where
77 show s = s
88
9−test1 = show "testing"
9+test1 = \ -> show "testing"
1010
1111f :: forall a. (TypeClasses.Show a) => a -> String
1212f x = show x
1313
14−test2 = f "testing"
14+test2 = \ -> f "testing"
1515
1616data Data a = Data a
1717
1818instance (TypeClasses.Show a) => TypeClasses.Show (Data a) where
1919 show (Data a) = "Data (" ++ show a ++ ")"
2020
21−test3 = show (Data "testing")
21+test3 = \ -> show (Data "testing")
2222
2323class Monad m where
2424 ret :: forall a. a -> m a
@@ -35,10 +35,10 @@ instance TypeClasses.Monad Maybe where
3535 (>>=) Nothing _ = Nothing
3636 (>>=) (Just a) f = f a
3737
38−test4 :: forall m. (Monad m) => m Number
39−test4 = ret 1
38+test4 :: forall m. (Monad m) => () -> m Number
39+test4 = \ -> ret 1
4040
41−test5 = Just 1 >>= \n -> ret (n + 1)
41+test5 = \ -> Just 1 >>= \n -> ret (n + 1)
4242
4343module TypeClasses2 where
4444
@@ -48,5 +48,9 @@ instance (TypeClasses.Show a) => TypeClasses.Show [a] where
4848 show [] = "[]"
4949 show (x:xs) = TypeClasses.show x ++ ", " ++ TypeClasses.show xs
5050
51−test6 = show ["testing"]
51+test6 = \ -> show ["testing"]
52+
53+module Main where
54+
55+main = Trace.trace "Done"
5256
examples/passing/TypeDecl.purs+4
@@ -14,3 +14,7 @@ module TypeDecl where
1414 }
1515 return result;
1616 }
17+
18+module Main where
19+
20+main = Trace.trace "Done"
examples/passing/TypeSynonyms.purs+13−2
@@ -27,7 +27,18 @@ module TypeSynonyms where
2727 test1 :: forall a b c. Lens (Pair (Pair a b) c) a
2828 test1 = composeLenses fst fst
2929
30− foreign import head :: forall a. Lens [a] a
30+ import Arrays
31+
32+ headLens :: forall a. Lens [a] a
33+ headLens =
34+ { get: \l -> head l
35+ , set: \l a -> case l of
36+ _:xs -> a : xs
37+ }
3138
3239 test2 :: forall a b c. Lens (Pair [Pair a b] c) a
33− test2 = composeLenses fst (composeLenses head fst)
40+ test2 = composeLenses fst (composeLenses headLens fst)
41+
42+module Main where
43+
44+main = Trace.trace "Done"
examples/passing/iota.purs+4
@@ -5,3 +5,7 @@ module Iota where
55 k = \x -> \y -> x
66
77 iota = \x -> x s k
8+
9+module Main where
10+
11+main = Trace.trace "Done"
examples/passing/s.purs+4
@@ -1,3 +1,7 @@
11module S where
22
33 s = \x y z -> x z (y z)
4+
5+module Main where
6+
7+main = Trace.trace "Done"
libraries/prelude/Makefiledeleted−3
@@ -1,3 +0,0 @@
1−all:
2− ../../dist/build/psc/psc --no-prelude prelude.purs -o all.js
3− cat prelude.js >> all.js
libraries/prelude/prelude.jsdeleted−42
@@ -1,42 +0,0 @@
1−// Arrays
2−
3−function indexOf(l) { return function (e) { return l.indexOf(e); }; }
4−function lastIndexOf(l) { return function (e) { return l.lastIndexOf(e); }; }
5−function concat(l1) { return function (l2) { return l1.concat(l2); }; }
6−function join(l) { return l.join(); }
7−function joinWith(l) { return function (s) { return l.join(s); }; }
8−function push(l) { return function (e) { var l1 = l.slice(); l1.push(e); return l1; }; }
9−function reverse(l) { var l1 = l.slice(); l1.reverse(); return l1; }
10−function shift(l) { var l1 = l.slice(); l1.shift(); return l1; }
11−function slice(s) { return function(e) { return function (l) { return l.slice(s, e); }; }; }
12−function sort(l) { var l1 = l.slice(); l.sort(); return l1; }
13−function splice(s) { return function(e) { return function(l1) { return function(l2) { return l2.splice(s, e, l1); }; }; }; }
14−
15−function charAt(i) { return function(s) { return s.charAt(i); }; }
16−function indexOfS(s1) { return function(s2) { return s2.indexOf(s2); }; }
17−function lastIndexOfS(s1) { return function(s2) { return s2.lastIndexOf(s2); }; }
18−function localeCompare(s1) { return function(s2) { return s1.localeCompare(s2); }; }
19−function replace(s1) { return function(s2) { return function(s3) { return s3.replace(s1, s2) }; }; }
20−function sliceS(st) { return function(e) { return function(s) { return s.slice(st, e); }; }; }
21−function split(sep) { return function(s) { return s.split(s); }; }
22−function substr(n1) { return function(n2) { return function(s) { return s.substr(n1, n2); }; }; }
23−function substring(n1) { return function(n2) { return function(s) { return s.substring(n1, n2); }; }; }
24−function toLower(s) { return s.toLower(); }
25−function toUpper(s) { return s.toUpper(); }
26−function trim(s) { return s.trim(); }
27−
28−function regex(s1) { return function(s2) { return new Regex(s1, s2); }; };
29−function test(r) { return function (s) { return r.test(s); }; };
30−function match(r) { return function (s) { return s.match(r); }; };
31−function replaceR(r) { return function(s1) { return function(s2) { return s2.replace(r, s1) }; }; }
32−function search(r) { return function (s) { return s.search(r); }; };
33−
34−var nan = NaN;
35−var infinity = Infinity;
36−function toExponential(n) { return n.toExponential(); }
37−function toFixed(d) { return function(n) { return n.toFixed(d); }; }
38−function toPrecision(d) { return function(n) { return n.toPrecision(d); }; }
39−function numberToString(n) { return n.toString(); }
40−
41−var math = Math;
42−
libraries/prelude/prelude.purs+288−83
@@ -49,8 +49,9 @@ module Maybe where
4949 fromMaybe :: forall a. a -> Maybe a -> a
5050 fromMaybe a = maybe a Prelude.id
5151
52− bindMaybe :: forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
53− bindMaybe m f = maybe Nothing f m
52+ instance Prelude.Monad Maybe where
53+ ret = Just
54+ (>>=) m f = maybe Nothing f m
5455
5556module Either where
5657
@@ -60,11 +61,13 @@ module Either where
6061 either f _ (Left a) = f a
6162 either _ g (Right b) = g b
6263
63− bindEither :: forall e a b. Either e a -> (a -> Either e b) -> Either e b
64− bindEither = either (\e _ -> Left e) (\a f -> f a)
64+ instance Prelude.Monad (Either e) where
65+ ret = Right
66+ (>>=) = either (\e _ -> Left e) (\a f -> f a)
6567
6668module Arrays where
6769
70+ import Prelude
6871 import Maybe
6972
7073 head :: forall a. [a] -> a
@@ -97,27 +100,77 @@ module Arrays where
97100 \ return xs.length; \
98101 \}" :: forall a. [a] -> Number
99102
100− foreign import indexOf :: forall a. [a] -> a -> Number
101−
102− foreign import lastIndexOf :: forall a. [a] -> a -> Number
103−
104− foreign import concat :: forall a. [a] -> [a] -> [a]
105−
106− foreign import join :: [String] -> String
107−
108− foreign import joinWith :: [String] -> String -> String
109−
110− foreign import push :: forall a. [a] -> a -> [a]
111−
112− foreign import reverse :: forall a. [a] -> [a]
113−
114− foreign import shift :: forall a. [a] -> [a]
115−
116− foreign import slice :: forall a. Number -> Number -> [a] -> [a]
117−
118− foreign import sort :: forall a. [a] -> [a]
119−
120− foreign import splice :: forall a. Number -> Number -> [a] -> [a] -> [a]
103+ foreign import indexOf "function indexOf(l) {\
104+ \ return function (e) {\
105+ \ return l.indexOf(e);\
106+ \ };\
107+ \}" :: forall a. [a] -> a -> Number
108+
109+ foreign import lastIndexOf "function lastIndexOf(l) {\
110+ \ return function (e) {\
111+ \ return l.lastIndexOf(e);\
112+ \ };\
113+ \}" :: forall a. [a] -> a -> Number
114+
115+ foreign import concat "function concat(l1) {\
116+ \ return function (l2) {\
117+ \ return l1.concat(l2);\
118+ \ };\
119+ \}" :: forall a. [a] -> [a] -> [a]
120+
121+ foreign import join "function join(l) {\
122+ \ return l.join();\
123+ \}" :: [String] -> String
124+
125+ foreign import joinWith "function joinWith(l) {\
126+ \ return function (s) {\
127+ \ return l.join(s);\
128+ \ };\
129+ \}" :: [String] -> String -> String
130+
131+ foreign import push "function push(l) {\
132+ \ return function (e) {\
133+ \ var l1 = l.slice();\
134+ \ l1.push(e); \
135+ \ return l1;\
136+ \ };\
137+ \}" :: forall a. [a] -> a -> [a]
138+
139+ foreign import reverse "function reverse(l) {\
140+ \ var l1 = l.slice();\
141+ \ l1.reverse(); \
142+ \ return l1;\
143+ \}" :: forall a. [a] -> [a]
144+
145+ foreign import shift "function shift(l) {\
146+ \ var l1 = l.slice();\
147+ \ l1.shift();\
148+ \ return l1;\
149+ \}" :: forall a. [a] -> [a]
150+
151+ foreign import slice "function slice(s) {\
152+ \ return function(e) {\
153+ \ return function (l) {\
154+ \ return l.slice(s, e);\
155+ \ };\
156+ \ };\
157+ \}" :: forall a. Number -> Number -> [a] -> [a]
158+
159+ foreign import sort "function sort(l) {\
160+ \ var l1 = l.slice();\
161+ \ l.sort();\
162+ \ return l1;\
163+ \}" :: forall a. [a] -> [a]
164+
165+ foreign import splice "function splice(s) {\
166+ \ return function(e) {\
167+ \ return function(l1) { \
168+ \ return function(l2) {\
169+ \ return l2.splice(s, e, l1);\
170+ \ }; \
171+ \ }; \
172+ \ };\
173+ \}":: forall a. Number -> Number -> [a] -> [a] -> [a]
121174
122175 infixr 6 :
123176
@@ -158,6 +211,10 @@ module Arrays where
158211 all _ [] = true
159212 all p (a:as) = p a && all p as
160213
214+ instance (Prelude.Show a) => Prelude.Show [a] where
215+ show [] = "[]"
216+ show (x:xs) = show x ++ " : " ++ show xs
217+
161218module Tuple where
162219
163220 import Arrays
@@ -187,57 +244,141 @@ module String where
187244 \ return s.length;\
188245 \}" :: String -> Number
189246
190− foreign import charAt :: Number -> String -> String
191−
192− foreign import indexOfS :: String -> String -> Number
193−
194− foreign import lastIndexOfS :: String -> String -> Number
195−
196− foreign import localeCompare :: String -> String -> Number
197−
198− foreign import replace :: String -> String -> String -> String
199−
200− foreign import sliceS :: Number -> Number -> String -> String
201−
202− foreign import split :: String -> String -> [String]
203−
204− foreign import substr :: Number -> Number -> String -> String
205−
206− foreign import substring :: Number -> Number -> String -> String
207−
208− foreign import toLower :: String -> String
209−
210− foreign import toUpper :: String -> String
211−
212− foreign import trim :: String -> String
247+ foreign import charAt "function charAt(i) {\
248+ \ return function(s) {\
249+ \ return s.charAt(i); \
250+ \ };\
251+ \}" :: Number -> String -> String
252+
253+ foreign import indexOfS "function indexOfS(s1) {\
254+ \ return function(s2) {\
255+ \ return s2.indexOf(s2);\
256+ \ }; \
257+ \}" :: String -> String -> Number
258+
259+ foreign import lastIndexOfS "function lastIndexOfS(s1) {\
260+ \ return function(s2) {\
261+ \ return s2.lastIndexOf(s2);\
262+ \ };\
263+ \}" :: String -> String -> Number
264+
265+ foreign import localeCompare "function localeCompare(s1) {\
266+ \ return function(s2) { \
267+ \ return s1.localeCompare(s2);\
268+ \ };\
269+ \}" :: String -> String -> Number
270+
271+ foreign import replace "function replace(s1) {\
272+ \ return function(s2) {\
273+ \ return function(s3) {\
274+ \ return s3.replace(s1, s2);\
275+ \ };\
276+ \ };\
277+ \}" :: String -> String -> String -> String
278+
279+ foreign import sliceS "function sliceS(st) {\
280+ \ return function(e) {\
281+ \ return function(s) {\
282+ \ return s.slice(st, e);\
283+ \ };\
284+ \ };\
285+ \}" :: Number -> Number -> String -> String
286+
287+ foreign import split "function split(sep) {\
288+ \ return function(s) {\
289+ \ return s.split(s);\
290+ \ };\
291+ \}" :: String -> String -> [String]
292+
293+ foreign import substr "function substr(n1) {\
294+ \ return function(n2) {\
295+ \ return function(s) {\
296+ \ return s.substr(n1, n2);\
297+ \ };\
298+ \ };\
299+ \}" :: Number -> Number -> String -> String
300+
301+ foreign import substring "function substring(n1) {\
302+ \ return function(n2) {\
303+ \ return function(s) {\
304+ \ return s.substring(n1, n2);\
305+ \ };\
306+ \ };\
307+ \}" :: Number -> Number -> String -> String
308+
309+ foreign import toLower "function toLower(s) {\
310+ \ return s.toLower();\
311+ \}" :: String -> String
312+
313+ foreign import toUpper "function toUpper(s) {\
314+ \ return s.toUpper();\
315+ \}" :: String -> String
316+
317+ foreign import trim "function trim(s) {\
318+ \ return s.trim();\
319+ \}" :: String -> String
213320
214321module Regex where
215322
216323 foreign import data Regex :: *
217324
218− foreign import regex :: String -> String -> Regex
219−
220− foreign import test :: Regex -> String -> Boolean
221−
222− foreign import match :: Regex -> String -> [String]
223−
224− foreign import replaceR :: Regex -> String -> String -> String
225−
226− foreign import search :: Regex -> String -> Number
325+ foreign import regex "function regex(s1) {\
326+ \ return function(s2) {\
327+ \ return new Regex(s1, s2);\
328+ \ };\
329+ \}" :: String -> String -> Regex
330+
331+ foreign import test "function test(r) {\
332+ \ return function (s) { \
333+ \ return r.test(s);\
334+ \ };\
335+ \}" :: Regex -> String -> Boolean
336+
337+ foreign import match "function match(r) {\
338+ \ return function (s) {\
339+ \ return s.match(r); \
340+ \ };\
341+ \}" :: Regex -> String -> [String]
342+
343+ foreign import replaceR "function replaceR(r) {\
344+ \ return function(s1) {\
345+ \ return function(s2) { \
346+ \ return s2.replace(r, s1);\
347+ \ };\
348+ \ };\
349+ \}" :: Regex -> String -> String -> String
350+
351+ foreign import search "function search(r) {\
352+ \ return function (s) {\
353+ \ return s.search(r);\
354+ \ };\
355+ \}" :: Regex -> String -> Number
227356
228357module Global where
229358
230− foreign import nan :: Number
359+ foreign import nan "var nan = NaN;" :: Number
231360
232− foreign import infinity :: Number
361+ foreign import infinity "var infinity = Infinity;" :: Number
233362
234− foreign import toExponential :: Number -> String
363+ foreign import toExponential "function toExponential(n) {\
364+ \ return n.toExponential();\
365+ \}" :: Number -> String
235366
236− foreign import toFixed :: Number -> Number -> String
367+ foreign import toFixed "function toFixed(d) {\
368+ \ return function(n) {\
369+ \ return n.toFixed(d);\
370+ \ };\
371+ \}" :: Number -> Number -> String
237372
238− foreign import toPrecision :: Number -> Number -> String
373+ foreign import toPrecision "function toPrecision(d) {\
374+ \ return function(n) {\
375+ \ return n.toPrecision(d);\
376+ \ };\
377+ \}" :: Number -> Number -> String
239378
240− foreign import numberToString :: Number -> String
379+ foreign import numberToString "function numberToString(n) {\
380+ \ return n.toString();\
381+ \}" :: Number -> String
241382
242383 foreign import isNaN :: Number -> Boolean
243384
@@ -255,6 +396,9 @@ module Global where
255396
256397 foreign import decodeURI :: String -> String
257398
399+ instance Prelude.Show Number where
400+ show = numberToString
401+
258402module Math where
259403
260404 type Math =
@@ -277,19 +421,31 @@ module Math where
277421 , tan :: Number -> Number
278422 }
279423
280− foreign import math :: Math
424+ foreign import math "var math = Math;" :: Math
281425
282426module Eff where
283427
284428 foreign import data Eff :: # ! -> * -> *
285429
286− foreign import retEff "function retEff(a) { return function() { return a; }; }" :: forall e a. a -> Eff e a
430+ foreign import retEff "function retEff(a) { \
431+ \ return function() { \
432+ \ return a; \
433+ \ }; \
434+ \}" :: forall e a. a -> Eff e a
287435
288− foreign import bindEff "function bindEff(a) { return function(f) { return function() { return f(a())(); }; }; }" :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
436+ foreign import bindEff "function bindEff(a) { \
437+ \ return function(f) { \
438+ \ return function() { \
439+ \ return f(a())(); \
440+ \ }; \
441+ \ }; \
442+ \}" :: forall e a b. Eff e a -> (a -> Eff e b) -> Eff e b
289443
290444 type Pure a = forall e. Eff e a
291445
292− foreign import runPure "function runPure(f) { return f(); }" :: forall a. Pure a -> a
446+ foreign import runPure "function runPure(f) { \
447+ \ return f(); \
448+ \}" :: forall a. Pure a -> a
293449
294450 instance Prelude.Monad (Eff e) where
295451 ret = retEff
@@ -301,9 +457,23 @@ module Errors where
301457
302458 foreign import data Error :: * -> !
303459
304− foreign import throwError "function throwError(e) { return function() { throw e; }; }" :: forall a e r. e -> Eff (err :: Error e | r) a
305−
306− foreign import catchError "function catchError(c) { return function(t) { return function() { try { return t(); } catch(e) { return c(e)(); } }; }; }" :: forall e r a. (e -> Eff r a) -> Eff (err :: Error e | r) a -> Eff r a
460+ foreign import throwError "function throwError(e) { \
461+ \ return function() { \
462+ \ throw e; \
463+ \ }; \
464+ \}" :: forall a e r. e -> Eff (err :: Error e | r) a
465+
466+ foreign import catchError "function catchError(c) { \
467+ \ return function(t) { \
468+ \ return function() { \
469+ \ try { \
470+ \ return t(); \
471+ \ } catch(e) { \
472+ \ return c(e)(); \
473+ \ }\
474+ \ }; \
475+ \ }; \
476+ \}" :: forall e r a. (e -> Eff r a) -> Eff (err :: Error e | r) a -> Eff r a
307477
308478module IORef where
309479
@@ -313,11 +483,25 @@ module IORef where
313483
314484 foreign import data IORef :: * -> *
315485
316− foreign import newIORef :: forall s r. s -> Eff (ref :: Ref | r) (IORef s)
317−
318− foreign import readIORef :: forall s r. IORef s -> Eff (ref :: Ref | r) s
319−
320− foreign import writeIORef :: forall s r. IORef s -> s -> Eff (ref :: Ref | r) {}
486+ foreign import newIORef "function newIORef(val) {\
487+ \ return function () {\
488+ \ return { value: val };\
489+ \ };\
490+ \}" :: forall s r. s -> Eff (ref :: Ref | r) (IORef s)
491+
492+ foreign import readIORef "function readIORef(ref) {\
493+ \ return function() {\
494+ \ return ref.value;\
495+ \ };\
496+ \}" :: forall s r. IORef s -> Eff (ref :: Ref | r) s
497+
498+ foreign import writeIORef "function writeIORef(ref) {\
499+ \ return function(val) {\
500+ \ return function() {\
501+ \ ref.value = val;\
502+ \ };\
503+ \ };\
504+ \}" :: forall s r. IORef s -> s -> Eff (ref :: Ref | r) {}
321505
322506module Trace where
323507
@@ -326,7 +510,12 @@ module Trace where
326510
327511 foreign import data Trace :: !
328512
329− foreign import trace "function trace(s) { return function() { console.log(s); return {}; }; }" :: forall r. String -> Eff (trace :: Trace | r) {}
513+ foreign import trace "function trace(s) { \
514+ \ return function() { \
515+ \ console.log(s); \
516+ \ return {}; \
517+ \ }; \
518+ \}" :: forall r. String -> Eff (trace :: Trace | r) {}
330519
331520 print :: forall a r. (Prelude.Show a) => a -> Eff (trace :: Trace | r) {}
332521 print o = trace (show o)
@@ -339,11 +528,27 @@ module ST where
339528
340529 foreign import data STRef :: * -> * -> *
341530
342− foreign import newSTRef :: forall a h r. a -> Eff (st :: ST h | r) (STRef h a)
343−
344− foreign import readSTRef :: forall a h r. STRef h a -> Eff (st :: ST h | r) a
345−
346− foreign import modifySTRef :: forall a h r. (a -> a) -> STRef h a -> Eff (st :: ST h | r) {}
347−
348− foreign import runST :: forall a r. (forall h. Eff (st :: ST h | r) a) -> Eff r a
531+ foreign import newSTRef "function newSTRef(val) {\
532+ \ return function () {\
533+ \ return { value: val };\
534+ \ };\
535+ \}" :: forall a h r. a -> Eff (st :: ST h | r) (STRef h a)
536+
537+ foreign import readSTRef "function readSTRef(ref) {\
538+ \ return function() {\
539+ \ return ref.value;\
540+ \ };\
541+ \}" :: forall a h r. STRef h a -> Eff (st :: ST h | r) a
542+
543+ foreign import modifySTRef "function modifySTRef(f) {\
544+ \ return function(ref) {\
545+ \ return function() {\
546+ \ ref.value = f(ref.value);\
547+ \ };\
548+ \ };\
549+ \}" :: forall a h r. (a -> a) -> STRef h a -> Eff (st :: ST h | r) {}
550+
551+ foreign import runST "function runST(f) {\
552+ \ return f;\
553+ \}" :: forall a r. (forall h. Eff (st :: ST h | r) a) -> Eff r a
349554
libraries/prelude/tests/.gitignorenew file+1
@@ -0,0 +1 @@
1+Eff.js
libraries/prelude/tests/Eff.purs+11−11
@@ -5,32 +5,32 @@ import Eff
55import Errors
66import Trace
77
8−test1 n = runPure $ catchError (\s -> eff do return 0) $ eff do
8+test1 n = runPure $ catchError (\s -> ret 0) $ do
99 case {} of
10− _ | n > 10 -> eff do
10+ _ | n > 10 -> do
1111 throwError "Error!"
12− _ -> eff do return n
12+ _ -> ret n
1313
14−test2 = eff do
14+test2 = do
1515 trace "Hello World"
1616 throwError "Error!"
1717
18−test3 n = catchError (\s -> eff do return 0) $ eff do
18+test3 n = catchError (\s -> ret 0) $ do
1919 case {} of
20− _ | n > 10 -> eff do
20+ _ | n > 10 -> do
2121 trace "n > 10"
2222 throwError "Error!"
23− _ -> eff do return n
23+ _ -> ret n
2424
25−test4 = eff do
25+test4 = do
2626 trace "Hello World!"
27− return 0
27+ ret 0
2828
29−test5 = eff do
29+test5 = do
3030 trace "Hello World!"
3131 test5
3232
33−test6 s = eff do
33+test6 s = do
3434 trace s
3535 test6 (s ++ " test")
3636
libraries/prelude/tests/build.shnew file+2
@@ -0,0 +1,2 @@
1+psc ../prelude.purs Eff.purs --magic-do --run-main --no-prelude -o Eff.js
2+
purescript.cabal+1−1
@@ -89,7 +89,7 @@ executable psci
8989test-suite tests
9090 build-depends: base >=4 && <5, containers -any, directory -any,
9191 filepath -any, mtl -any, parsec -any, purescript -any, syb -any,
92− transformers -any, utf8-string -any
92+ transformers -any, utf8-string -any, process -any
9393 type: exitcode-stdio-1.0
9494 main-is: Main.hs
9595 buildable: True
tests/Main.hs+34−14
@@ -19,41 +19,61 @@ import qualified Language.PureScript as P
1919import Data.List (isSuffixOf)
2020import Control.Applicative
2121import Control.Monad
22−import System.Exit (exitSuccess, exitFailure)
22+import System.Exit
23+import System.Process
2324import System.FilePath (pathSeparator)
2425import System.Directory (getCurrentDirectory, getDirectoryContents)
26+import Text.Parsec (ParseError)
27+import qualified Paths_purescript as Paths
2528import qualified System.IO.UTF8 as U
2629import qualified Data.Map as M
2730
28−compile :: FilePath -> IO (Either String P.Environment)
29−compile inputFile = do
30− modules <- P.runIndentParser P.parseModules <$> U.readFile inputFile
31+preludeFilename :: IO FilePath
32+preludeFilename = Paths.getDataFileName "libraries/prelude/prelude.purs"
33+
34+readInput :: [FilePath] -> IO (Either ParseError [P.Module])
35+readInput inputFiles = fmap (fmap concat . sequence) $ forM inputFiles $ \inputFile -> do
36+ text <- U.readFile inputFile
37+ return $ P.runIndentParser P.parseModules text
38+
39+compile :: P.Options -> [FilePath] -> IO (Either String String)
40+compile opts inputFiles = do
41+ modules <- readInput inputFiles
3142 case modules of
3243 Left parseError -> do
3344 return (Left $ show parseError)
3445 Right ms -> do
35− case P.compile P.defaultOptions ms of
46+ case P.compile opts ms of
3647 Left typeError -> do
3748 return (Left typeError)
38− Right (_, _, env) -> do
39− return (Right env)
49+ Right (js, _, _) -> do
50+ return (Right js)
4051
41−assert :: FilePath -> (Either String P.Environment -> Maybe String) -> IO ()
42−assert inputFile f = do
43− e <- compile inputFile
44− case f e of
45− Just err -> exitFailure
52+assert :: P.Options -> [FilePath] -> (Either String String -> IO (Maybe String)) -> IO ()
53+assert opts inputFiles f = do
54+ e <- compile opts inputFiles
55+ maybeErr <- f e
56+ case maybeErr of
57+ Just err -> putStrLn err >> exitFailure
4658 Nothing -> return ()
4759
4860assertCompiles :: FilePath -> IO ()
4961assertCompiles inputFile = do
5062 putStrLn $ "assert " ++ inputFile ++ " compiles successfully"
51− assert inputFile $ either Just (const Nothing)
63+ prelude <- preludeFilename
64+ assert (P.defaultOptions { P.optionsRunMain = True }) [prelude, inputFile] $ either (return . Just) $ \js -> do
65+ (exitCode, out, err) <- readProcessWithExitCode "nodejs" [] js
66+ case exitCode of
67+ ExitSuccess -> putStrLn out >> return Nothing
68+ ExitFailure code -> return $ Just err
5269
5370assertDoesNotCompile :: FilePath -> IO ()
5471assertDoesNotCompile inputFile = do
5572 putStrLn $ "assert " ++ inputFile ++ " does not compile"
56− assert inputFile $ either (const Nothing) (const $ Just "Should not have compiled")
73+ assert P.defaultOptions [inputFile] $ \e ->
74+ case e of
75+ Left _ -> return Nothing
76+ Right _ -> return $ Just "Should not have compiled"
5777
5878main :: IO ()
5979main = do