2
2
3
3
module Data.Lazy where
4
4
5
+ import Prelude
6
+
5
7
import Control.Comonad (Comonad )
6
8
import Control.Extend (Extend )
7
9
import Data.Monoid (Monoid , mempty )
@@ -20,54 +22,30 @@ import qualified Control.Lazy as CL
20
22
foreign import data Lazy :: * -> *
21
23
22
24
-- | 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
42
26
43
27
-- | 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
50
29
51
30
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
53
32
zero = defer \_ -> zero
54
- (*) a b = defer \_ -> force a * force b
33
+ mul a b = defer \_ -> force a * force b
55
34
one = defer \_ -> one
56
35
57
36
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
59
38
60
39
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
62
41
mod a b = defer \_ -> force a `mod` force b
63
42
64
43
instance divisionRingLazy :: (DivisionRing a ) => DivisionRing (Lazy a )
65
44
66
45
instance numLazy :: (Num a ) => Num (Lazy a )
67
46
68
47
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)
71
49
72
50
instance ordLazy :: (Ord a ) => Ord (Lazy a ) where
73
51
compare x y = compare (force x) (force y)
@@ -77,40 +55,32 @@ instance boundedLazy :: (Bounded a) => Bounded (Lazy a) where
77
55
bottom = defer \_ -> bottom
78
56
79
57
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
81
59
82
60
instance monoidLazy :: (Monoid a ) => Monoid (Lazy a ) where
83
61
mempty = defer \_ -> mempty
84
62
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
+
98
68
instance functorLazy :: Functor Lazy where
99
- (<$>) f l = defer \_ -> f (force l)
69
+ map f l = defer \_ -> f (force l)
100
70
101
71
instance applyLazy :: Apply Lazy where
102
- (<*>) f x = defer \_ -> force f $ force x
72
+ apply f x = defer \_ -> force f $ force x
103
73
104
74
instance applicativeLazy :: Applicative Lazy where
105
75
pure a = defer \_ -> a
106
76
107
77
instance bindLazy :: Bind Lazy where
108
- (>>=) l f = defer \_ -> force <<< f <<< force $ l
78
+ bind l f = defer \_ -> force <<< f <<< force $ l
109
79
110
80
instance monadLazy :: Monad Lazy
111
81
112
82
instance extendLazy :: Extend Lazy where
113
- (<<=) f x = defer \_ -> f x
83
+ extend f x = defer \_ -> f x
114
84
115
85
instance comonadLazy :: Comonad Lazy where
116
86
extract = force
0 commit comments