caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Option functions (or lack thereof) + operator for composition
@ 2010-11-16 11:27 Serge Le Huitouze
  2010-11-16 11:49 ` [Caml-list] " David Allsopp
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Serge Le Huitouze @ 2010-11-16 11:27 UTC (permalink / raw)
  To: caml-list

Hi,

While writing a sample program (with lablgtk2), I found a few things annoying
and thought I would ask here what you guys think.


1. Option type
****************
It seems that there is no predefined function to test an "'a option" for being
specifically "None" or "Some _". This seems to be confirmed by the very
existence of:
http://ocaml-lib.sourceforge.net/doc/Option.html
which defines such functions ("is_none" and "is_some").
I found it weird to be forced to use "match" expressions in my code for
doing that, e.g.:
*  let curSelectedRow = ref None in
*  let updateButtonsStatus () =
*      button_remove#misc#set_sensitive
*              (match !curSelectedRow with None -> false | _ -> true)
*  in
*  ...

I could add the OCaml library mentioned above, but I don't know how to do
it (and where to find it) and, since my code is supposed to go into some other
code, I'd prefer avoiding adding yet another dependency to it...


2. Operator for composition (and its precedence)
********************************************************
To get rid of many warnings, I wrapped some calls (the "connect" calls of
my widgets) into "ignore (f x y)" statements.
I've no particular grief in using "ignore", but I find the parentheses
*really* annoying.

In Haskell, I would write "ignore $ f x y", which I find much lighter weight.

I'm not familiar with operators and their precedence, but I wonder: is it
possible to do something similar with OCaml?


Thanks for reading.

Best regards.

--Serge


^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: [Caml-list] Option functions (or lack thereof) + operator for composition
  2010-11-16 11:27 Option functions (or lack thereof) + operator for composition Serge Le Huitouze
@ 2010-11-16 11:49 ` David Allsopp
  2010-11-16 14:23   ` Michael Ekstrand
  2010-11-16 13:30 ` Jacques Garrigue
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: David Allsopp @ 2010-11-16 11:49 UTC (permalink / raw)
  To: Serge Le Huitouze, caml-list

Serge Le Huitouze wrote:
> 1. Option type
> ****************
> It seems that there is no predefined function to test an "'a option" for
> being specifically "None" or "Some _". This seems to be confirmed by the
> very existence of:
> http://ocaml-lib.sourceforge.net/doc/Option.html
> which defines such functions ("is_none" and "is_some").
> I found it weird to be forced to use "match" expressions in my code for
> doing that, e.g.:
> *  let curSelectedRow = ref None in
> *  let updateButtonsStatus () =
> *      button_remove#misc#set_sensitive
> *              (match !curSelectedRow with None -> false | _ -> true)
> *  in
> *  ...
> 
> I could add the OCaml library mentioned above, but I don't know how to do
> it (and where to find it) and, since my code is supposed to go into some
> other code, I'd prefer avoiding adding yet another dependency to it...

ExtLib is well-worth having and it's very easy to install (just run ocaml install.ml in its source tree - findlib recommended but I've got a feeling that it can install without findlib as well). It's also very stable, feature-wise, so you're not really adding a tricky dependency. It's now hosted on Google Code at http://code.google.com/p/ocaml-extlib/downloads/list

