caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Tomasz Zielonka <zielony@cs.net.pl>
To: CAML list <caml-list@inria.fr>
Subject: Re: [Caml-list] Narrowing coercions for functions with optional parameters
Date: Sun, 24 Feb 2002 20:50:28 +0100	[thread overview]
Message-ID: <20020224195028.GA11234@cs.net.pl> (raw)
In-Reply-To: <20020224183323.GB4757@cs.unibo.it>

On Sun, Feb 24, 2002 at 07:33:23PM +0100, Stefano Zacchiroli wrote:
> On Sun, Feb 24, 2002 at 03:01:25PM +0100, Tomasz Zielonka wrote:
> > # let coerce f x = f x
> > val coerce : ('a -> 'b) -> 'a -> 'b = <fun>
> > 
> > # coerce g ;;
> > - : int -> int = <fun>
> 
> With such a call you haven't change the original "g" function so the
> coersion of "g" to "int -> int" doesn't work because it was applied to
> the old "g" function.

Yes, I want it that way.
 
Maybe I wasn't clear enough. Let's try again.

My original question was: Is is possible to implement narrowing coercions
for functions with optional parameters. 

Such coercion would erase optional parameters not found in target type. 
It is similar to coercing one class to another, when methods not found
in the target class are hidden/"erased".

Lets say you have several functions of the same type - some request
handlers which take request as argument and return reply.

  type request and reply
  type handler = request -> reply

You could place them in a list, hash table or some other structure, from
which they are taken to handle typical requests.

Imagine that in some situation you would like to call one handler from
another passing some additional parameters. Of course the former handler
will have different type (optional labeled parameters). Now you can't
put it in the list with others (unless you wrap it in other function
with appropriate type).

Then it would be nice if you could just do (g :> handler), and get the
optional parameters erased.

When I was preparing an example, it came to me that such coercions are
sometimes performed automatically:

We have:
  val coerce : ('a -> 'b) -> 'a -> 'b = <fun>
  val g : ?add:int -> int -> int = <fun>

When 'coerce' is applied to 'g' the type of 'g' must be unified with
type of 'coerce's argument, that is:

  ?add:int -> int -> int

is unified with

  'a -> 'b

The only way to do it is to _erase_ optional argument ?add and then
substitute int for both 'a and 'b.

So here 'g' was used with type int -> int, optional argument ?add being
automatically erased. 
Here the coercion was implicit.

It DOES WORK in Ocaml 3.04

I would like that it was also possible to explicitly coerce 'g' to
type int -> int like in:

  (g : int -> int)

or rather

  (g :> int -> int)

But this DOESN'T WORK.

So now the question is:
  Why implicit coercions are performed and explicit coercions are not
  allowed?

> Try this (after executing your examples of course):
> 
>      # let g = coerce g;;
>      val g : int -> int = <fun>
>      # (g : int -> int) ;;  
>      - : int -> int = <fun>

Then the second phrase is only type assertion (or something) not
coercion. Not an interesting coercion.

> Hope this helps.
> Cheers.

Hope I was clear enough this time.
tom

-- 
   .-.   Tomasz Zielonka                           CYBER SERVICE
   oo|   programista                        http://www.cs.net.pl
  /`'\   zielony@cs.net.pl
 (\_;/)  tel: [48] (22) 723-06-79 | tel/fax: [48] (22) 723-01-75
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  reply	other threads:[~2002-02-24 19:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-02-24 14:01 Tomasz Zielonka
2002-02-24 18:33 ` Stefano Zacchiroli
2002-02-24 19:50   ` Tomasz Zielonka [this message]
2002-02-25  1:29     ` Jacques Garrigue
2002-02-25  9:44       ` Tomasz Zielonka

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20020224195028.GA11234@cs.net.pl \
    --to=zielony@cs.net.pl \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).