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)





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





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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế