Why have a wrapper constructor when storing a function in a data type?

I can never do too many Haskell tutorials. There is always something to refine and learn. So, I am working through chapter 10 of Real World Haskell, and everything makes sense.

but

Why do

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>newtype Parse a = Parse { runParse :: ParseState -> Either String (a, ParseState) }
identity :: a -> Parse a
identity a = Parse (s -> Right (a, s))
getState :: Parse ParseState
getState = Parse (s -> Right (s, s))
putState :: Parse ()
putState = Parse (s -> Right ((), s))
</code>
<code>newtype Parse a = Parse { runParse :: ParseState -> Either String (a, ParseState) } identity :: a -> Parse a identity a = Parse (s -> Right (a, s)) getState :: Parse ParseState getState = Parse (s -> Right (s, s)) putState :: Parse () putState = Parse (s -> Right ((), s)) </code>
newtype Parse a = Parse { runParse :: ParseState -> Either String (a, ParseState) }

identity :: a -> Parse a
identity a = Parse (s -> Right (a, s))

getState :: Parse ParseState
getState = Parse (s -> Right (s, s))

putState :: Parse ()
putState = Parse (s -> Right ((), s))

Why not simply do

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type Parse a = ParseState -> Either String (a, ParseState)
identity :: a -> Parse a
identity a = s -> Right (a, s)
getState :: Parse ParseState
getState = s -> Right (s, s)
putState :: Parse ()
putState = s -> Right ((), s)
</code>
<code>type Parse a = ParseState -> Either String (a, ParseState) identity :: a -> Parse a identity a = s -> Right (a, s) getState :: Parse ParseState getState = s -> Right (s, s) putState :: Parse () putState = s -> Right ((), s) </code>
type Parse a = ParseState -> Either String (a, ParseState)

identity :: a -> Parse a
identity a = s -> Right (a, s)

getState :: Parse ParseState
getState = s -> Right (s, s)

putState :: Parse ()
putState = s -> Right ((), s)

In other words, what is the purpose of the wrap and unwrap pattern in here (which I see used in a lot of different places in Haskell)? We get the same Parse Whatever data type, but without the extra indirection.

The answer is that Haskell doesn’t have arbitrary type lambdas, so when we want to eventually make our Parse type an instance of Monad, a newtype is required.

3

It looks like using the notation you’re taking issue with is a point of contention. From the comments on that Real World Haskell page:

I’m trying to go through the monad chapter now (chapter 14), and it
looks more and more like the parse example in this chapter is somewhat
contrived, in that some concepts it presents don’t make sense and lead
to what looks like needlessly hard-to-understand and complicated code…

and

While newtype and record syntax are explained in chapter 6, here (and
for the rest of the book) they’re not used for “records”, just for
what looks like a poor man’s substitute for encapsulation. I can’t
really see why. Isn’t the module system provided by Haskell enough for
this purpose?

In other words, the answer may well be “don’t”.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật