You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: text/chapter3.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -371,7 +371,7 @@ $ spago repl
371
371
> import Data.List
372
372
> :type Cons
373
373
374
-
forall a. a -> List a -> List a
374
+
forall (a :: Type). a -> List a -> List a
375
375
```
376
376
377
377
This type signature says that `Cons` takes a value of some type `a`, takes a list of elements of type `a`, and returns a new list with entries of the same type. Let's specialize this with `a` as our `Entry` type:
@@ -506,11 +506,11 @@ $ spago repl
506
506
> import Data.List
507
507
> :type filter
508
508
509
-
forall a. (a -> Boolean) -> List a -> List a
509
+
forall (a :: Type). (a -> Boolean) -> List a -> List a
510
510
511
511
> :type head
512
512
513
-
forall a. List a -> Maybe a
513
+
forall (a :: Type). List a -> Maybe a
514
514
```
515
515
516
516
Let's pick apart these two types to understand their meaning.
Copy file name to clipboardExpand all lines: text/chapter4.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,13 +112,13 @@ Let's look at the type of `map`:
112
112
113
113
```text
114
114
> :type map
115
-
forall a b f. Functor f => (a -> b) -> f a -> f b
115
+
forall (f :: Type -> Type) (a :: Type) (b :: Type). Functor f => (a -> b) -> f a -> f b
116
116
```
117
117
118
118
The type of `map` is actually more general than we need in this chapter. For our purposes, we can treat `map` as if it had the following less general type:
119
119
120
120
```text
121
-
forall a b. (a -> b) -> Array a -> Array b
121
+
forall (a :: Type) (b :: Type). (a -> b) -> Array a -> Array b
122
122
```
123
123
124
124
This type says that we can choose any two types, `a` and `b`, with which to apply the `map` function. `a` is the type of elements in the source array, and `b` is the type of elements in the target array. In particular, there is no reason why `map` has to preserve the type of the array elements. We can use `map` or `<$>` to transform integers to strings, for example:
@@ -195,7 +195,7 @@ Another standard function on arrays is the `concat` function, defined in `Data.A
195
195
> import Data.Array
196
196
197
197
> :type concat
198
-
forall a. Array (Array a) -> Array a
198
+
forall (a :: Type). Array (Array a) -> Array a
199
199
200
200
> concat [[1, 2, 3], [4, 5], [6]]
201
201
[1, 2, 3, 4, 5, 6]
@@ -209,7 +209,7 @@ Let's see it in action:
209
209
> import Data.Array
210
210
211
211
> :type concatMap
212
-
forall a b. (a -> Array b) -> Array a -> Array b
212
+
forall (a :: Type) (b :: Type). (a -> Array b) -> Array a -> Array b
213
213
214
214
> concatMap (\n -> [n, n * n]) (1 .. 5)
215
215
[1,1,2,4,3,9,4,16,5,25]
@@ -335,7 +335,7 @@ Just like `pure`, we can apply the `guard` function in PSCi to understand how it
335
335
> import Control.Alternative
336
336
337
337
> :type guard
338
-
forall m. Alternative m => Boolean -> m Unit
338
+
forall (m :: Type -> Type). Alternative m => Boolean -> m Unit
339
339
```
340
340
341
341
In our case, we can assume that PSCi reported the following type:
@@ -377,19 +377,19 @@ Start by importing the `Data.Foldable` module and inspecting the types of the `f
377
377
> import Data.Foldable
378
378
379
379
> :type foldl
380
-
forall a b f. Foldable f => (b -> a -> b) -> b -> f a -> b
380
+
forall (f :: Type -> Type) (a :: Type) (b :: Type). Foldable f => (b -> a -> b) -> b -> f a -> b
381
381
382
382
> :type foldr
383
-
forall a b f. Foldable f => (a -> b -> b) -> b -> f a -> b
383
+
forall (f :: Type -> Type) (a :: Type) (b :: Type). Foldable f => (a -> b -> b) -> b -> f a -> b
384
384
```
385
385
386
-
These types are more general than we are interested in right now. For this chapter, we can assume that PSCi has given the following (more specific) answer:
386
+
These types are more general than we are interested in right now. For this chapter, we can simplify and assume the following (more specific) type signatures:
Copy file name to clipboardExpand all lines: text/chapter6.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -381,7 +381,7 @@ To see this, try using one of the standard type classes like `Semiring` in PSCi:
381
381
> import Prelude
382
382
383
383
> :type \x -> x + x
384
-
forall a. Semiring a => a -> a
384
+
forall (a :: Type). Semiring a => a -> a
385
385
```
386
386
387
387
Here, we might have annotated this function as `Int -> Int` or `Number -> Number`, but PSCi shows us that the most general type works for any `Semiring`, allowing us to use our function with both `Int`s and `Number.
@@ -540,7 +540,7 @@ This hint is enough for the compiler to infer the correct type for our generic t
540
540
541
541
```text
542
542
>:type genericTail
543
-
forall stream element.Stream stream element => stream ->Maybe stream
543
+
forall (stream ::Type) (element::Type).Stream stream element => stream ->Maybe stream
0 commit comments