"ocaml_beginners"::[] "extending" a function type

View: New views
7 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] "extending" a function type

by rixed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Suppose one have defined the type :

type t = foo:int -> bar:float -> int

Now one want to define many functions of type t with the addition of
some other optional arguments :

val f1 : ?arg:int -> foo:int -> bar:float -> int
val f2 : ?arg1:float -> ?arg2:int -> foo:int -> bar:float -> int
val etc...

Is there a way to use the named type t to kind of abbreviate "foo:int -> bar:float -> int"
in these declarations ?

Firing camlp4 for that seams overkill... Any trick ?


"ocaml_beginners"::[] Re: "extending" a function type

by Vincent Aravantinos :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message




Just to be clear:

--- In ocaml_beginners@..., rixed@... wrote:

> val f1 : ?arg:int -> foo:int -> bar:float -> int
> val f2 : ?arg1:float -> ?arg2:int -> foo:int -> bar:float -> int
> val etc...

Is this really what you want? All arguments in f1 and f2 are labelled so the first optional arguments will never be optional. See 4.1.1 at http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html#toc35 .


"ocaml_beginners"::[] Re: "extending" a function type

by Hezekiah M. Carty-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

--- In ocaml_beginners@..., rixed@... wrote:

>
> Suppose one have defined the type :
>
> type t = foo:int -> bar:float -> int
>
> Now one want to define many functions of type t with the addition of
> some other optional arguments :
>
> val f1 : ?arg:int -> foo:int -> bar:float -> int
> val f2 : ?arg1:float -> ?arg2:int -> foo:int -> bar:float -> int
> val etc...
>
> Is there a way to use the named type t to kind of abbreviate "foo:int -> bar:float -> int"
> in these declarations ?
>
> Firing camlp4 for that seams overkill... Any trick ?

This should work without any tricks.  For example:

        Objective Caml version 3.11.1

# type t = int -> int;;
type t = int -> int
# module type M_t = sig val f : t end;;
module type M_t = sig val f : t end
# module M1 : M_t = struct let f x = x end;;
module M1 : M_t
# M1.f 1;;
- : int = 1
# M1.f "a string";;
Error: This expression has type string but an expression was expected of type int
# module type M_t2 = sig val f : ?foo:int -> t end;;
module type M_t2 = sig val f : ?foo:int -> t end
# module M2 : M_t2 = struct let f ?(foo = 1) x = x + x end;;
module M2 : M_t2
# module type M_t3 = sig val f : t -> t -> int -> int end;;
module type M_t3 = sig val f : t -> t -> int -> int end
# module M3 : M_t3 = struct let f g h x = g (h x) end;;
module M3 : M_t3
# M3.f M1.f M2.f 1;;
- : int = 2

Hope this helps.

Hez


Re: "ocaml_beginners"::[] "extending" a function type

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 15, 2009 at 03:32:30PM +0200, rixed@... wrote:

> Suppose one have defined the type :
>
> type t = foo:int -> bar:float -> int
>
> Now one want to define many functions of type t with the addition of
> some other optional arguments :
>
> val f1 : ?arg:int -> foo:int -> bar:float -> int
> val f2 : ?arg1:float -> ?arg2:int -> foo:int -> bar:float -> int
> val etc...

Remember in a functional language, functions are first class,
so just return one.  The types above are equivalent to:

val f1 : ?arg:int -> t
val f2 : ?arg1:float -> t

How exactly you define them depends rather a lot on what exactly you
want them to do.  Here is an plausible example that I came up with:

let print_pair i f = printf "%d, %g\n" i f    (* This is the 't' function *)

let prefix_str s = printf "PREFIX<%s> " s; print_pair   (* 'f1' *)
let prefix_float f = printf "PREFIX<%g> " f; print_pair (* 'f2' *)

The toplevel gives these the following types:

val print_pair : int -> float -> unit = <fun>
val prefix_str : string -> int -> float -> unit = <fun>
val prefix_float : float -> int -> float -> unit = <fun>

And you can use them like this:

# prefix_float 5.0 1 42.0 ;;
PREFIX<5> 1, 42

# prefix_str "hello" 4 99.0 ;;
PREFIX<hello> 4, 99

Rich.

--
Richard Jones
Red Hat

Re: "ocaml_beginners"::[] Re: "extending" a function type

by rixed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> All arguments in f1 and f2 are labelled so the first optional arguments will never be optional. See 4.1.1 at http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html#toc35 .

Wooops, I was unaware of this.
Ok, now the last ones are no more labelled.
Thank you very much, I would have a hard time figuring out what's
happening.


Re: "ocaml_beginners"::[] "extending" a function type

by rixed :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you both of you hezekiah44 and Richard !

I guess I'm still a rough C programmer in the dainty functionnal world
:-)


Re: "ocaml_beginners"::[] "extending" a function type

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 15, 2009 at 05:06:04PM +0100, Richard Jones wrote:
> On Thu, Oct 15, 2009 at 03:32:30PM +0200, rixed@... wrote:
> > val f2 : ?arg1:float -> ?arg2:int -> foo:int -> bar:float -> int

> val f2 : ?arg1:float -> t

Umm I missed out arg2 here, but the same argument applies.

Rich.

--
Richard Jones
Red Hat