Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rename datatype "No instance … arising from the literal"

I’m new to Haskell and trying to explore how datatypes in Haskell work. For instance, I’m trying to run the following code:

data Number = Int
num :: Number
num = 1

However with that, I get the following error:

main.hs:3:7: error:
    * No instance for (Num Number) arising from the literal `1'
    * In the expression: 1
      In an equation for `num': num = 1
  |
3 | num = 1
  |   

Why am I getting that error when 1 should be an Int?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For reference, I come from a TypeScript and Rust background where you can do similar things:

// TypeScript
type Num = number // the Number type is already defined
let num: Num = 1
// Rust
type Number = usize;
let num: Number = 1;

>Solution :

You created a type Number that has one data constructor without a parameter. What you likely want to do is construct a type alias with:

type Number = Int

num :: Number
num = 1

Here both Number and Int are different aliasses for the same type.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading