Updated April 5, 2023
Definition of Haskell Newtype Function
Haskell new type is a type that is represented as the identical copy of the existing type in Haskell, this new type provides us strict type checking at the compile-time, which makes it less prone to error in Haskell. NewType in Haskell offers us correctness because it follows the type checking.
It guarantees the correctness of the code at compile-time only. It just wraps the existing type, but it applies its own semantic to the newly create newtype. In the coming section of the tutorial, we will discuss more the internal working of the new type, its implementation, and usage while programming in detail.
Syntax:
As we discussed it is identical to the existing type in Haskell, but after creation, it applies some of the semantics of it. Let’s take a look at its syntax to understand how to use this while programming, for beginners to understand with clarity see below;
newtype name = name Type
As you can see in the above lines of syntax to define a newtype, we need to use the ‘newtype’ keyword provided by Haskell. After this name of the variable and one existing type. Let’s take a practice syntax for beginners to understand the syntax for newtype in more detail see below;
e.g. :
newtype Demo = Demo String
As you can see in the above line of code we are trying to create a newtype in Haskell by using the existing type. In the coming section of the tutorial, we will discuss more the internal working of newtype in detail which will make it easier for below to understand it better with some more practical examples.
How does Newtype function work in Haskell?
As we have already known that newtype is a way to create a copy of the existing type from Haskell, but it offers some more advantages besides just making the same type using the newtype keyword in Haskell. The first thing about newtype in Haskell is that it is an in-built feature from Haskell we do not require to include any dependency or library as such to implement or use it while programming in Haskell. Also, its syntax and way of usage is very easy to understand and handle, also readable by others. Let’s take a look at its signature how we can start using it inside the program see below;
Signature:
newtype Name = Name Existing type: In this signature of using newtype in Haskell, we are using newtype keyword to define it. If you want to define a variable as newtype then at the start you have to use newtype keyword at the start of the declaration. After this it can be the name by which we want it to be defined in Haskell, followed by one of the existing types in Haskell, so what it will do, it will wrap the existing type also will apply type checking to the newtype variable. The new type is useful in various scenarios in Haskell or programming language, suppose we have some entities in our code which ahs the same representation in Haskell, so in these types of cases we cause newtype from Haskell. Also one more thing it is not a function rather it is a type or new type in Haskell with a correctness guarantee in Haskell. The above case that we have discussed is the real case that can occur when we have several or multiple entities that have the same representation. Let’s take a sample example of newtype in Haskell to understand it better and start using it see below;
e.g. :
newtype Demo = Demo { demovalue :: Float }
demofunc1 :: Demo -> Float
val1 :: Demo
val1 = Demo 20
demofucn2 :: Float
demofucn2 = demofunc1 val1
demofunc1 (Demo value2) = value2 + 10
main = do
print(demofucn2)
As you can see in the above lines of code we are trying to use a newtype in Haskell, but the user above is a little bit complex to understand here. First, we have defined the newtype by the name Demo. After this, we have defined one function which is using this Demo newtype. Now we are trying to assign value to the function. In the end, we are calling this function to get the value calculated by using the above function. This is not easy to use while programming but if you try to understand the basic concept then it will surely be easy to implement in Haskell. let’s take a look at a few points which we should keep in mind in order to use newtype in Haskell see below;
1) This is an in-built feature provided by Haskell, we do not require to include any library for this.
2) Simple to create using ‘newtype’ in Haskell.
3) Should be defined before the main method of Haskell.
4) Always remember it will always wrap up the existing type from Haskell, and apply a strict type checking rule to the variable.
Examples
1) In this example we are trying to use the newtype in Haskell, by the use of it we are trying to calculate the value from two different functions. this is just a sample example for beginners to understand the usage of newtype in Haskell.
Code:
newtype Demo = Demo { demovalue :: Float }
demofunc1 :: Demo -> Float
val1 :: Demo
val1 = Demo 20
demofucn2:: Float
demofucn2 = demofunc1 val1
demofunc1 (Demo value2) = value2 + 10
main = do
print("Demo to show usage of newtype in haskell !!")
print("calculated result by the function is ::")
print(demofucn2)
Output:
Conclusion
By the use of this newtype keyword in Haskell we can deal with various scenarios in Haskell. Also, it is not a function rather it is a keyword in Haskell. Which allows us to create the new type in Haskell with some added rules. It is also identical to the existing type of Haskell.
Recommended Articles
We hope that this EDUCBA information on “Haskell Newtype” was beneficial to you. You can view EDUCBA’s recommended articles for more information.