Home > Net >  Rename datatype "No instance … arising from the literal"
Rename datatype "No instance … arising from the literal"

Time:01-03

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?

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;

CodePudding user response:

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.

  • Related