Skip to content

Commit 2ae4723

Browse files
committed
Explain why eta-reduced type synonyms are good
1 parent 5f3d43b commit 2ae4723

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

errors/PartiallyAppliedSynonym.md

+22
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,25 @@ um = UserNT'Maybe { firstName: Just "test" }
7070
And to fix the second example, we can't simply fully apply `X`, as that would produce a kind error, `Could not match kind Type -> Type with kind Type`. The `f` in `IdentityF f a` needs to be kind `Type -> Type`, but here it is kind `Type`. The most appropriate solution is to simply use `A` or `B` as the type synonym, as demonstrated in `alsoOk` and `alsoAlsoOk`.
7171

7272
## Notes
73+
74+
Eta-expanding makes it impossible to use the synonym in some situations in which it would otherwise work, and unlike some other restrictions we impose on ourselves using the type system, this one is never useful - it only prevents you doing things you might want to do, not things you might want to avoid.
75+
76+
An example, from transformers, if State was defined as follows, it would be impossible to use `State` in some situations.
77+
78+
```
79+
-- note the `a`, in the library this is omitted on both sides
80+
type State s a = StateT s Identity a
81+
````
82+
83+
For example, the following situation is one in which we would want to use `State`. When stacking monad transformers, only the "top" of the stack has its `a`, its second type parameter, provided, and the "lower" layers are used to specify its `m`, its monad.
84+
85+
```
86+
-- If `State` is defined as above, the following would fail,
87+
-- as the type synonym is only receiving an argument for `s`, the `MyState` type.
88+
newtype App1 a = App1 (ExceptT MyError (State MyState) a)
89+
newtype App2 a = App2 (WriterT MyLog (ExceptT MyError (State MyState)) a)
90+
```
91+
92+
By defining it as `type State s a`, we are saying it requires two type arguments, but in both of the above cases, we apply `MyState` to `State`, which is just one type argument. Because the example didn't include a type for `a` when using the `State` synonym, it is left partially applied,
93+
94+
Related to this, purescript/purescript#2691 is an issue which requests adding a warning for type synonyms which can be eta-reduced.

0 commit comments

Comments
 (0)