infix and bind pseudonym

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

infix and bind pseudonym

Michael Easter
Folks,

I have a simple type called Beverage and an example that allows me to
construct Maybe Beverage types.

Here are some type signatures for example functions:

request :: String -> Maybe Beverage

addMalt :: Beverage -> Maybe Beverage

I have defined a chain function like so:

chain :: (Maybe a) -> (a -> Maybe b) -> (Maybe b)
chain = (>>=)

I can do this:

(chain (request "beer") addMalt)

and

request "beer" `chain` addMalt

I think I understand why, as I use the back-ticks for infix.

However, I don't have to do that for the true bind function, (>>=)

request "beer" >>= addMalt

I would like to use chain in this way -- that is without back-ticks.  I'm
not sure how...

Is there something I'm missing?

thanks
Mike

ps. Thanks to everyone for the great discussion on IO (re: previous
question)

--
----------------------
Michael Easter
http://codetojoy.blogspot.com: Putting the thrill back in blog

http://youtube.com/ocitv -> Fun people doing serious software engineering
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090304/9f5050a2/attachment.htm
Reply | Threaded
Open this post in threaded view
|

infix and bind pseudonym

Magnus Therning
On Wed, Mar 4, 2009 at 12:25 PM, Michael Easter <[hidden email]> wrote:

>
> Folks,
>
> I have a simple type called Beverage and an example that allows me to
> construct Maybe Beverage types.
>
> Here are some type signatures for example functions:
>
> request :: String -> Maybe Beverage
>
> addMalt :: Beverage -> Maybe Beverage
>
> I have defined a chain function like so:
>
> chain :: (Maybe a) -> (a -> Maybe b) -> (Maybe b)
> chain = (>>=)
>
> I can do this:
>
> (chain (request "beer") addMalt)
>
> and
>
> request "beer" `chain` addMalt
>
> I think I understand why, as I use the back-ticks for infix.
>
> However, I don't have to do that for the true bind function, (>>=)
>
> request "beer" >>= addMalt
>
> I would like to use chain in this way -- that is without back-ticks.? I'm
> not sure how...
>
> Is there something I'm missing?

Yes, there are certain function names that allow infix usage without
the back-ticks, the name 'chain' doesn't.  What those function names
are?  Roughly you can say that functions that they are functions that
look like binary operations, like + - ++ >>> etc.  I'm not sure I read
the pangauage spec correctly, but it looks like operators are made up
of the following characters !@#$%^&*+-./\|<=>?~ (IIRC ':' has a
special meaning in that it's allowed in "constructors", cf 1:2:[]).

/M

--
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus?therning?org          Jabber: magnus?therning?org
http://therning.org/magnus         identi.ca|twitter: magthe
Reply | Threaded
Open this post in threaded view
|

infix and bind pseudonym

Daniel Fischer-4
In reply to this post by Michael Easter
Am Mittwoch, 4. M?rz 2009 13:25 schrieb Michael Easter:

> Folks,
>
> I have a simple type called Beverage and an example that allows me to
> construct Maybe Beverage types.
>
> Here are some type signatures for example functions:
>
> request :: String -> Maybe Beverage
>
> addMalt :: Beverage -> Maybe Beverage
>
> I have defined a chain function like so:
>
> chain :: (Maybe a) -> (a -> Maybe b) -> (Maybe b)
> chain = (>>=)
>
> I can do this:
>
> (chain (request "beer") addMalt)
>
> and
>
> request "beer" `chain` addMalt
>
> I think I understand why, as I use the back-ticks for infix.
>
> However, I don't have to do that for the true bind function, (>>=)
>
> request "beer" >>= addMalt
>
> I would like to use chain in this way -- that is without back-ticks.  I'm
> not sure how...

