do notation and type synonym
hi, first one : i've read in different places that "there is nothing magic with do" and that do { x <- m ; return x } is the *same* as m >>= \x -> return x however i've came up with : m :: Maybe Int m = return 4 check5 = do { 5 <- m ; return 0 } check5' = m >>= \5 -> return 0 the first check5 is ok and return Nothing while the second check5' will raise an "Exception: Non-exhaustive patterns" (which is what i've expected). the sentence "there is nothing magic with do" with its reason are important for me to understand monads and do notation, so maybe someone can rephrase "there is nothing magic with do" and explain the exposed behavior? second one: is it possible to write something like type Point = (Float, Float) type Vector = (Float, Float) and still have type safety (i.e. not be able to mix Point and Vector) (and still have (*,*) without constructor)? thanks, minh thu
On 8/7/05, mt <mtvo@info.fundp.ac.be> wrote:
hi,
first one : i've read in different places that "there is nothing magic with do" and that
do { x <- m ; return x }
is the *same* as
m >>= \x -> return x
however i've came up with :
m :: Maybe Int m = return 4
check5 = do { 5 <- m ; return 0 } check5' = m >>= \5 -> return 0
the first check5 is ok and return Nothing while the second check5' will raise an "Exception: Non-exhaustive patterns" (which is what i've expected).
the sentence "there is nothing magic with do" with its reason are important for me to understand monads and do notation, so maybe someone can rephrase "there is nothing magic with do" and explain the exposed behavior?
Read http://www.haskell.org/onlinereport/exps.html, section 3.14.
second one: is it possible to write something like type Point = (Float, Float) type Vector = (Float, Float) and still have type safety (i.e. not be able to mix Point and Vector) (and still have (*,*) without constructor)?
No, that's not possible. -- Friendly, Lemmih
Am Sonntag, 7. August 2005 20:40 schrieb Lemmih:
[...]
second one: is it possible to write something like type Point = (Float, Float) type Vector = (Float, Float) and still have type safety (i.e. not be able to mix Point and Vector) (and still have (*,*) without constructor)?
No, that's not possible.
Note that while newtype declared types use some kind of data constructor, internally the newtype declared type is normally represented the same way as the "encapsulated" type. So newtype normally doesn't introduce any overhead concerning execution time and space but, of course, it introduces overhead concerning typing. :-) Best regards, Wolfgang
On Sun, Aug 07, 2005 at 08:26:07PM +0200, mt wrote:
the first check5 is ok and return Nothing while the second check5' will raise an "Exception: Non-exhaustive patterns" (which is what i've expected).
the sentence "there is nothing magic with do" with its reason are important for me to understand monads and do notation, so maybe someone can rephrase "there is nothing magic with do" and explain the exposed behavior?
The actual translation is a bit more complex to deal with (surprise!) pattern-match failures, like in your code. See the third equation in the "Translation" border on this page: http://www.haskell.org/onlinereport/exps.html#sect3.14 Still no magic.
second one: is it possible to write something like type Point = (Float, Float) type Vector = (Float, Float) and still have type safety (i.e. not be able to mix Point and Vector) (and still have (*,*) without constructor)?
I think it's not possible in current Haskell. If tuples were somehow overloaded, as numbers are... Best regards Tomasz
Hello mt, Sunday, August 07, 2005, 10:26:07 PM, you wrote: m> check5 = do { 5 <- m ; return 0 } i think the actual translation is: m >>= (\var -> case var of 5 -> return 0 _ -> fail) where fail = Nothing for Maybe monad m> second one: m> is it possible to write something like m> type Point = (Float, Float) m> type Vector = (Float, Float) m> and still have type safety (i.e. not be able to mix Point and Vector) (and m> still have (*,*) without constructor)? constructor gives you type-safety! without constructor there is no difference between Point and (Float, Float), only constructor give you syntax difference between these two types! newtype Point = Point (Float, Float) unPoint (Point p) = p -- Best regards, Bulat mailto:bulatz@HotPOP.com
Am Montag, 8. August 2005 11:30 schrieb Bulat Ziganshin:
[...]
constructor gives you type-safety! without constructor there is no difference between Point and (Float, Float), only constructor give you syntax difference between these two types!
newtype Point = Point (Float, Float) unPoint (Point p) = p
Or, more simply: newtype Point = Point {unPoint :: (Float,Float) } Best regards, Wolfgang
participants (5)
-
Bulat Ziganshin -
Lemmih -
mt -
Tomasz Zielonka -
Wolfgang Jeltsch