@@ -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