Difference between revisions of "User:Benmachine"

From HaskellWiki
Jump to: navigation, search
(newtype draft)
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
I found a [[User:benmachine/hasktag_bug|bug with the <hask> tag]]. I put it on its own page so it doesn't ruin my user page.
== Contributions ==
I wrote a [[User:benmachine/Cont|Cont tutorial]] of sorts.
Articles written mostly by me, in descending order of how much I like them:
I have some objections to [[User:benmachine/Overqualified modules|module overqualification]]
* [https://wiki.haskell.org/index.php?title=Non-strict_semantics&oldid=63164 Non-strict semantics]
* [https://wiki.haskell.org/index.php?title=Pure&oldid=56834 Pure]
* [https://wiki.haskell.org/index.php?title=Polymorphism&oldid=59216 Polymorphism]
* [https://wiki.haskell.org/index.php?title=Seq&oldid=59016 seq]
* [https://wiki.haskell.org/index.php?title=Impredicative_types&direction=next&oldid=55281 Impredicative types]
* [https://wiki.haskell.org/index.php?title=Newtype&oldid=36897 Newtype]
* [https://wiki.haskell.org/index.php?title=Monoid&oldid=60323 Monoid]
* [https://wiki.haskell.org/index.php?title=OCaml&oldid=55119 OCaml]
I wrote an [[User:benmachine/uninstall.sh|uninstall script]] for things cabal installs.
== Opinions ==
Draft space:
I have some objections to [[User:benmachine/Overqualified modules|module overqualification]]
= Newtype =
A '''newtype''' declaration creates a fresh type with the same representation as an existing ("underlying") type. The most common reasons they are used are:
* providing additional type safety, by making different uses of the same underlying type incompatible,
* creating abstract data types,
* permitting alternative or additional typeclass instances to be declared for the new type.
== Syntax ==
The syntax is similar to that of [[data]] declarations:
-- name of the new type
-- | name of its value constructor
-- | | underlying type
-- | | |
-- v v v
newtype Username = MkUsername String
deriving (Eq, Ord, Read, Show)
-- may also have type parameters
newtype Parser a = MkParser (String -> Maybe (String, a))
-- or record syntax
newtype StateT s m a = StateT { runStateT :: s -> m (a, s) }
Following the above declarations, we have new type constructors <hask>Username</hask>, <hask>Parser</hask>, and <hask>StateT</hask>, and new value constructors with the following types:
MkUsername :: String -> Username
MkParser :: (String -> Maybe (String, a)) -> Parser a
StateT :: (s -> m (a, s)) -> StateT s m a
runStateT :: StateT s m a -> (s -> m (a, s))
Notice that in the case of StateT, the type constructor and the value constructor have the same name: some find that this is a helpful mnemonic, while others find it confusing, and insist on something like the <tt>Mk</tt> prefix used above (both these conventions also exist for single-constructor data declarations).
By contrast to data declarations, which may have several value constructors, each with zero or more fields containing values of other types, newtypes may only have exactly one constructor, and that constructor must have exactly one field (which, as shown above, is permitted to be a record field). This ensures that the new type and the underlying type – the type of that single field of that single constructor – are in direct correspondence.
== Uses ==
So, if a newtype declaration can only create a type that's exactly the same as an existing type, why bother at all? Let's return to the three bullet points given at the start of the article:
== Drafts ==
=== Additional type safety ===
[[User:benmachine/Cont|Cont tutorial]] (too much magic)
Sometimes you use one type for many different purposes, and it's important not to get them mixed up.
[[User:benmachine/Newtype]] (not finished)

Latest revision as of 14:45, 15 December 2019


Articles written mostly by me, in descending order of how much I like them:


I have some objections to module overqualification


Cont tutorial (too much magic)

User:benmachine/Newtype (not finished)