You might also take a look at the batteries project (http://batteries.forge.ocamlcore.org) which I believe includes most if not all of ExtLib.



David


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Option functions (or lack thereof) + operator for composition
  2010-11-16 11:27 Option functions (or lack thereof) + operator for composition Serge Le Huitouze
  2010-11-16 11:49 ` [Caml-list] " David Allsopp
@ 2010-11-16 13:30 ` Jacques Garrigue
  2010-11-16 13:52   ` Serge Le Huitouze
  2010-11-16 14:26 ` Michael Ekstrand
  2010-11-16 15:26 ` bluestorm
  3 siblings, 1 reply; 9+ messages in thread
From: Jacques Garrigue @ 2010-11-16 13:30 UTC (permalink / raw)
  To: Serge Le Huitouze; +Cc: caml-list

On 2010/11/16, at 20:27, Serge Le Huitouze wrote:
> 1. Option type
> ****************
> It seems that there is no predefined function to test an "'a option" for being
> specifically "None" or "Some _".

In ocaml you can compare at any type.
So you can just write
  (!curSelectedRow <> None)
which explains why there is no such function in the standard library.

> 2. Operator for composition (and its precedence)
> ********************************************************
> To get rid of many warnings, I wrapped some calls (the "connect" calls of
> my widgets) into "ignore (f x y)" statements.
> I've no particular grief in using "ignore", but I find the parentheses
> *really* annoying.
> 
> In Haskell, I would write "ignore $ f x y", which I find much lighter weight.
> 
> I'm not familiar with operators and their precedence, but I wonder: is it
> possible to do something similar with OCaml?

You can, but unfortunately $ does not have the right associativity.

# let ($) f x = f x;;
val ( $ ) : ('a -> 'b) -> 'a -> 'b = <fun>
# ignore $ 1+1;;
- : unit = ()
# succ $ succ 3;;
- : int = 5
# succ $ succ $ succ 3;;
Error: This expression has type int -> int
       but an expression was expected of type int

As you can see, it works fine for one argument, but doesn't work if you nest them.
(Actually, this is a question of point of view, since you can use it
in place of parentheses to sequence multiple arguments)

If you want the compositionality, you can use something starting with @:

# let (@@) f x = f x;;
val ( @@ ) : ('a -> 'b) -> 'a -> 'b = <fun>
# succ @@ succ @@ succ 3;;
- : int = 6

Note however that there is a simpler way to circumvent the problem:
use the "-w s" option on the command line, disabling the statement warning.
All my lablgtk code uses this flag :-)

Jacques Garrigue

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Option functions (or lack thereof) + operator for composition
  2010-11-16 13:30 ` Jacques Garrigue
@ 2010-11-16 13:52   ` Serge Le Huitouze
  2010-11-16 14:19     ` dmitry grebeniuk
  0 siblings, 1 reply; 9+ messages in thread
From: Serge Le Huitouze @ 2010-11-16 13:52 UTC (permalink / raw)
  To: caml-list, Jacques Garrigue

>> To get rid of many warnings, I wrapped some calls (the "connect" calls of
>> my widgets) into "ignore (f x y)" statements.
>
> You can, but unfortunately $ does not have the right associativity.
>
> # let ($) f x = f x;;
> val ( $ ) : ('a -> 'b) -> 'a -> 'b = <fun>
> # ignore $ 1+1;;
> - : unit = ()
> # succ $ succ 3;;
> - : int = 5
> # succ $ succ $ succ 3;;
>
> If you want the compositionality, you can use something starting with @:
>
> # let (@@) f x = f x;;
> val ( @@ ) : ('a -> 'b) -> 'a -> 'b = <fun>
> # succ @@ succ @@ succ 3;;
> - : int = 6
>

Wow, tricky!
Not as tricky as Perl naming scheme for variables, but still tricky! ;-)
So I have to learn the precedence and associaticity table in section 6.7 of
OCaml's reference manual!!!


> Note however that there is a simpler way to circumvent the problem:
> use the "-w s" option on the command line, disabling the statement warning.
> All my lablgtk code uses this flag :-)

Warnings should be gotten rid of, so I don't like the idea of making
them silent.
But restricting silence to the statement warning is probably OK.
And I'll take your words for it, since, obviously, you have more experience
with lablgtk than myself! ;-)

But, on the other hand, maybe you can harmlessly use "-w s", whereas it may
bite me, given my poor understanding of this stuff...


Thanks.

--Serge


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Option functions (or lack thereof) + operator for composition
  2010-11-16 13:52   ` Serge Le Huitouze
@ 2010-11-16 14:19     ` dmitry grebeniuk
  0 siblings, 0 replies; 9+ messages in thread
From: dmitry grebeniuk @ 2010-11-16 14:19 UTC (permalink / raw)
  To: Serge Le Huitouze

