caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] use identity for default function
@ 2013-02-07 19:22 Ashish Agarwal
  2013-02-07 19:53 ` Jeff Meister
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Ashish Agarwal @ 2013-02-07 19:22 UTC (permalink / raw)
  To: Caml List

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

I'd like to implement

val foo : ?f:('a -> 'b) -> 'a -> 'b

where the default for f is identity. Is there now a way to do this with all
the new features of OCaml?

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

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

* Re: [Caml-list] use identity for default function
  2013-02-07 19:22 [Caml-list] use identity for default function Ashish Agarwal
@ 2013-02-07 19:53 ` Jeff Meister
  2013-02-07 20:04   ` AW: " Gerd Stolpmann
                     ` (2 more replies)
  2013-02-07 19:59 ` Martin Jambon
  2013-02-08  2:47 ` Jacques Garrigue
  2 siblings, 3 replies; 10+ messages in thread
From: Jeff Meister @ 2013-02-07 19:53 UTC (permalink / raw)
  To: Ashish Agarwal; +Cc: Caml List

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

Can't you simply define it this way? (I don't think I'm using any new
features, so maybe I misunderstood your question.)

        OCaml version 4.00.1

# external id : 'a -> 'b = "%identity";;
external id : 'a -> 'b = "%identity"
# let foo ?(f = id) x = f x;;
val foo : ?f:('a -> 'b) -> 'a -> 'b = <fun>


On Thu, Feb 7, 2013 at 11:22 AM, Ashish Agarwal <agarwal1975@gmail.com>wrote:

> I'd like to implement
>
> val foo : ?f:('a -> 'b) -> 'a -> 'b
>
> where the default for f is identity. Is there now a way to do this with
> all the new features of OCaml?
>
>

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

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

* Re: [Caml-list] use identity for default function
  2013-02-07 19:22 [Caml-list] use identity for default function Ashish Agarwal
  2013-02-07 19:53 ` Jeff Meister
@ 2013-02-07 19:59 ` Martin Jambon
  2013-02-07 20:12   ` Gabriel Kerneis
  2013-02-07 20:57   ` Ashish Agarwal
  2013-02-08  2:47 ` Jacques Garrigue
  2 siblings, 2 replies; 10+ messages in thread
From: Martin Jambon @ 2013-02-07 19:59 UTC (permalink / raw)
  To: Ashish Agarwal; +Cc: Caml List

On 02/07/2013 11:22 AM, Ashish Agarwal wrote:
> I'd like to implement
>
> val foo : ?f:('a -> 'b) -> 'a -> 'b
>
> where the default for f is identity. Is there now a way to do this with
> all the new features of OCaml?

What's wrong with the following:

let foo ~f x = ...
let fooi ?(f = fun x -> x) x = foo ~f x


Martin


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

* AW: [Caml-list] use identity for default function
  2013-02-07 19:53 ` Jeff Meister
@ 2013-02-07 20:04   ` Gerd Stolpmann
  2013-02-07 20:06   ` Gabriel Kerneis
  2013-02-09 13:13   ` Goswin von Brederlow
  2 siblings, 0 replies; 10+ messages in thread
From: Gerd Stolpmann @ 2013-02-07 20:04 UTC (permalink / raw)
  To: Jeff Meister; +Cc: Ashish Agarwal, Caml List

Nope:

#  external id : 'a -> 'b = "%identity";;
external id : 'a -> 'b = "%identity"
# (id 5);;
- : 'a = <poly>
# ((id 5) : string);;
Signal -10

# external id : 'a -> 'b = "%identity";;
external id : 'a -> 'b = "%identity"
# let foo ?(f = id) x = f x;;
val foo : ?f:('a -> 'b) -> 'a -> 'b = <fun>
# foo 5;;
- : 'a = <poly>
# ((foo 5):string);;
Signal -10

I think it is not sound what Ashish wants: If f is not passed, the type  
checker needs to add a constraint about the return value. This looks  
like a dependent type.

Gerd

Am 07.02.2013 20:53:25 schrieb(en) Jeff Meister:
> Can't you simply define it this way? (I don't think I'm using any new
> features, so maybe I misunderstood your question.)
> 
>         OCaml version 4.00.1
> 
> # external id : 'a -> 'b = "%identity";;
> external id : 'a -> 'b = "%identity"
> # let foo ?(f = id) x = f x;;
> val foo : ?f:('a -> 'b) -> 'a -> 'b = <fun>
> 
> 
> On Thu, Feb 7, 2013 at 11:22 AM, Ashish Agarwal  
> <agarwal1975@gmail.com>wrote:
> 
> > I'd like to implement
> >
> > val foo : ?f:('a -> 'b) -> 'a -> 'b
> >
> > where the default for f is identity. Is there now a way to do this  
> with
> > all the new features of OCaml?
> >
> >
> 
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs



-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
Creator of GODI and camlcity.org.
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------

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

* Re: [Caml-list] use identity for default function
  2013-02-07 19:53 ` Jeff Meister
  2013-02-07 20:04   ` AW: " Gerd Stolpmann
@ 2013-02-07 20:06   ` Gabriel Kerneis
  2013-02-09 13:13   ` Goswin von Brederlow
  2 siblings, 0 replies; 10+ messages in thread
From: Gabriel Kerneis @ 2013-02-07 20:06 UTC (permalink / raw)
  To: Jeff Meister; +Cc: Ashish Agarwal, Caml List

On Thu, Feb 07, 2013 at 11:53:25AM -0800, Jeff Meister wrote:
> Can't you simply define it this way?
> # external id : 'a -> 'b = "%identity";;
> # let foo ?(f = id) x = f x;;

If using unsafe features is okay, you might do even shorter:

# let foo ?(f = Obj.magic) x = f x ;;
val foo : ?f:('a -> 'b) -> 'a -> 'b = <fun>

But remember that “Obj.magic is not part of the language”!
For some good reason:

# let x : int list = foo 0;;
val x : int list = []

-- 
Gabriel

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

* Re: [Caml-list] use identity for default function
  2013-02-07 19:59 ` Martin Jambon
@ 2013-02-07 20:12   ` Gabriel Kerneis
  2013-02-07 20:57   ` Ashish Agarwal
  1 sibling, 0 replies; 10+ messages in thread
From: Gabriel Kerneis @ 2013-02-07 20:12 UTC (permalink / raw)
  To: Martin Jambon; +Cc: Ashish Agarwal, Caml List

On Thu, Feb 07, 2013 at 11:59:21AM -0800, Martin Jambon wrote:
> On 02/07/2013 11:22 AM, Ashish Agarwal wrote:
> >val foo : ?f:('a -> 'b) -> 'a -> 'b
> 
> What's wrong with the following:

It does not have the requested type (for fooi).

> let foo ~f x = ...
val foo : f:('a -> 'b) -> 'a -> 'b = <fun>

> let fooi ?(f = fun x -> x) x = foo ~f x
val fooi : ?f:('a -> 'a) -> 'a -> 'a = <fun>

-- 
Gabriel

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

* Re: [Caml-list] use identity for default function
  2013-02-07 19:59 ` Martin Jambon
  2013-02-07 20:12   ` Gabriel Kerneis
@ 2013-02-07 20:57   ` Ashish Agarwal
  1 sibling, 0 replies; 10+ messages in thread
From: Ashish Agarwal @ 2013-02-07 20:57 UTC (permalink / raw)
  To: Martin Jambon; +Cc: Caml List

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

On Thu, Feb 7, 2013 at 2:59 PM, Martin Jambon <martin.jambon@ens-lyon.org>wrote:


> What's wrong with the following:
>
> let foo ~f x = ...
> let fooi ?(f = fun x -> x) x = foo ~f x


I'd not even define fooi. One can easily call `foo id`, so indeed this is
not a big issue. It would just be a slight convenience to have the default,
and it's mostly out of curiosity.

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

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

* Re: [Caml-list] use identity for default function
  2013-02-07 19:22 [Caml-list] use identity for default function Ashish Agarwal
  2013-02-07 19:53 ` Jeff Meister
  2013-02-07 19:59 ` Martin Jambon
@ 2013-02-08  2:47 ` Jacques Garrigue
  2 siblings, 0 replies; 10+ messages in thread
