Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplication problem involving kind `Nat`

Why does addition, subtraction and division of Nats work, but not multiplication?

λ> :set -XDataKinds
λ> :set -XTypeOperators
λ> import GHC.TypeLits
λ> :k! 1 + 2
1 + 2 :: Nat
= 3
λ> :k! 1 - 2
1 - 2 :: Nat
= 1 - 2
λ> :k! 5 `Div` 2
5 `Div` 2 :: Nat
= 2
λ> :k! 1 * 2

<interactive>:1:1: error:
    • Expected kind ‘* -> Nat -> k0’, but ‘1’ has kind ‘Nat’
    • In the type ‘1 * 2’
like image 629
mhwombat Avatar asked Sep 17 '21 19:09

mhwombat


People also ask

How to learn multiplication for kids?

Read the word problems and find the product. Apply long multiplication (also known as column multiplication) method for easy calculation. Our engaging theme-based pdf worksheets help young minds understand the fundamentals of multiplication. Answer the word problems based on three fascinating themes - Winter Season, Ice rink and Library.

How to solve multiplication word problems?

The word problems featured here are based on practical applications and fact-based situations. Multiply a three or four-digit number by a single-digit multiplier to find the correct product. Sharpen your skills by solving these engaging multi-digit word problems. Apply long multiplication method to solve the problems.

What kind of problems will I be expected to solve in maths?

After going through this module, you are expected to solve routine and non routine problems involving multiplication without or with addition or subtraction of decimals and whole numbers including money using appropriate problem-solving strategies and tools.


1 Answers

The * was used to specify a simple Type. As a result 1 is seen as something that takes as first type parameter a * (so Type) and as second type parameter a 2, and thus 1 should have kind * -> Nat -> something. GHC will parse an asterisk (*) as a reference to Type if the StarIsType extension is enabled, which is the default.

If you disable it, then the asterisk (*) will refer to the multiplication, for example:

Prelude> :set -XDataKinds 
Prelude> :set -XTypeOperators
Prelude> :set -XNoStarIsType
Prelude> import GHC.TypeLits
Prelude GHC.TypeLits> :k 1 * 2
1 * 2 :: Nat
Prelude GHC.TypeLits> :kind! 1 * 2
1 * 2 :: Nat
= 2

You can also explicitly specify the module where you use the * type family family from with:

Prelude> :set -XDataKinds 
Prelude> :set -XTypeOperators
Prelude> import GHC.TypeLits
Prelude GHC.TypeLits> :k 1 GHC.TypeLits.* 2
1 GHC.TypeLits.* 2 :: Nat
Prelude GHC.TypeLits> :kind! 1 GHC.TypeLits.* 2
1 GHC.TypeLits.* 2 :: Nat
= 2
like image 88
Willem Van Onsem Avatar answered Oct 23 '22 16:10

Willem Van Onsem