git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #488 from purescript/mkfn

Add mkFn and runFn for arities up to 10 #486

commit6a7ebd275626cf71a1e9bf3e89ceb73c79b69069 authorPhil Freeman <paf31@cantab.net> dateWed 16 Jul 2014, 17:44 (12 years ago) parent8b357ffb59f6d2
3 files changed+177−27
examples/passing/MultiArgFunctions.purs+6−1 prelude/prelude.purs+169−24 src/Language/PureScript/Optimizer/Inliner.hs+2−2
examples/passing/MultiArgFunctions.purs+6−1
@@ -11,4 +11,9 @@ main = do
1111 runFn3 (mkFn3 $ \a b c -> trace $ show [a, b, c]) 1 2 3
1212 runFn4 (mkFn4 $ \a b c d -> trace $ show [a, b, c, d]) 1 2 3 4
1313 runFn5 (mkFn5 $ \a b c d e -> trace $ show [a, b, c, d, e]) 1 2 3 4 5
14− trace "Done!"
14+ runFn6 (mkFn6 $ \a b c d e f -> trace $ show [a, b, c, d, e, f]) 1 2 3 4 5 6
15+ runFn7 (mkFn7 $ \a b c d e f g -> trace $ show [a, b, c, d, e, f, g]) 1 2 3 4 5 6 7
16+ runFn8 (mkFn8 $ \a b c d e f g h -> trace $ show [a, b, c, d, e, f, g, h]) 1 2 3 4 5 6 7 8
17+ runFn9 (mkFn9 $ \a b c d e f g h i -> trace $ show [a, b, c, d, e, f, g, h, i]) 1 2 3 4 5 6 7 8 9
18+ runFn10 (mkFn10 $ \a b c d e f g h i j-> trace $ show [a, b, c, d, e, f, g, h, i, j]) 1 2 3 4 5 6 7 8 9 10
19+ trace "Done!"
prelude/prelude.purs+169−24
@@ -490,88 +490,128 @@ module Data.Function where
490490 foreign import data Fn3 :: * -> * -> * -> * -> *
491491 foreign import data Fn4 :: * -> * -> * -> * -> * -> *
492492 foreign import data Fn5 :: * -> * -> * -> * -> * -> * -> *
493+ foreign import data Fn6 :: * -> * -> * -> * -> * -> * -> * -> *
494+ foreign import data Fn7 :: * -> * -> * -> * -> * -> * -> * -> * -> *
495+ foreign import data Fn8 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> *
496+ foreign import data Fn9 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> *
497+ foreign import data Fn10 :: * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> * -> *
493498
494499 foreign import mkFn0
495− "function mkFn0(f) {\
500+ "function mkFn0(fn) {\
496501 \ return function() {\
497− \ return f({});\
502+ \ return fn({});\
498503 \ };\
499504 \}" :: forall a. (Unit -> a) -> Fn0 a
500505
501506 foreign import mkFn1
502− "function mkFn1(f) {\
507+ "function mkFn1(fn) {\
503508 \ return function(a) {\
504− \ return f(a);\
509+ \ return fn(a);\
505510 \ };\
506511 \}" :: forall a b. (a -> b) -> Fn1 a b
507512
508513 foreign import mkFn2
509− "function mkFn2(f) {\
514+ "function mkFn2(fn) {\
510515 \ return function(a, b) {\
511− \ return f(a)(b);\
516+ \ return fn(a)(b);\
512517 \ };\
513518 \}" :: forall a b c. (a -> b -> c) -> Fn2 a b c
514519
515520 foreign import mkFn3
516− "function mkFn3(f) {\
521+ "function mkFn3(fn) {\
517522 \ return function(a, b, c) {\
518− \ return f(a)(b)(c);\
523+ \ return fn(a)(b)(c);\
519524 \ };\
520525 \}" :: forall a b c d. (a -> b -> c -> d) -> Fn3 a b c d
521526
522527 foreign import mkFn4
523− "function mkFn4(f) {\
528+ "function mkFn4(fn) {\
524529 \ return function(a, b, c, d) {\
525− \ return f(a)(b)(c)(d);\
530+ \ return fn(a)(b)(c)(d);\
526531 \ };\
527532 \}" :: forall a b c d e. (a -> b -> c -> d -> e) -> Fn4 a b c d e
528533
529534 foreign import mkFn5
530− "function mkFn5(f) {\
535+ "function mkFn5(fn) {\
531536 \ return function(a, b, c, d, e) {\
532− \ return f(a)(b)(c)(d)(e);\
537+ \ return fn(a)(b)(c)(d)(e);\
533538 \ };\
534539 \}" :: forall a b c d e f. (a -> b -> c -> d -> e -> f) -> Fn5 a b c d e f
535540
541+ foreign import mkFn6
542+ "function mkFn6(fn) {\
543+ \ return function(a, b, c, d, e, f) {\
544+ \ return fn(a)(b)(c)(d)(e)(f);\
545+ \ };\
546+ \}" :: forall a b c d e f g. (a -> b -> c -> d -> e -> f -> g) -> Fn6 a b c d e f g
547+
548+ foreign import mkFn7
549+ "function mkFn7(fn) {\
550+ \ return function(a, b, c, d, e, f, g) {\
551+ \ return fn(a)(b)(c)(d)(e)(f)(g);\
552+ \ };\
553+ \}" :: forall a b c d e f g h. (a -> b -> c -> d -> e -> f -> g -> h) -> Fn7 a b c d e f g h
554+
555+ foreign import mkFn8
556+ "function mkFn8(fn) {\
557+ \ return function(a, b, c, d, e, f, g, h) {\
558+ \ return fn(a)(b)(c)(d)(e)(f)(g)(h);\
559+ \ };\
560+ \}" :: forall a b c d e f g h i. (a -> b -> c -> d -> e -> f -> g -> h -> i) -> Fn8 a b c d e f g h i
561+
562+ foreign import mkFn9
563+ "function mkFn9(fn) {\
564+ \ return function(a, b, c, d, e, f, g, h, i) {\
565+ \ return fn(a)(b)(c)(d)(e)(f)(g)(h)(i);\
566+ \ };\
567+ \}" :: forall a b c d e f g h i j. (a -> b -> c -> d -> e -> f -> g -> h -> i -> j) -> Fn9 a b c d e f g h i j
568+
569+ foreign import mkFn10
570+ "function mkFn10(fn) {\
571+ \ return function(a, b, c, d, e, f, g, h, i, j) {\
572+ \ return fn(a)(b)(c)(d)(e)(f)(g)(h)(i)(j);\
573+ \ };\
574+ \}" :: forall a b c d e f g h i j k. (a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k) -> Fn10 a b c d e f g h i j k
575+
536576 foreign import runFn0
537− "function runFn0(f) {\
538− \ return f();\
577+ "function runFn0(fn) {\
578+ \ return fn();\
539579 \}" :: forall a. Fn0 a -> a
540580
541581 foreign import runFn1
542− "function runFn1(f) {\
582+ "function runFn1(fn) {\
543583 \ return function(a) {\
544− \ return f(a);\
584+ \ return fn(a);\
545585 \ };\
546586 \}" :: forall a b. Fn1 a b -> a -> b
547587
548588 foreign import runFn2
549− "function runFn2(f) {\
589+ "function runFn2(fn) {\
550590 \ return function(a) {\
551591 \ return function(b) {\
552− \ return f(a, b);\
592+ \ return fn(a, b);\
553593 \ };\
554594 \ };\
555595 \}" :: forall a b c. Fn2 a b c -> a -> b -> c
556596
557597 foreign import runFn3
558− "function runFn3(f) {\
598+ "function runFn3(fn) {\
559599 \ return function(a) {\
560600 \ return function(b) {\
561601 \ return function(c) {\
562− \ return f(a, b, c);\
602+ \ return fn(a, b, c);\
563603 \ };\
564604 \ };\
565605 \ };\
566606 \}" :: forall a b c d. Fn3 a b c d -> a -> b -> c -> d
567607
568608 foreign import runFn4
569− "function runFn4(f) {\
609+ "function runFn4(fn) {\
570610 \ return function(a) {\
571611 \ return function(b) {\
572612 \ return function(c) {\
573613 \ return function(d) {\
574− \ return f(a, b, c, d);\
614+ \ return fn(a, b, c, d);\
575615 \ };\
576616 \ };\
577617 \ };\
@@ -579,13 +619,13 @@ module Data.Function where
579619 \}" :: forall a b c d e. Fn4 a b c d e -> a -> b -> c -> d -> e
580620
581621 foreign import runFn5
582− "function runFn5(f) {\
622+ "function runFn5(fn) {\
583623 \ return function(a) {\
584624 \ return function(b) {\
585625 \ return function(c) {\
586626 \ return function(d) {\
587627 \ return function(e) {\
588− \ return f(a, b, c, d, e);\
628+ \ return fn(a, b, c, d, e);\
589629 \ };\
590630 \ };\
591631 \ };\
@@ -593,6 +633,111 @@ module Data.Function where
593633 \ };\
594634 \}" :: forall a b c d e f. Fn5 a b c d e f -> a -> b -> c -> d -> e -> f
595635
636+ foreign import runFn6
637+ "function runFn6(fn) {\
638+ \ return function(a) {\
639+ \ return function(b) {\
640+ \ return function(c) {\
641+ \ return function(d) {\
642+ \ return function(e) {\
643+ \ return function(f) {\
644+ \ return fn(a, b, c, d, e, f);\
645+ \ };\
646+ \ };\
647+ \ };\
648+ \ };\
649+ \ };\
650+ \ };\
651+ \}" :: forall a b c d e f g. Fn6 a b c d e f g -> a -> b -> c -> d -> e -> f -> g
652+
653+ foreign import runFn7
654+ "function runFn7(fn) {\
655+ \ return function(a) {\
656+ \ return function(b) {\
657+ \ return function(c) {\
658+ \ return function(d) {\
659+ \ return function(e) {\
660+ \ return function(f) {\
661+ \ return function(g) {\
662+ \ return fn(a, b, c, d, e, f, g);\
663+ \ };\
664+ \ };\
665+ \ };\
666+ \ };\
667+ \ };\
668+ \ };\
669+ \ };\
670+ \}" :: forall a b c d e f g h. Fn7 a b c d e f g h -> a -> b -> c -> d -> e -> f -> g -> h
671+
672+ foreign import runFn8
673+ "function runFn8(fn) {\
674+ \ return function(a) {\
675+ \ return function(b) {\
676+ \ return function(c) {\
677+ \ return function(d) {\
678+ \ return function(e) {\
679+ \ return function(f) {\
680+ \ return function(g) {\
681+ \ return function(h) {\
682+ \ return fn(a, b, c, d, e, f, g, h);\
683+ \ };\
684+ \ };\
685+ \ };\
686+ \ };\
687+ \ };\
688+ \ };\
689+ \ };\
690+ \ };\
691+ \}" :: forall a b c d e f g h i. Fn8 a b c d e f g h i -> a -> b -> c -> d -> e -> f -> g -> h -> i
692+
693+ foreign import runFn9
694+ "function runFn9(fn) {\
695+ \ return function(a) {\
696+ \ return function(b) {\
697+ \ return function(c) {\
698+ \ return function(d) {\
699+ \ return function(e) {\
700+ \ return function(f) {\
701+ \ return function(g) {\
702+ \ return function(h) {\
703+ \ return function(i) {\
704+ \ return fn(a, b, c, d, e, f, g, h, i);\
705+ \ };\
706+ \ };\
707+ \ };\
708+ \ };\
709+ \ };\
710+ \ };\
711+ \ };\
712+ \ };\
713+ \ };\
714+ \}" :: forall a b c d e f g h i j. Fn9 a b c d e f g h i j -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j
715+
716+ foreign import runFn10
717+ "function runFn10(fn) {\
718+ \ return function(a) {\
719+ \ return function(b) {\
720+ \ return function(c) {\
721+ \ return function(d) {\
722+ \ return function(e) {\
723+ \ return function(f) {\
724+ \ return function(g) {\
725+ \ return function(h) {\
726+ \ return function(i) {\
727+ \ return function(j) {\
728+ \ return fn(a, b, c, d, e, f, g, h, i, j);\
729+ \ };\
730+ \ };\
731+ \ };\
732+ \ };\
733+ \ };\
734+ \ };\
735+ \ };\
736+ \ };\
737+ \ };\
738+ \ };\
739+ \}" :: forall a b c d e f g h i j k. Fn10 a b c d e f g h i j k -> a -> b -> c -> d -> e -> f -> g -> h -> i -> j -> k
740+
596741module Data.Eq where
597742
598743 data Ref a = Ref a
src/Language/PureScript/Optimizer/Inliner.hs+2−2
@@ -124,7 +124,7 @@ inlineCommonOperators = applyAll $
124124 , binary C.boolLikeBoolean (C.||) Or
125125 , unary C.boolLikeBoolean C.not Not
126126 ] ++
127− [ fn | i <- [0..5], fn <- [ mkFn i, runFn i ] ]
127+ [ fn | i <- [0..10], fn <- [ mkFn i, runFn i ] ]
128128 where
129129 binary :: String -> String -> BinaryOperator -> JS -> JS
130130 binary dictName opString op = everywhereOnJS convert
@@ -170,7 +170,7 @@ inlineCommonOperators = applyAll $
170170 Nothing -> orig
171171 convert other = other
172172 collectArgs :: Int -> [String] -> JS -> Maybe ([String], [JS])
173− collectArgs 1 acc (JSFunction Nothing [oneArg] (JSBlock js)) | length acc == n - 1 = Just (reverse (oneArg : acc), js)
173+ collectArgs 1 acc (JSFunction Nothing [oneArg] (JSBlock js)) | length acc == n - 1 = Just (reverse (oneArg : acc), js)
174174 collectArgs m acc (JSFunction Nothing [oneArg] (JSBlock [JSReturn ret])) = collectArgs (m - 1) (oneArg : acc) ret
175175 collectArgs _ _ _ = Nothing
176176