Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 5 :: Sum Integer work as literal?

Why does 5 :: Sum Integer work in the interpreter but given a

newtype NovoTipo a = NovoTipo a

6 :: NovoTipo Integer does not work?

like image 736
Pedro Rolo Avatar asked Dec 15 '16 12:12

Pedro Rolo


People also ask

What is a literal data type?

A literal (or literal data) is data that appears directly in the source code, like the number 5, the character A, and the text “Hello, World.” A value is an immutable, typed storage unit. A value can be assigned data when it is defined, but can never be reassigned. A variable is a mutable, typed storage unit.

What is a literal value?

Literal values (constants) are exact values (alphabetic or numeric). These values are also the constants that you use in expressions, such as the numeric value 100, or the string "John Smith". You must enclose only string literals within quotes.

What is a literal variable?

A literal is notation for representing a fixed ( const ) value. A variable is storage location associated with a symbolic name (pointed to, if you'd like).

What is meant by literal in Python?

Literals in Python is defined as the raw data assigned to variables or constants while programming. We mainly have five types of literals which includes string literals, numeric literals, boolean literals, literal collections and a special literal None.


1 Answers

You can write numeric literals for any type which has a Num instance. Sum has it, while your newtype doesn't.

You can enable GeneralizedNewtypeDeriving in ghci the following way:

:set -XGeneralizedNewtypeDeriving

Then you can write:

newtype NovoTipo a = NovoTipo a deriving (Num)

And then 6 :: NovoTipo Integer is well-typed.

like image 54
András Kovács Avatar answered Oct 12 '22 17:10

András Kovács