on master help sign in
Merge pull request #1174 from bmsherman/master

Add laziness and some documentation

commite7e5638c872851c596a8f3cdf5117d75fbefb67d authorEdwin Brady <edwin.brady@gmail.com> dateSun 4 May 2014, 14:24 (12 years ago) parent58774fe50954d3
9 files changed+56−27
libs/effects/Effects.idr+2−2 libs/oldeffects/Effects.idr+1−1 libs/prelude/Prelude/Applicative.idr+5−2 libs/prelude/Prelude/Bool.idr+4−4 libs/prelude/Prelude/Either.idr+4−4 libs/prelude/Prelude/Foldable.idr+25−8 libs/prelude/Prelude/List.idr+2−2 libs/prelude/Prelude/Maybe.idr+10−3 libs/prelude/Prelude/Stream.idr+3−1
libs/effects/Effects.idr+2−2
@@ -337,8 +337,8 @@ mapVE f [] = pure []
337337mapVE f (x :: xs) = [| f x :: mapVE f xs |]
338338
339339
340−when : Applicative m => Bool -> ({xs} Eff m ()) -> {xs} Eff m ()
341−when True e = e
340+when : Applicative m => Bool -> Lazy ({xs} Eff m ()) -> {xs} Eff m ()
341+when True e = Force e
342342when False e = pure ()
343343
344344-- --------------------------------------------------------------------- [ EOF ]
libs/oldeffects/Effects.idr+1−1
@@ -303,6 +303,6 @@ mapE : Applicative m => (a -> Eff m xs b) -> List a -> Eff m xs (List b)
303303mapE f [] = pure []
304304mapE f (x :: xs) = [| f x :: mapE f xs |]
305305
306−when : Applicative m => Bool -> Eff m xs () -> Eff m xs ()
306+when : Applicative m => Bool -> Lazy (Eff m xs ()) -> Eff m xs ()
307307when True e = e
308308when False e = pure ()
libs/prelude/Prelude/Applicative.idr+5−2
@@ -38,8 +38,11 @@ class Applicative f => Alternative (f : Type -> Type) where
3838 empty : f a
3939 (<|>) : f a -> f a -> f a
4040
41+||| `guard a` is `pure ()` if `a` is `True` and `empty` if `a` is `False`
4142guard : Alternative f => Bool -> f ()
4243guard a = if a then pure () else empty
4344
44−when : Applicative f => Bool -> f () -> f ()
45−when a f = if a then f else pure ()
45+||| Conditionally execute an applicative expression
46+when : Applicative f => Bool -> Lazy (f ()) -> f ()
47+when True f = Force f
48+when False f = pure ()
libs/prelude/Prelude/Bool.idr+4−4
@@ -27,13 +27,13 @@ syntax if [test] then [t] else [e] = boolElim test (Delay t) (Delay e)
2727-- Boolean Operator Precedence
2828infixl 4 &&, ||
2929
30−||| Boolean OR
31−(||) : Bool -> Bool -> Bool
30+||| Boolean OR only evaluates the second argument if the first is `False`.
31+(||) : Bool -> Lazy Bool -> Bool
3232(||) False x = x
3333(||) True _ = True
3434
35−||| Boolean AND
36−(&&) : Bool -> Bool -> Bool
35+||| Boolean AND only evaluates the second argument if the first is `True`.
36+(&&) : Bool -> Lazy Bool -> Bool
3737(&&) True x = x
3838(&&) False _ = False
3939
libs/prelude/Prelude/Either.idr+4−4
@@ -39,9 +39,9 @@ choose False = Right oh
3939||| @ f the action to take on Left
4040||| @ g the action to take on Right
4141||| @ e the sum to analyze
42−either : (f : a -> c) -> (g : b -> c) -> (e : Either a b) -> c
43−either l r (Left x) = l x
44−either l r (Right x) = r x
42+either : (f : Lazy (a -> c)) -> (g : Lazy (b -> c)) -> (e : Either a b) -> c
43+either l r (Left x) = (Force l) x
44+either l r (Right x) = (Force r) x
4545
4646||| Keep the payloads of all Left constructors in a list of Eithers
4747lefts : List (Either a b) -> List a
@@ -74,7 +74,7 @@ fromEither (Right r) = r
7474
7575||| Convert a Maybe to an Either by using a default value in case of Nothing
7676||| @ e the default value
77−maybeToEither : (def : e) -> Maybe a -> Either e a
77+maybeToEither : (def : Lazy e) -> Maybe a -> Either e a
7878maybeToEither def (Just j) = Right j
7979maybeToEither def Nothing = Left def
8080
libs/prelude/Prelude/Foldable.idr+25−8
@@ -14,27 +14,44 @@ class Foldable (t : Type -> Type) where
1414foldl : Foldable t => (acc -> elt -> acc) -> acc -> t elt -> acc
1515foldl f z t = foldr (flip (.) . flip f) id t z
1616
17+||| Combine each element of a structure into a monoid
1718concat : (Foldable t, Monoid a) => t a -> a
1819concat = foldr (<+>) neutral
1920
21+||| Combine into a monoid the collective results of applying a function
22+||| to each element of a structure
2023concatMap : (Foldable t, Monoid m) => (a -> m) -> t a -> m
2124concatMap f = foldr ((<+>) . f) neutral
2225
23−and : Foldable t => t Bool -> Bool
24−and = foldr (&&) True
25−
26−or : Foldable t => t Bool -> Bool
27−or = foldr (||) False
28−
26+||| The conjunction of all elements of a structure containing
27+||| lazy boolean values. `and` short-circuits from left to right,
28+||| evaluating until either an element is `False` or no elements remain.
29+and : Foldable t => t (Lazy Bool) -> Bool
30+and = foldl (&&) True
31+
32+||| The disjunction of all elements of a structure containing
33+||| lazy boolean values. `or` short-circuits from left to right, evaluating
34+||| either until an element is `True` or no elements remain.
35+or : Foldable t => t (Lazy Bool) -> Bool
36+or = foldl (||) False
37+
38+||| The disjunction of the collective results of applying a
39+||| predicate to all elements of a structure. `any` short-circuits
40+||| from left to right.
2941any : Foldable t => (a -> Bool) -> t a -> Bool
30−any p = foldr ((||) . p) False
42+any p = foldl (\x => \y => x || p y) False
3143
44+||| The disjunction of the collective results of applying a
45+||| predicate to all elements of a structure. `all` short-circuits
46+||| from left to right.
3247all : Foldable t => (a -> Bool) -> t a -> Bool
33−all p = foldr ((&&) . p) True
48+all p = foldl (\x => \y => x && p y) True
3449
50+||| Add together all the elements of a structure
3551sum : (Foldable t, Num a) => t a -> a
3652sum = foldr (+) 0
3753
54+||| Multiply together all elements of a structure
3855product : (Foldable t, Num a) => t a -> a
3956product = foldr (*) 1
4057
libs/prelude/Prelude/List.idr+2−2
@@ -182,9 +182,9 @@ dropWhile p (x::xs) = if p x then dropWhile p xs else x::xs
182182||| @ nil what to return at the end of the list
183183||| @ cons what to do at each step of recursion
184184||| @ xs the list to recurse over
185−list : (nil : a) -> (cons : a -> List a -> a) -> (xs : List a) -> a
185+list : (nil : Lazy b) -> (cons : Lazy (a -> List a -> b)) -> (xs : List a) -> b
186186list nil cons [] = nil
187−list nil cons (x::xs) = cons x xs
187+list nil cons (x::xs) = (Force cons) x xs
188188
189189--------------------------------------------------------------------------------
190190-- Building (bigger) lists
libs/prelude/Prelude/Maybe.idr+10−3
@@ -28,25 +28,32 @@ isJust (Just j) = True
2828-- Misc
2929--------------------------------------------------------------------------------
3030
31−maybe : Lazy b -> (a -> b) -> Maybe a -> b
31+maybe : Lazy b -> Lazy (a -> b) -> Maybe a -> b
3232maybe n j Nothing = n
33−maybe n j (Just x) = j x
33+maybe n j (Just x) = (Force j) x
3434
35+||| Convert a `Maybe a` value to an `a` value by providing a default `a` value
36+||| in the case that the `Maybe` value is `Nothing`.
3537fromMaybe : (Lazy a) -> Maybe a -> a
3638fromMaybe def Nothing = def
3739fromMaybe def (Just j) = j
3840
39−toMaybe : Bool -> a -> Maybe a
41+||| Returns `Just` the given value if the conditional is `True`
42+||| and `Nothing` if the conditional is `False`.
43+toMaybe : Bool -> Lazy a -> Maybe a
4044toMaybe True j = Just j
4145toMaybe False j = Nothing
4246
4347justInjective : {x : t} -> {y : t} -> (Just x = Just y) -> x = y
4448justInjective refl = refl
4549
50+||| Convert a `Maybe a` value to an `a` value, using `neutral` in the case
51+||| that the `Maybe` value is `Nothing`.
4652lowerMaybe : Monoid a => Maybe a -> a
4753lowerMaybe Nothing = neutral
4854lowerMaybe (Just x) = x
4955
56+||| Returns `Nothing` when applied to `neutral`, and `Just` the value otherwise.
5057raiseToMaybe : (Monoid a, Eq a) => a -> Maybe a
5158raiseToMaybe x = if x == neutral then Nothing else Just x
5259
libs/prelude/Prelude/Stream.idr+3−1
@@ -31,8 +31,10 @@ take : (n : Nat) -> (xs : Stream a) -> Vect n a
3131take Z _ = []
3232take (S n) (x :: xs) = x :: (take n xs)
3333
34+||| Drop the first n elements from the stream
35+||| @ n how many elements to drop
3436%assert_total
35−drop : Nat -> Stream a -> Stream a
37+drop : (n : Nat) -> Stream a -> Stream a
3638drop Z xs = xs
3739drop (S k) (x::xs) = drop k xs
3840