Using * as a primitive on Nat
Asked Answered
E

1

7

I'm currently going through Sandy Maguire's Thinking with Types, and chapter 2 covers Terms, Types and Kinds. In it, there's an example of a simple interaction with the type-level primitives for performing arithmetic on Nats.

The following session is shown to work in the book, but fails on my machine:

Prelude> import GHC.TypeLits
Prelude GHC.TypeLits> :set -XDataKinds
Prelude GHC.TypeLits> :set -XTypeOperators
Prelude GHC.TypeLits> :kind! (1 + 17) * 3

<interactive>:1:1: error:
    * Expected kind `* -> Nat -> k0', but `1 + 17' has kind `Nat'
    * In the type `(1 + 17) * 3'

The next example in the book works, though:

Prelude GHC.TypeLits> :kind! (128 `Div` 8) ^ 2
(128 `Div` 8) ^ 2 :: Nat
= 256

I suspect the problem has something to do with * also indicates a kind. Sandy Maguire writes that this syntax is slated for deprecation, but if it's still around, I can see how GHCi thinks I mean the kind * instead of the type-level function *.

Am I on the right track, and if so, is there some flag I can use to make the example work?

Esotropia answered 12/5, 2020 at 6:48 Comment(0)
T
8

I suspect the problem has something to do with * also indicates a kind.

Yes, that's the problem. It can be solved by using the -XNoStarIsType extension, that lets you treat * as just another type operator.

To refer to the kind formerly known as * you can import Type form Data.Kind. For example, the kind of Maybe is Type -> Type and the kind of StateT is Type -> (Type -> Type) -> Type -> Type.

Hopefully, at some point in the future -XNoStarIsType will become the default and we'll always use Type.

Truelove answered 12/5, 2020 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.