For new type why P(...)
is used to enclose the content?
Any newtype
declared type must have a single constructor. In this case that constructor is named P
and its type is
P :: (String -> [(a, String)]) -> Parser a
You can also use record syntax, so it would be equivalent to
newtype Parser a = P { runParser :: String -> [(a, String)] }
Or if you were to use a data
type (in which case the wrapper does not get optimized away at runtime as easily), it would be very similar:
data Parser a = P { runParser :: String -> [(a, String)] }
It requires to provide new constructor with newtype, but I don't seem to find one from the example code. How to define constructor for newtype? Is it OK not to provide one?
As mentioned above, the constructor to the newtype Parser
is named P
, and there must be exactly one constructor for a newtype
.
Some more clarification, the type
keyword constructs a simple alias. For example, String
is defined as
type String = [Char]
You can use String
in any function that takes [Char]
, and you can use [Char]
in any function that takes String
. Since FilePath
is defined as
type FilePath = String
Then you can use FilePath
where ever you would use a String
and thus anywhere you would use [Char]
, and vice versa. These are simply names for reducing more complicated types. I would say that in practice it's more common to use newtype
rather than type
except in very simple cases, simply because it allows the type system to enforce things more in your software. There are also disadvantages to type
s, since you need language extensions to use them in instance
declarations, and they must always be fully applied in other expressions. The disadvantages of newtype
is that you have to wrap/unwrap them a lot, but this can be more of a boon when working with truly complex programs.
P
is the new constructor. – Daniel Wagner