Can we declare a newtype with a function?
Can we declare a newtype with a function?
newtype State s a = StateOf (s -> (s, a))
(s -> (s, a))
is a function, isn't it?
(s -> (s, a))
newtype State s a = State runState :: s -> (s, a)
such expression make sense since record syntax is allowed.
newtype State s a = State runState :: s -> (s, a)
Why wouldn't this be possible? Remember that, in Haskell, functions are also data.
– AJFarmar
Aug 22 at 22:31
2 Answers
2
(s -> (s, a))
is a function, isn't it?
(s -> (s, a))
Not sure if that answers your question, but: technically speaking no, (s -> (s, a))
is not a function, it's a function type. I.e., a type whose values are functions. Thus State
is a new type whose values are internally given as functions (but from the outside, are just “values of some opaque, named type”).
(s -> (s, a))
State
It's only opaque if the constructor isn't exported, isn't it?
– Alexey Romanov
Aug 23 at 7:33
Functions are values, too. As far as defining a type, record syntax simply provides a shortcut for
newtype State s a = StateOf (s -> (s, a))
runState :: State s a -> s -> (s, a)
runState (StateOf f) = f
(Record syntax also provides additional pattern-matching and value construction syntax.)
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The answers to both the question in the title and the one in your body are "yes". Beyond that, I'm not sure what you're asking.
– sepp2k
Aug 22 at 22:20