You can't.
Haskell has (infix) operators, whose names are composed of symbols (>, <, |,
:, +, ...) and (prefix) functions, whose names are composed of letters,
underscores (_) and primes (').
If you want to use a function infix or an operator prefix, you must indicate
that to the compiler, which is done by enclosing a function name in backticks
or an operator symbol in parentheses.

If you could use a function name infix or prefix without indicating which you
want, what would

id const even

be? Should it be id `const` even or (id const) even?

>
> Is there something I'm missing?
>
> thanks
> Mike
>
> ps. Thanks to everyone for the great discussion on IO (re: previous
> question)

Reply | Threaded
Open this post in threaded view
|

infix and bind pseudonym

Daniel Fischer-4
In reply to this post by Magnus Therning
Am Mittwoch, 4. M?rz 2009 13:59 schrieb Magnus Therning:
> Yes, there are certain function names that allow infix usage without
> the back-ticks, the name 'chain' doesn't.  What those function names
> are?  Roughly you can say that functions that they are functions that
> look like binary operations, like + - ++ >>> etc.  I'm not sure I read
> the pangauage spec correctly, but it looks like operators are made up
> of the following characters !@#$%^&*+-./\|<=>?~ (IIRC ':' has a
> special meaning in that it's allowed in "constructors", cf 1:2:[]).

':' is the symbol-equivalent of an upper case letter, so it's special only if
it's the first symbol of an operator name, then the operator is a
constructor. It can appear in any place but the first in ordinary operators.
For example:
        (:) :: a -> [a] -> [a] -- first symbol is ':' => constructor
        (:+) :: (RealFloat a) => a -> a -> Complex a -- constructor
        (/:/) :: a -> b -> b -- ':' not first symbol => ordinary operator

>
> /M

Reply | Threaded
Open this post in threaded view
|

infix and bind pseudonym

Magnus Therning
On Wed, Mar 4, 2009 at 1:20 PM, Daniel Fischer <[hidden email]> wrote:

> Am Mittwoch, 4. M?rz 2009 13:59 schrieb Magnus Therning:
>> Yes, there are certain function names that allow infix usage without
>> the back-ticks, the name 'chain' doesn't. ?What those function names
>> are? ?Roughly you can say that functions that they are functions that
>> look like binary operations, like + - ++ >>> etc. ?I'm not sure I read
>> the pangauage spec correctly, but it looks like operators are made up
>> of the following characters !@#$%^&*+-./\|<=>?~ (IIRC ':' has a
>> special meaning in that it's allowed in "constructors", cf 1:2:[]).
>
> ':' is the symbol-equivalent of an upper case letter, so it's special only if
> it's the first symbol of an operator name, then the operator is a
> constructor. It can appear in any place but the first in ordinary operators.
> For example:
> ? ? ? ?(:) :: a -> [a] -> [a] ?-- first symbol is ':' => constructor
> ? ? ? ?(:+) :: (RealFloat a) => a -> a -> Complex a ? ?-- constructor
> ? ? ? ?(/:/) :: a -> b -> b ? ?-- ':' not first symbol => ordinary operator

Yes, that's what I meant, but you put it more nicely :-)

Is : really allowed in the middle of an operator like that?  (I can't
find it at all in the description of var-symbol on the last page in
http://www.cs.uu.nl/wiki/pub/FP/CourseLiterature/haskellsyntax-main.pdf
, hence my question.)

/M

--
Magnus Therning                        (OpenPGP: 0xAB4DFBA4)
magnus?therning?org          Jabber: magnus?therning?org
http://therning.org/magnus         identi.ca|twitter: magthe
Reply | Threaded
Open this post in threaded view
|

infix and bind pseudonym

Daniel Fischer-4
Am Mittwoch, 4. M?rz 2009 15:19 schrieb Magnus Therning:
> On Wed, Mar 4, 2009 at 1:20 PM, Daniel Fischer <[hidden email]>
wrote:

> > Am Mittwoch, 4. M?rz 2009 13:59 schrieb Magnus Therning:
> >> Yes, there are certain function names that allow infix usage without
> >> the back-ticks, the name 'chain' doesn't.  What those function names
> >> are?  Roughly you can say that functions that they are functions that
> >> look like binary operations, like + - ++ >>> etc.  I'm not sure I read
> >> the pangauage spec correctly, but it looks like operators are made up
> >> of the following characters !@#$%^&*+-./\|<=>?~ (IIRC ':' has a
> >> special meaning in that it's allowed in "constructors", cf 1:2:[]).
> >
> > ':' is the symbol-equivalent of an upper case letter, so it's special
> > only if it's the first symbol of an operator name, then the operator is a
> > constructor. It can appear in any place but the first in ordinary
> > operators. For example:
> >        (:) :: a -> [a] -> [a]  -- first symbol is ':' => constructor
> >        (:+) :: (RealFloat a) => a -> a -> Complex a    -- constructor
> >        (/:/) :: a -> b -> b    -- ':' not first symbol => ordinary
> > operator
>
> Yes, that's what I meant, but you put it more nicely :-)
>
> Is : really allowed in the middle of an operator like that?  (I can't
> find it at all in the description of var-symbol on the last page in
> http://www.cs.uu.nl/wiki/pub/FP/CourseLiterature/haskellsyntax-main.pdf
> , hence my question.)

When in doubt, consult the report,
http://haskell.org/onlinereport/lexemes.html

Section 2.4:
varsym
->
( symbol {symbol | :})<reservedop | dashes>


consym
->
(: {symbol | :})<reservedop>


reservedop
->
.. | : | :: | = | \ | | | <- | -> | @ | ~ | =>

>
> /M

Cheers,
Daniel