git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix == on lists, use >>> and <<< for category composition
commitdaacf6bdfd1b5228733c0ce05f77fae08d46da7d authorPhil Freeman <paf31@cantab.net> dateTue 4 Feb 2014, 09:54 (13 years ago) parentca5a7c7
3 files changed+46−47
examples/passing/Category.purs−15 libraries/prelude/prelude.purs+33−28 src/Language/PureScript/CodeGen/Optimize.hs+13−4
examples/passing/Category.pursdeleted−15
@@ -1,15 +0,0 @@
1−module Category where
2−
3−class Category c where
4− id :: forall x. c x x
5− (<|) :: forall x y z. c y z -> c x y -> c x z
6−
7−instance Category (->) where
8− id = Prelude.id
9− (<|) = Prelude.(<|)
10−
11−module Main where
12−
13−import Category
14−
15−main = Trace.trace (id "Done")
libraries/prelude/prelude.purs+33−28
@@ -6,29 +6,33 @@ module Prelude where
66 foreign import data Array :: * -> *
77 foreign import data Function :: * -> * -> *
88
9− id :: forall a. a -> a
10− id = \x -> x
11−
129 flip :: forall a b c. (a -> b -> c) -> b -> a -> c
13− flip = \f -> \b -> \a -> f a b
10+ flip f b a = f a b
1411
1512 konst :: forall a b. a -> b -> a
16− konst = \a -> \b -> a
13+ konst a _ = a
1714
18− (|>) :: forall a b c. (a -> b) -> (b -> c) -> a -> c
19− (|>) = \f -> \g -> \a -> g (f a)
15+ infixr 5 >>>
16+ infixr 5 <<<
2017
21− infixr 5 |>
18+ class Category a where
19+ id :: forall t. a t t
20+ (<<<) :: forall b c d. a c d -> a b c -> a b d
21+ (>>>) :: forall b c d. a b c -> a c d -> a b d
2222
23− (<|) :: forall a b c. (b -> c) -> (a -> b) -> a -> c
24− (<|) = flip (|>)
23+ instance Category (->) where
24+ id x = x
25+ (<<<) f g x = f (g x)
26+ (>>>) f g x = g (f x)
2527
26− infixr 5 <|
28+ infixr 1000 $
29+ infixl 1000 #
2730
2831 ($) :: forall a b. (a -> b) -> a -> b
2932 ($) f x = f x
3033
31− infixr 1000 $
34+ (#) :: forall a b. a -> (a -> b) -> b
35+ (#) x f = f x
3236
3337 class Show a where
3438 show :: a -> String
@@ -169,8 +173,10 @@ module Prelude where
169173 (/=) = unsafeRefIneq
170174
171175 instance (Eq a) => Eq [a] where
172− (==) = unsafeRefEq
173− (/=) = unsafeRefIneq
176+ (==) [] [] = true
177+ (==) (x:xs) (y:ys) = x == y && xs == ys
178+ (==) _ _ = false
179+ (/=) xs ys = not (xs == ys)
174180
175181 infixl 3 <
176182 infixl 3 >
@@ -213,21 +219,17 @@ module Prelude where
213219 (<=) = numLessEq
214220 (>=) = numGreaterEq
215221
216− infixl 8 <<
217− infixl 8 >>
218− infixl 8 >>>
219−
220222 infixl 10 &
221223 infixl 10 |
222− infixl 10 ^
224+ infixl 10 ^
223225
224226 class Bits b where
225− (<<) :: b -> Number -> b
226− (>>) :: b -> Number -> b
227− (>>>) :: b -> Number -> b
228227 (&) :: b -> b -> b
229228 (|) :: b -> b -> b
230229 (^) :: b -> b -> b
230+ shl :: b -> Number -> b
231+ shr :: b -> Number -> b
232+ zshr :: b -> Number -> b
231233 complement :: b -> b
232234
233235 foreign import numShl "function numShl(n1) {\
@@ -242,7 +244,7 @@ module Prelude where
242244 \ };\
243245 \}" :: Number -> Number -> Number
244246
245− foreign import numZfShr "function numZfShr(n1) {\
247+ foreign import numZshr "function numZshr(n1) {\
246248 \ return function(n2) {\
247249 \ return n1 >>> n2;\
248250 \ };\
@@ -271,12 +273,12 @@ module Prelude where
271273 \}" :: Number -> Number
272274
273275 instance Bits Number where
274− (<<) = numShl
275− (>>) = numShr
276− (>>>) = numZfShr
277276 (&) = numAnd
278277 (|) = numOr
279278 (^) = numXor
279+ shl = numShl
280+ shr = numShr
281+ zshr = numZshr
280282 complement = numComplement
281283
282284 infixl 4 !!
@@ -326,6 +328,8 @@ module Prelude where
326328
327329module Maybe where
328330
331+ import Prelude
332+
329333 data Maybe a = Nothing | Just a
330334
331335 maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
@@ -333,7 +337,7 @@ module Maybe where
333337 maybe _ f (Just a) = f a
334338
335339 fromMaybe :: forall a. a -> Maybe a -> a
336− fromMaybe a = maybe a Prelude.id
340+ fromMaybe a = maybe a (Prelude.id :: forall a. a -> a)
337341
338342 instance Prelude.Monad Maybe where
339343 ret = Just
@@ -503,6 +507,7 @@ module Arrays where
503507
504508module Tuple where
505509
510+ import Prelude
506511 import Arrays
507512
508513 type Tuple a b = { fst :: a, snd :: b }
@@ -514,7 +519,7 @@ module Tuple where
514519 uncurry f t = f t.fst t.snd
515520
516521 tuple :: forall a b. a -> b -> Tuple a b
517− tuple = curry Prelude.id
522+ tuple = curry (\t -> t)
518523
519524 zip :: forall a b. [a] -> [b] -> [Tuple a b]
520525 zip = zipWith tuple
src/Language/PureScript/CodeGen/Optimize.hs+13−4
@@ -27,7 +27,7 @@
2727--
2828-- * Inlining variables
2929--
30−-- * Inline Prelude.($), Prelude.(++), Prelude.(!!)
30+-- * Inline Prelude.($), Prelude.(#), Prelude.(++), Prelude.(!!)
3131--
3232-- * Inlining primitive Javascript operators
3333--
@@ -63,6 +63,7 @@ optimize opts | optionsNoOptimizations opts = id
6363 , etaConvert
6464 , inlineVariables
6565 , inlineOperator "$" $ \f x -> JSApp f [x]
66+ , inlineOperator "#" $ \x f -> JSApp f [x]
6667 , inlineOperator "!!" $ flip JSIndexer
6768 , inlineOperator "++" $ JSBinary Add
6869 , inlineCommonOperators ]
@@ -330,9 +331,9 @@ inlineCommonOperators = applyAll
330331 , binary "==" "Eq" tyBoolean EqualTo
331332 , binary "/=" "Eq" tyBoolean NotEqualTo
332333
333− , binary "<<" "Bits" tyNumber ShiftLeft
334− , binary ">>" "Bits" tyNumber ShiftRight
335− , binary ">>>" "Bits" tyNumber ZeroFillShiftRight
334+ , binaryFunction "shl" "Bits" tyNumber ShiftLeft
335+ , binaryFunction "shr" "Bits" tyNumber ShiftRight
336+ , binaryFunction "zshr" "Bits" tyNumber ZeroFillShiftRight
336337 , binary "&" "Bits" tyNumber BitwiseAnd
337338 , binary "|" "Bits" tyNumber BitwiseOr
338339 , binary "^" "Bits" tyNumber BitwiseXor
@@ -352,6 +353,14 @@ inlineCommonOperators = applyAll
352353 isOp (JSAccessor longForm (JSVar "Prelude")) | longForm == identToJs (Op opString) = True
353354 isOp (JSIndexer (JSStringLiteral op') (JSVar "Prelude")) | opString == op' = True
354355 isOp _ = False
356+ binaryFunction :: String -> String -> Type -> BinaryOperator -> JS -> JS
357+ binaryFunction fnName className classTy op = everywhere (mkT convert)
358+ where
359+ convert :: JS -> JS
360+ convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict className classTy dict = JSBinary op x y
361+ convert other = other
362+ isOp (JSAccessor fnName' (JSVar "Prelude")) | fnName == fnName' = True
363+ isOp _ = False
355364 unary :: String -> String -> Type -> UnaryOperator -> JS -> JS
356365 unary fnName className classTy op = everywhere (mkT convert)
357366 where