Hello.

> So I have to learn the precedence and associaticity table in section 6.7 of
> OCaml's reference manual!!!

  I usually redefine (&) operator: let ( & ) f x = f x
  It's ok until you don't use JoCaml which defines keyword "&".
  Also take a look at other infix operators I use, maybe you found them
useful too: http://bitbucket.org/gds/ops/src/tip/ops.ml (comments are
in russian and in OCaml, I'll translate russian text to english at first
request, it's not hard for me, but it seems that OCaml code in comments
describes the usage well enough).


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Option functions (or lack thereof) + operator for composition
  2010-11-16 11:49 ` [Caml-list] " David Allsopp
@ 2010-11-16 14:23   ` Michael Ekstrand
  0 siblings, 0 replies; 9+ messages in thread
From: Michael Ekstrand @ 2010-11-16 14:23 UTC (permalink / raw)
  To: caml-list

On 11/16/2010 05:49 AM, David Allsopp wrote:
> ExtLib is well-worth having and it's very easy to install (just run
> ocaml install.ml in its source tree - findlib recommended but I've
> got a feeling that it can install without findlib as well). It's also
> very stable, feature-wise, so you're not really adding a tricky
> dependency. It's now hosted on Google Code at
> http://code.google.com/p/ocaml-extlib/downloads/list
> 
> You might also take a look at the batteries project
> (http://batteries.forge.ocamlcore.org) which I believe includes most
> if not all of ExtLib.

Yes, Batteries includes pretty much all of Extlib (I'm not aware of any
missing pieces), and specifically includes the Option module.

I would recommend writing any new OCaml code with something like
Batteries (or Jane St.'s Core, if you prefer) if at all possible; it
fills in a lot of gaps in the standard library.

- Michael


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Option functions (or lack thereof) + operator for composition
  2010-11-16 11:27 Option functions (or lack thereof) + operator for composition Serge Le Huitouze
  2010-11-16 11:49 ` [Caml-list] " David Allsopp
  2010-11-16 13:30 ` Jacques Garrigue
@ 2010-11-16 14:26 ` Michael Ekstrand
  2010-11-16 15:18   ` bluestorm
  2010-11-16 15:26 ` bluestorm
  3 siblings, 1 reply; 9+ messages in thread
From: Michael Ekstrand @ 2010-11-16 14:26 UTC (permalink / raw)
  To: caml-list

On 11/16/2010 05:27 AM, Serge Le Huitouze wrote:
> 2. Operator for composition (and its precedence)
> ********************************************************
> To get rid of many warnings, I wrapped some calls (the "connect" calls of
> my widgets) into "ignore (f x y)" statements.
> I've no particular grief in using "ignore", but I find the parentheses
> *really* annoying.
> 
> In Haskell, I would write "ignore $ f x y", which I find much lighter weight.
> 
> I'm not familiar with operators and their precedence, but I wonder: is it
> possible to do something similar with OCaml?

Batteries provides operators for things like this.  It defines the '**>'
operator for function application; it's an odd name, but it has the
right associativity.  As Dmitry mentioned, some override (&).  Batteries
also provides composition operators |- and -|, and a pipeline operator
|> (opposite of **>).  With that operator, you can write:

    f x y |> ignore

thereby putting the emphasis on "f x y" and relegating "ignore" to a
cleanup at the end.

- Michael


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Option functions (or lack thereof) + operator for composition
  2010-11-16 14:26 ` Michael Ekstrand
@ 2010-11-16 15:18   ` bluestorm
  0 siblings, 0 replies; 9+ messages in thread
From: bluestorm @ 2010-11-16 15:18 UTC (permalink / raw)
  To: Michael Ekstrand; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1508 bytes --]

On Tue, Nov 16, 2010 at 3:26 PM, Michael Ekstrand <michael@elehack.net>wrote:
>
> Batteries provides operators for things like this.  It defines the '**>'
> operator for function application; it's an odd name, but it has the
> right associativity.  As Dmitry mentioned, some override (&).  Batteries
> also provides composition operators |- and -|, and a pipeline operator
> |> (opposite of **>).  With that operator, you can write:
>
>    f x y |> ignore
>
> thereby putting the emphasis on "f x y" and relegating "ignore" to a
> cleanup at the end.
>
> - Michael


(<|) as inverse of (|>) is also available. It doesn't have the "right"
associativity, but you can easily use (f -| g -| h <| x) instead of (f **> g
**> h **> x).

Though I find the application-as-pipeline style quite readable in some
cases, I think that in general it is more often superfluous than not.
Besides, as mentioned recently on this list, overuse of the function
composition operators (|-) and (-|) are also call for troubles with the
value restriction.
All in all, I think it's reasonable to stay conservative and not advertise
funky binary operators too loudly.

That said, domain-specific binary operators are certainly useful for
readability in some contexts --- that's what an infix operator is anyway :
an unreadable-by-design symbol that only get meaning by domain-specific
conventions. Local open in, available by standard since OCaml 3.12, allow us
to neatly encapsulate such domain-specific notations into OCaml modules.

[-- Attachment #2: Type: text/html, Size: 1973 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] Option functions (or lack thereof) + operator for composition
  2010-11-16 11:27 Option functions (or lack thereof) + operator for composition Serge Le Huitouze
                   ` (2 preceding siblings ...)
  2010-11-16 14:26 ` Michael Ekstrand
@ 2010-11-16 15:26 ` bluestorm
  3 siblings, 0 replies; 9+ messages in thread
From: bluestorm @ 2010-11-16 15:26 UTC (permalink / raw)
  To: Serge Le Huitouze; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1885 bytes --]

On Tue, Nov 16, 2010 at 12:27 PM, Serge Le Huitouze <
serge.lehuitouze@gmail.com> wrote:

> It seems that there is no predefined function to test an "'a option" for
> being
> specifically "None" or "Some _". This seems to be confirmed by the very
> existence of:
> http://ocaml-lib.sourceforge.net/doc/Option.html
> which defines such functions ("is_none" and "is_some").
> I found it weird to be forced to use "match" expressions in my code for
> doing that, e.g.:
> *  let curSelectedRow = ref None in
> *  let updateButtonsStatus () =
> *      button_remove#misc#set_sensitive
> *              (match !curSelectedRow with None -> false | _ -> true)
> *  in
> *  ...
>

Though useless in this case (just use ((<>) None)), there is a very nice
syntax extension proposal by Richard Jones to transform any pattern into a
boolean predicate : (matches p) would be equivalent to a function that
returns true if the input matches the pattern. I have implemented it in
camlp4 (the code may be slightly bitrotten) in case you're interested:
  http://bluestorm.info/camlp4/pa_matches.ml.html



> I'm not familiar with operators and their precedence, but I wonder: is it
> possible to do something similar with OCaml?
>

In OCaml, the associativity/precedence of an operator is defined by its
first symbols. For example (++$*) has exactly the precedence of (+). You can
find all precedence classes and their prefixes in the OCaml Manual:
  http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#@manual.kwd33
<http://caml.inria.fr/pub/docs/manual-ocaml/expr.html#@manual.kwd33>Though
this is less flexible that other languages that let you choose precedence
and associativity on a case per case basis, it gives a nice homogeneity to
binary operators: you don't need to look at the operator definition site to
have a (vague, unless you know the table by hearth) idea of its syntactic
properties.

[-- Attachment #2: Type: text/html, Size: 2708 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-11-16 15:27 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-11-16 11:27 Option functions (or lack thereof) + operator for composition Serge Le Huitouze
2010-11-16 11:49 ` [Caml-list] " David Allsopp
2010-11-16 14:23   ` Michael Ekstrand
2010-11-16 13:30 ` Jacques Garrigue
2010-11-16 13:52   ` Serge Le Huitouze
2010-11-16 14:19     ` dmitry grebeniuk
2010-11-16 14:26 ` Michael Ekstrand
2010-11-16 15:18   ` bluestorm
2010-11-16 15:26 ` bluestorm

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).