From: Jacques Garrigue @ 2013-02-08  2:47 UTC (permalink / raw)
  To: Ashish Agarwal; +Cc: Caml List

On 2013/02/08, at 4:22, Ashish Agarwal <agarwal1975@gmail.com> wrote:

> I'd like to implement
> 
> val foo : ?f:('a -> 'b) -> 'a -> 'b
> 
> where the default for f is identity. Is there now a way to do this with all the new features of OCaml?

This is an old request (about as old as optional arguments).
The problem is clear enough: you want a different type when no argument is passed.
Interestingly, it would be possible to do that using GADTs:

type (_,_) optional =
  | Override : 'a -> ('a,'b) optional
  | Default : ('a,'a) optional

let foo : type a b. (a -> b, a -> a) optional -> a -> b = function
  | Override f -> f
  | Default -> fun x -> x

Unfortunately, GADTs were not there when optional arguments were introduced.
Also, there is the problem that the type gets more complicated.
(But there could be some sugar to print identical types only once)

Jacques Garrigue

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

* Re: [Caml-list] use identity for default function
  2013-02-07 19:53 ` Jeff Meister
  2013-02-07 20:04   ` AW: " Gerd Stolpmann
  2013-02-07 20:06   ` Gabriel Kerneis
@ 2013-02-09 13:13   ` Goswin von Brederlow
  2013-02-09 20:10     ` Jeff Meister
  2 siblings, 1 reply; 10+ messages in thread
From: Goswin von Brederlow @ 2013-02-09 13:13 UTC (permalink / raw)
  To: caml-list

On Thu, Feb 07, 2013 at 11:53:25AM -0800, Jeff Meister wrote:
> Can't you simply define it this way? (I don't think I'm using any new
> features, so maybe I misunderstood your question.)
> 
>         OCaml version 4.00.1
> 
> # external id : 'a -> 'b = "%identity";;
> external id : 'a -> 'b = "%identity"

That is wrong. %identity is of type 'a -> 'a. You are lying to the
type system and all matter of errors and segfaults will ensure (see
later mails).

MfG
	Goswin

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

* Re: [Caml-list] use identity for default function
  2013-02-09 13:13   ` Goswin von Brederlow
@ 2013-02-09 20:10     ` Jeff Meister
  0 siblings, 0 replies; 10+ messages in thread
From: Jeff Meister @ 2013-02-09 20:10 UTC (permalink / raw)
  To: Goswin von Brederlow; +Cc: Caml List

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

Indeed, reading this thread now I have no idea why I thought that was a
good idea in the first place!


On Sat, Feb 9, 2013 at 5:13 AM, Goswin von Brederlow <goswin-v-b@web.de>wrote:

> On Thu, Feb 07, 2013 at 11:53:25AM -0800, Jeff Meister wrote:
> > Can't you simply define it this way? (I don't think I'm using any new
> > features, so maybe I misunderstood your question.)
> >
> >         OCaml version 4.00.1
> >
> > # external id : 'a -> 'b = "%identity";;
> > external id : 'a -> 'b = "%identity"
>
> That is wrong. %identity is of type 'a -> 'a. You are lying to the
> type system and all matter of errors and segfaults will ensure (see
> later mails).
>
> MfG
>         Goswin
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

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

end of thread, other threads:[~2013-02-09 20:10 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-07 19:22 [Caml-list] use identity for default function Ashish Agarwal
2013-02-07 19:53 ` Jeff Meister
2013-02-07 20:04   ` AW: " Gerd Stolpmann
2013-02-07 20:06   ` Gabriel Kerneis
2013-02-09 13:13   ` Goswin von Brederlow
2013-02-09 20:10     ` Jeff Meister
2013-02-07 19:59 ` Martin Jambon
2013-02-07 20:12   ` Gabriel Kerneis
2013-02-07 20:57   ` Ashish Agarwal
2013-02-08  2:47 ` Jacques Garrigue

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).