Skip to content

Commit 19f3bed

Browse files
committed
Updates for 0.7
1 parent b85b026 commit 19f3bed

File tree

2 files changed

+45
-49
lines changed

2 files changed

+45
-49
lines changed

src/Data/Lazy.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* global exports */
2+
"use strict";
3+
4+
// module Data.Lazy
5+
6+
exports.defer = function(thunk) {
7+
if (this instanceof defer) {
8+
this.thunk = thunk;
9+
return this;
10+
} else {
11+
return new defer(thunk);
12+
}
13+
};
14+
15+
exports.defer.prototype.force = function () {
16+
var value = this.thunk();
17+
delete this.thunk;
18+
this.force = function () {
19+
return value;
20+
};
21+
return value;
22+
};
23+
24+
exports.force = function(l) {
25+
return l.force();
26+
};

src/Data/Lazy.purs

Lines changed: 19 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
module Data.Lazy where
44

5+
import Prelude
6+
57
import Control.Comonad (Comonad)
68
import Control.Extend (Extend)
79
import Data.Monoid (Monoid, mempty)
@@ -20,54 +22,30 @@ import qualified Control.Lazy as CL
2022
foreign import data Lazy :: * -> *
2123

2224
-- | Defer a computation, creating a `Lazy` value.
23-
foreign import defer
24-
"""
25-
function defer(thunk) {
26-
if (this instanceof defer) {
27-
this.thunk = thunk;
28-
return this;
29-
} else {
30-
return new defer(thunk);
31-
}
32-
}
33-
defer.prototype.force = function () {
34-
var value = this.thunk();
35-
delete this.thunk;
36-
this.force = function () {
37-
return value;
38-
};
39-
return value;
40-
};
41-
""" :: forall a. (Unit -> a) -> Lazy a
25+
foreign import defer :: forall a. (Unit -> a) -> Lazy a
4226

4327
-- | Force evaluation of a `Lazy` value.
44-
foreign import force
45-
"""
46-
function force(l) {
47-
return l.force();
48-
}
49-
""" :: forall a. Lazy a -> a
28+
foreign import force :: forall a. Lazy a -> a
5029

5130
instance semiringLazy :: (Semiring a) => Semiring (Lazy a) where
52-
(+) a b = defer \_ -> force a + force b
31+
add a b = defer \_ -> force a + force b
5332
zero = defer \_ -> zero
54-
(*) a b = defer \_ -> force a * force b
33+
mul a b = defer \_ -> force a * force b
5534
one = defer \_ -> one
5635

5736
instance ringLazy :: (Ring a) => Ring (Lazy a) where
58-
(-) a b = defer \_ -> force a - force b
37+
sub a b = defer \_ -> force a - force b
5938

6039
instance moduloSemiringLazy :: (ModuloSemiring a) => ModuloSemiring (Lazy a) where
61-
(/) a b = defer \_ -> force a / force b
40+
div a b = defer \_ -> force a / force b
6241
mod a b = defer \_ -> force a `mod` force b
6342

6443
instance divisionRingLazy :: (DivisionRing a) => DivisionRing (Lazy a)
6544

6645
instance numLazy :: (Num a) => Num (Lazy a)
6746

6847
instance eqLazy :: (Eq a) => Eq (Lazy a) where
69-
(==) x y = (force x) == (force y)
70-
(/=) x y = not (x == y)
48+
eq x y = (force x) == (force y)
7149

7250
instance ordLazy :: (Ord a) => Ord (Lazy a) where
7351
compare x y = compare (force x) (force y)
@@ -77,40 +55,32 @@ instance boundedLazy :: (Bounded a) => Bounded (Lazy a) where
7755
bottom = defer \_ -> bottom
7856

7957
instance semigroupLazy :: (Semigroup a) => Semigroup (Lazy a) where
80-
(<>) a b = defer \_ -> force a <> force b
58+
append a b = defer \_ -> force a <> force b
8159

8260
instance monoidLazy :: (Monoid a) => Monoid (Lazy a) where
8361
mempty = defer \_ -> mempty
8462

85-
instance latticeLazy :: (Lattice a) => Lattice (Lazy a) where
86-
sup a b = defer \_ -> force a `sup` force b
87-
inf a b = defer \_ -> force a `inf` force b
88-
89-
instance boundedLatticeLazy :: (BoundedLattice a) => BoundedLattice (Lazy a)
90-
91-
instance complementedLatticeLazy :: (ComplementedLattice a) => ComplementedLattice (Lazy a) where
92-
not a = defer \_ -> not (force a)
93-
94-
instance distributiveLatticeLazy :: (DistributiveLattice a) => DistributiveLattice (Lazy a)
95-
96-
instance booleanAlgebraLazy :: (BooleanAlgebra a) => BooleanAlgebra (Lazy a)
97-
63+
instance booleanAlgebraLazy :: (BooleanAlgebra a) => BooleanAlgebra (Lazy a) where
64+
conj a b = conj <$> a <*> b
65+
disj a b = disj <$> a <*> b
66+
not a = not <$> a
67+
9868
instance functorLazy :: Functor Lazy where
99-
(<$>) f l = defer \_ -> f (force l)
69+
map f l = defer \_ -> f (force l)
10070

10171
instance applyLazy :: Apply Lazy where
102-
(<*>) f x = defer \_ -> force f $ force x
72+
apply f x = defer \_ -> force f $ force x
10373

10474
instance applicativeLazy :: Applicative Lazy where
10575
pure a = defer \_ -> a
10676

10777
instance bindLazy :: Bind Lazy where
108-
(>>=) l f = defer \_ -> force <<< f <<< force $ l
78+
bind l f = defer \_ -> force <<< f <<< force $ l
10979

11080
instance monadLazy :: Monad Lazy
11181

11282
instance extendLazy :: Extend Lazy where
113-
(<<=) f x = defer \_ -> f x
83+
extend f x = defer \_ -> f x
11484

11585
instance comonadLazy :: Comonad Lazy where
11686
extract = force

0 commit comments

Comments
 (0)