caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* RE: [Caml-list] Polymorphism and the "for" loop
@ 2004-10-22 14:07 Harrison, John R
  2004-10-22 14:45 ` John Carr
  2004-10-22 14:47 ` Andreas Rossberg
  0 siblings, 2 replies; 25+ messages in thread
From: Harrison, John R @ 2004-10-22 14:07 UTC (permalink / raw)
  To: caml-list

| (Incidentally, O'Caml doesn't really need a type "void" since it can
convey
| a similar semantic notion using polymorphism.  Consider the function
raise,
| which has type "exn -> 'a" -- in other words, given an exception, it
| produces an arbitrary value of any type.  Since such a thing is
impossible
| (using the type-safe core language), we must conclude that raise does
not
| return to its call site when it executes.)

That's true. In any case I assume (given that a recursive type is some
kind of least fixed point) that you can define an empty type in OCaml
like
this:

  # type void = Constr of void;; 
  type void = Constr of void

So the only functions into such a type are those that never terminate
and return a value, e.g.

  # let rec f x = (f:int->void) x;;
  val f : int -> void = <fun>

or

  # let (g:int->void) = fun x -> failwith "void function";;
  val g : int -> void = <fun>

or

  # let (h:int->void) = fun x -> if x = 0 then f x else g x;;
  val h : int -> void = <fun>

Perhaps the name "void" in C is a bit poorly chosen. It's probably meant
to say that an element of the type imparts no information, rather than
imply a semantic model based on an empty type.

John.

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22 14:07 [Caml-list] Polymorphism and the "for" loop Harrison, John R
@ 2004-10-22 14:45 ` John Carr
  2004-10-23 20:58   ` Christophe Raffalli
  2004-10-22 14:47 ` Andreas Rossberg
  1 sibling, 1 reply; 25+ messages in thread
From: John Carr @ 2004-10-22 14:45 UTC (permalink / raw)
  To: caml-list


>   # type void = Constr of void;;
>   type void = Constr of void

Given this definition a void value can be constructed as
let rec x = Constr x;;
or a pair of void values as
let rec x = Constr y and y = Constr x;;

In response to another message, ocamlopt does not eliminate unit
arguments to functions though it does avoid construction of unused
unit values within functions[*].  In the machine code for
let g (f : unit -> int) = f ()
the machine value 1 (representing ()) is passed to function f.

*See asmcomp/cmmgen.ml function remove_unit.

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22 14:07 [Caml-list] Polymorphism and the "for" loop Harrison, John R
  2004-10-22 14:45 ` John Carr
@ 2004-10-22 14:47 ` Andreas Rossberg
  1 sibling, 0 replies; 25+ messages in thread
From: Andreas Rossberg @ 2004-10-22 14:47 UTC (permalink / raw)
  To: caml-list

Harrison, John R wrote:
> 
> That's true. In any case I assume (given that a recursive type is some
> kind of least fixed point) that you can define an empty type in OCaml
> like
> this:
> 
>   # type void = Constr of void;; 
>   type void = Constr of void

Try

   let rec x = Constr x

Cheers,

	- Andreas

-- 
Andreas Rossberg, rossberg@ps.uni-sb.de

Let's get rid of those possible thingies!  -- TB

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22 14:45 ` John Carr
@ 2004-10-23 20:58   ` Christophe Raffalli
  0 siblings, 0 replies; 25+ messages in thread
From: Christophe Raffalli @ 2004-10-23 20:58 UTC (permalink / raw)
  To: John Carr; +Cc: caml-list

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


I think the main difference with unit and a type "void" meaning 
returning nothing, is that void should not be allowed on the left side 
of an arrow.

Then, you could have two types:

unit (for function taking no argument), but unit should be illegal on 
the right side of an arrow (in type)

void (for function returning nothing), but void should be illegal on the 
left side of an arrow.

Then it would be impossible to compose a function returning void with 
another function (on the left) or a function waiting for unit with 
another function (on the right)

To impement this in the type system, you need to mark polymorphic 
variable to know if they can be instanciated with void or/and unit we 
would have

'a _a '_a '_a ... polymorphic variables may be a bit too much :-)


-- 
Christophe Raffalli
Université de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tél: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-23 11:41 ` Richard Jones
@ 2004-10-23 12:06   ` Matti Jokinen
  0 siblings, 0 replies; 25+ messages in thread
From: Matti Jokinen @ 2004-10-23 12:06 UTC (permalink / raw)
  To: caml-list

>   # let f = ref None;;
>   val f : '_a option ref = {contents = None}
>   # let store_f f' = f := Some f';;
>   val store_f : '_a -> unit = <fun>
> 
>   (I want store_f to have type (unit -> 'a) -> unit.)

You can write

# let store_f f' = f := Some (fun () -> ignore (f'()));;
val store_f : (unit -> 'a) -> unit = <fun>


- Matti Jokinen

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-21 18:50 Jon Harrop
  2004-10-21 19:11 ` David Brown
  2004-10-21 19:12 ` Ville-Pertti Keinonen
@ 2004-10-23 11:41 ` Richard Jones
  2004-10-23 12:06   ` Matti Jokinen
  2 siblings, 1 reply; 25+ messages in thread
From: Richard Jones @ 2004-10-23 11:41 UTC (permalink / raw)
  To: caml-list

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

I've got a problem which is sort of related to this discussion.  I
want to store a function which has type unit -> 'a (which is to say, I
want to store a function which could return any type) in a hash table.

Later, I want to call this function, ignoring the return type.

A diluted example, with my notes added:

  # let f = ref None;;
  val f : '_a option ref = {contents = None}
  # let store_f f' = f := Some f';;
  val store_f : '_a -> unit = <fun>

  (I want store_f to have type (unit -> 'a) -> unit.)

  # let do_f () =
      match !f with
      | None -> print_endline "no function"
      | Some f -> ignore (f ());;
  val do_f : unit -> unit = <fun>
  # store_f;;
  - : (unit -> '_a) -> unit = <fun>

  (Almost ...)

  # store_f (fun () -> print_endline "this is the stored function");;
  - : unit = ()
  # do_f ();;
  this is the stored function
  - : unit = ()
  # store_f;;
  - : (unit -> unit) -> unit = <fun>

I also tried replacing the definition of store_f with:

  # let store_f (f' : unit -> 'a) = f := Some f';;
  val store_f : (unit -> '_a) -> unit = <fun>

Is this possible?

Rich.

-- 
Richard Jones.  http://www.annexia.org/  http://www.j-london.com/
>>>   http://www.team-notepad.com/ - collaboration tools for teams   <<<
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
http://youunlimited.co.uk/ - Personal improvement courses

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  6:19       ` skaller
  2004-10-22  7:38         ` Jon Harrop
@ 2004-10-22 17:06         ` Brian Hurt
  1 sibling, 0 replies; 25+ messages in thread
From: Brian Hurt @ 2004-10-22 17:06 UTC (permalink / raw)
  To: skaller; +Cc: Robert M. Solovay, David Brown, Jon Harrop, caml-list

On 22 Oct 2004, skaller wrote:

> On Fri, 2004-10-22 at 12:22, Robert M. Solovay wrote:
> > How does one define a function f of type int -> void? I looked in the
> > OCaml manual and couldn't find any reference to "void". {It is, of course,
> > a familiar concept in C.}
> 
> That's right. Ocaml has no void type. I consider that a bug.

unit is Ocaml's void.  There are some subtle differences, but generally 
anything you use void for in C, you use unit for in Ocaml.

As a side note, the correct return type for functions that don't return is 
'a.  This allows code like:

let do_something = function
	| h :: _ -> h
	| [] -> invalid_arg "do_something failed!"
;;

Note that the invalid_arg function has a type string -> 'a.  

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

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


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

* RE: [Caml-list] Polymorphism and the "for" loop
@ 2004-10-22 16:00 Harrison, John R
  0 siblings, 0 replies; 25+ messages in thread
From: Harrison, John R @ 2004-10-22 16:00 UTC (permalink / raw)
  To: caml-list

|
|    let rec x = Constr x
|

I stand corrected; I'd forgotten about the cyclic
structures in OCaml. It's one of those features,
like mutable strings, that my mind tries its best
to blot out and replace by a more acceptable
reality.

John.

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22 14:02           ` skaller
@ 2004-10-22 14:31             ` David Brown
  0 siblings, 0 replies; 25+ messages in thread
From: David Brown @ 2004-10-22 14:31 UTC (permalink / raw)
  To: skaller; +Cc: Jon Harrop, caml-list

On Sat, Oct 23, 2004 at 12:02:26AM +1000, skaller wrote:

> > type 'a 'b tree = Leaf of 'a | Node of 'b * 'a 'b tree * 'a 'b tree
> > 
> > Does a "unit unit tree" take up less space than a "int int tree"?
> 
> No, this isn't possible usually, because any polymorphic
> algorithm using this data structure expects a fixed
> layout in memory.
> 
> So in this case, a dummy value must exist.
> 
> This problem can't arise in Felix or C++, because
> they both use extensional polymorphism
> (compile time instantiation).
> 
> The only way this might work in Ocaml is if 
> the pointer to value was replaced by NULL,
> but that would require a NULL check, and slow
> down the algorithm for all types just to save
> some memory in degenerate cases.

The value of type 'unit' is kind of like a NULL.  It isn't a pointer, it is
an integer value.  A leaf of an (int, int) tree for the value (Leaf 42)
would look like (where each line is a word):

  +------+
  |header|
  +------+
  |85    |
  +------+

For an (unit, unit) tree, the value for (Leaf ()) would look like:

  +------+
  |header|
  +------+
  |1     |
  +------+

For a ((int, int), (int, int)) tree, the value will be a pointer to the
pair (Leaf (4, 5)) (which must be boxed):

          +------+
          |header|
          +------+
          |ptr   |---+
          +------+   |
                     |
    +----------------+
    |
    +-->  +------+
          |header|
          +------+
          |9     |
          +------+
          |11    |
          +------+

So, storing the unit type is the same efficiency as storing an int type,
both of which take less heap space than storing most other types.

Dave

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  7:38         ` Jon Harrop
  2004-10-22  8:06           ` David Brown
  2004-10-22  8:13           ` William Lovas
@ 2004-10-22 14:02           ` skaller
  2004-10-22 14:31             ` David Brown
  2 siblings, 1 reply; 25+ messages in thread
From: skaller @ 2004-10-22 14:02 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Fri, 2004-10-22 at 17:38, Jon Harrop wrote:

>  Surely a "procedure" 
> returns no information, which can be achieved by returning the only value 
> from a type representing a singleton set, i.e. _the_ value of the type unit.

Clearly is can, since that what Ocaml does. But as I pointed
out it fails to prevent certain abuses. 

> >  type void = []
> 
> Why not "`void"?

Because `void is a value so if you write:

type void = [`void]

all you've done is define a non-canonical unit,
which is categorically *not* a void :)

> I've been wondering about this recently: how do the compilers store types 
> which contain "unit". 

If the compiler is smart it just optimises it away.
Felix usually optimises it away. In Ocaml, it can't
always be optimised away:

> For example, if we have a tree:
> 
> type 'a 'b tree = Leaf of 'a | Node of 'b * 'a 'b tree * 'a 'b tree
> 
> Does a "unit unit tree" take up less space than a "int int tree"?

No, this isn't possible usually, because any polymorphic
algorithm using this data structure expects a fixed
layout in memory.

So in this case, a dummy value must exist.

This problem can't arise in Felix or C++, because
they both use extensional polymorphism
(compile time instantiation).

The only way this might work in Ocaml is if 
the pointer to value was replaced by NULL,
but that would require a NULL check, and slow
down the algorithm for all types just to save
some memory in degenerate cases.

> The reason I'm asking is that it might be nice to generalise data structures 
> as much as possible and then specialise them using "unit" arguments.

If you use a functor, the compiler might be able to 
optimise it away.

I think you can assume most uses like this:

let f () = ...

	f () 

actually don't pass the () around: that's such an obvious
thing to optimise. Still, there must be a version
of f that isn't optimised, otherwise you couldn't
pass it to a higher order function.


-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  8:06           ` David Brown
@ 2004-10-22  9:39             ` Andreas Rossberg
  0 siblings, 0 replies; 25+ messages in thread
From: Andreas Rossberg @ 2004-10-22  9:39 UTC (permalink / raw)
  To: caml-list

On Fri, Oct 22, 2004 at 08:38:49AM +0100, Jon Harrop wrote:
>I've been wondering about this recently: how do the compilers store types 
>which contain "unit". For example, if we have a tree:
>
>type 'a 'b tree = Leaf of 'a | Node of 'b * 'a 'b tree * 'a 'b tree
>
>Does a "unit unit tree" take up less space than a "int int tree"?

No, because the representation must be the same. Otherwise polymorphic 
functions could not operate on both. (Besides, you got the syntax wrong. 
It's ('a,'b) tree.).

If you rephrase the question as

"Does

   type unit_tree = Leaf of unit | Node of unit * unit_tree * unit_tree

have a more efficient representation than

   type int_tree = Leaf of int | Node of int * int_tree * int_tree

?"

you _might_ find a different answer. But in fact you don't, the 
representation still is the same. And I believe it is right for the 
compiler not to bother optimising types like unit_tree, because they are 
not very useful.

>The reason I'm asking is that it might be nice to generalise data structures 
>as much as possible and then specialise them using "unit" arguments.

This definitely won't work.

Cheers,

	- Andreas

-- 
Andreas Rossberg, rossberg@ps.uni-sb.de

Let's get rid of those possible thingies!  -- TB

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  8:13           ` William Lovas
@ 2004-10-22  8:17             ` Jon Harrop
  0 siblings, 0 replies; 25+ messages in thread
From: Jon Harrop @ 2004-10-22  8:17 UTC (permalink / raw)
  To: caml-list

On Friday 22 October 2004 09:13, William Lovas wrote:
> On Fri, Oct 22, 2004 at 08:38:49AM +0100, Jon Harrop wrote:
> > Why not "`void"?
>
> "unit" and "void" do not denote the same type...

Sorry! I forgot the: ;-)

Cheers,
Jon.

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  7:38         ` Jon Harrop
  2004-10-22  8:06           ` David Brown
@ 2004-10-22  8:13           ` William Lovas
  2004-10-22  8:17             ` Jon Harrop
  2004-10-22 14:02           ` skaller
  2 siblings, 1 reply; 25+ messages in thread
From: William Lovas @ 2004-10-22  8:13 UTC (permalink / raw)
  To: caml-list

On Fri, Oct 22, 2004 at 08:38:49AM +0100, Jon Harrop wrote:
> On Friday 22 October 2004 07:19, skaller wrote:
> >  type void = []
> 
> Why not "`void"?

"unit" and "void" do not denote the same type -- the former is the type
containing one element, whereas the latter is the type containing *no*
elements *at all*.  To emphasize this intuition, the types "unit" and
"void" are often written as "1" and "0" in the literature.

> > ...I consider that a bug...
> 
> I'd have to go right ahead and disagree with you there. Surely a "procedure" 
> returns no information, which can be achieved by returning the only value 
> from a type representing a singleton set, i.e. _the_ value of the type unit.

Well, a "procedure" *does* return some information -- the implicit
assertion that it completed!  (And in doing so, carried out any side
effects that it might have had.)  The only way to do this in O'Caml is
to return some value.  In the case of a function executed solely for its
effects, we conventionally choose the trivial value (), but any value
would serve as well.

"void" would be another story altogether: Since there are no elements of
type "void", a function returning that type can never return at all, so
it's not really the right idea for "procedures" (or, more precisely,
functions executed solely for their effects).  I don't see the value in
hijacking the type "void" and imbuing it with a new meaning when we have an
appropriate "procedure" return type already...

(Incidentally, O'Caml doesn't really need a type "void" since it can convey
a similar semantic notion using polymorphism.  Consider the function raise,
which has type "exn -> 'a" -- in other words, given an exception, it
produces an arbitrary value of any type.  Since such a thing is impossible
(using the type-safe core language), we must conclude that raise does not
return to its call site when it executes.)

cheers,
William

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  7:38         ` Jon Harrop
@ 2004-10-22  8:06           ` David Brown
  2004-10-22  9:39             ` Andreas Rossberg
  2004-10-22  8:13           ` William Lovas
  2004-10-22 14:02           ` skaller
  2 siblings, 1 reply; 25+ messages in thread
From: David Brown @ 2004-10-22  8:06 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Fri, Oct 22, 2004 at 08:38:49AM +0100, Jon Harrop wrote:

> I've been wondering about this recently: how do the compilers store types 
> which contain "unit". For example, if we have a tree:
> 
> type 'a 'b tree = Leaf of 'a | Node of 'b * 'a 'b tree * 'a 'b tree
> 
> Does a "unit unit tree" take up less space than a "int int tree"?

Each of the types 'Leaf', or Node will always take the same amount of
space, not counting the header (which is 1 word before the block):  A leaf
will be 1 word, and a Node will be 2 words.  The value passed around is
always a word, which fits in a pointer.

So a "unit unit tree" of the same shape takes up the same space as an "int
int tree".  However, if the type doesn't fit in a word, heap space will be
needed for the block.

In ocaml, (), the only value of type unit, is always represented as the
word '1'.

The unit type follows nicely from the pattern of n-tuples (pairs, triples,
4-typles, so why not 0-tuples).  The unit value is really a 0-tuple.  Since
the type is parameterized by 0 other types, there is only one unit type.

> The reason I'm asking is that it might be nice to generalise data structures 
> as much as possible and then specialise them using "unit" arguments.

But, the original discussion was concerning procedures that don't actually
return anything.  Without side-effects, procedures aren't useful, since
they can't do anything.  Since there are side effects, though, it is
meaningful to have a procedure that doesn't return any type.  The unit type
was created for this purpose.  If we created another type, say void, to
indicate a function that returned no value (a procedure), it would mostly
serve the same purpose as the unit type.  It might be possible to avoid
having a constructor for this type, but then it gets more difficult to
explicitly ignore values.  I think you would quickly get back to the same
place we are at now.

Perhaps 'unit' is fine for functions that don't return anything, and we
should come up with something different to pass in as the needed argument
to functions that don't need an argument.  You could always create

  type noarg = Noarg

another single-constructor type, and use noarg for functions that don't
really want an argument.  This would prevent you from calling this function
with the result of something that wasn't intended to return a value.

I guess my question is, for functions that takes unit, and returns unit,
what's really wrong with:

  f (g ())

The evaluation order is defined, although not necessarily meaningful.
Perhaps later I might want to come up with a value that gets passed from g
to f.

Dave

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  6:19       ` skaller
@ 2004-10-22  7:38         ` Jon Harrop
  2004-10-22  8:06           ` David Brown
                             ` (2 more replies)
  2004-10-22 17:06         ` Brian Hurt
  1 sibling, 3 replies; 25+ messages in thread
From: Jon Harrop @ 2004-10-22  7:38 UTC (permalink / raw)
  To: caml-list

On Friday 22 October 2004 07:19, skaller wrote:
> ...I consider that a bug...

I'd have to go right ahead and disagree with you there. Surely a "procedure" 
returns no information, which can be achieved by returning the only value 
from a type representing a singleton set, i.e. _the_ value of the type unit.

>  type void = []

Why not "`void"?

I've been wondering about this recently: how do the compilers store types 
which contain "unit". For example, if we have a tree:

type 'a 'b tree = Leaf of 'a | Node of 'b * 'a 'b tree * 'a 'b tree

Does a "unit unit tree" take up less space than a "int int tree"?

The reason I'm asking is that it might be nice to generalise data structures 
as much as possible and then specialise them using "unit" arguments.

Cheers,
Jon.

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  4:17     ` William Lovas
@ 2004-10-22  6:42       ` skaller
  0 siblings, 0 replies; 25+ messages in thread
From: skaller @ 2004-10-22  6:42 UTC (permalink / raw)
  To: William Lovas; +Cc: caml-list

On Fri, 2004-10-22 at 14:17, William Lovas wrote:

> > It can also be argued chosing unit for non-returning function
> > is the wrong choice and that correct choice is void.
> 
> Certainly.  But whoever mentioned non-returning functions?

Please excuse my sloppy wording! What I meant was
functions that don't return a value (aka procedures)
rather than functions that don't return control normally.

Sorry!


-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  2:22     ` Robert M. Solovay
@ 2004-10-22  6:19       ` skaller
  2004-10-22  7:38         ` Jon Harrop
  2004-10-22 17:06         ` Brian Hurt
  0 siblings, 2 replies; 25+ messages in thread
From: skaller @ 2004-10-22  6:19 UTC (permalink / raw)
  To: Robert M. Solovay; +Cc: David Brown, Jon Harrop, caml-list

On Fri, 2004-10-22 at 12:22, Robert M. Solovay wrote:
> How does one define a function f of type int -> void? I looked in the
> OCaml manual and couldn't find any reference to "void". {It is, of course,
> a familiar concept in C.}

That's right. Ocaml has no void type. I consider that a bug.
Briefly, it was possible to define one using polymorphic variants:

	type void = []

however that feature was removed. In any case that would
not be enough, since many standard functions would still
return the wrong type.

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  1:19   ` skaller
  2004-10-22  2:22     ` Robert M. Solovay
@ 2004-10-22  4:17     ` William Lovas
  2004-10-22  6:42       ` skaller
  1 sibling, 1 reply; 25+ messages in thread
From: William Lovas @ 2004-10-22  4:17 UTC (permalink / raw)
  To: caml-list

On Fri, Oct 22, 2004 at 11:19:53AM +1000, skaller wrote:
> On Fri, 2004-10-22 at 05:11, David Brown wrote:
> > On Thu, Oct 21, 2004 at 07:50:43PM +0100, Jon Harrop wrote:
> 
> > Because its only a warning, not an error.  g is allowed to return any type.
> > It could be argued that the loop expression must be of type unit, then this
> > could also be inferred by type inference.
> 
> It can also be argued chosing unit for non-returning function
> is the wrong choice and that correct choice is void.

Certainly.  But whoever mentioned non-returning functions?

William

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-22  1:19   ` skaller
@ 2004-10-22  2:22     ` Robert M. Solovay
  2004-10-22  6:19       ` skaller
  2004-10-22  4:17     ` William Lovas
  1 sibling, 1 reply; 25+ messages in thread
From: Robert M. Solovay @ 2004-10-22  2:22 UTC (permalink / raw)
  To: skaller; +Cc: David Brown, Jon Harrop, caml-list

How does one define a function f of type int -> void? I looked in the
OCaml manual and couldn't find any reference to "void". {It is, of course,
a familiar concept in C.}

	--Bob Solovay




On 22 Oct 2004, skaller wrote:

> On Fri, 2004-10-22 at 05:11, David Brown wrote:
> > On Thu, Oct 21, 2004 at 07:50:43PM +0100, Jon Harrop wrote:
>
> > Because its only a warning, not an error.  g is allowed to return any type.
> > It could be argued that the loop expression must be of type unit, then this
> > could also be inferred by type inference.
>
> It can also be argued chosing unit for non-returning function
> is the wrong choice and that correct choice is void.
>
> In particular given
>
> f: unit -> unit
>
> you can write
>
> f ( f () )
>
> which is silly. Given
>
> f: unit -> void
>
> that expression won't type check. The result is to force
> 'commands' with side effects and no return values
> be 'top level' (or at least arguments of for, ';', etc).
>
> This seems to work well in Felix.
>
> --
> John Skaller, mailto:skaller@users.sf.net
> voice: 061-2-9660-0850,
> snail: PO BOX 401 Glebe NSW 2037 Australia
> Checkout the Felix programming language http://felix.sf.net
>
>
>
> -------------------
> 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
>

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-21 19:11 ` David Brown
@ 2004-10-22  1:19   ` skaller
  2004-10-22  2:22     ` Robert M. Solovay
  2004-10-22  4:17     ` William Lovas
  0 siblings, 2 replies; 25+ messages in thread
From: skaller @ 2004-10-22  1:19 UTC (permalink / raw)
  To: David Brown; +Cc: Jon Harrop, caml-list

On Fri, 2004-10-22 at 05:11, David Brown wrote:
> On Thu, Oct 21, 2004 at 07:50:43PM +0100, Jon Harrop wrote:

> Because its only a warning, not an error.  g is allowed to return any type.
> It could be argued that the loop expression must be of type unit, then this
> could also be inferred by type inference.

It can also be argued chosing unit for non-returning function
is the wrong choice and that correct choice is void.

In particular given

f: unit -> unit

you can write

f ( f () )

which is silly. Given

f: unit -> void

that expression won't type check. The result is to force
'commands' with side effects and no return values 
be 'top level' (or at least arguments of for, ';', etc).

This seems to work well in Felix.

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net



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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-21 19:20   ` Jon Harrop
@ 2004-10-21 19:42     ` Ville-Pertti Keinonen
  0 siblings, 0 replies; 25+ messages in thread
From: Ville-Pertti Keinonen @ 2004-10-21 19:42 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

Jon Harrop wrote:

>I see. Does this stem from historical reasons or is there a logical reason why 
>this should be a warning rather than an error?
>  
>
Good question.  Personally, I probably would've made it an error.

>Yes, whereas the current approach can "fail" silently:
>
># f (fun () -> 1);;
>- : unit = ()
>  
>
That's not a failure, just a lack of a warning, since the type system 
lacks the ability to propagate the "<warning-if-not-unit>" property that 
I suggested.

Compared to compilers for other languages, OCaml seems to me fairly easy 
to understand in its warnings/lack-thereof.  I've run into quite a bit 
of C++ code, compiled using GCC, that generates (unjustified) warnings 
about potentially uninitialized variables on some architectures 
(PA-RISC/HP-UX) but not others...

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-21 19:12 ` Ville-Pertti Keinonen
@ 2004-10-21 19:20   ` Jon Harrop
  2004-10-21 19:42     ` Ville-Pertti Keinonen
  0 siblings, 1 reply; 25+ messages in thread
From: Jon Harrop @ 2004-10-21 19:20 UTC (permalink / raw)
  To: caml-list

On Thursday 21 October 2004 20:12, Ville-Pertti Keinonen wrote:
> Consider the fact that it's a warning as opposed to an error.

I see. Does this stem from historical reasons or is there a logical reason why 
this should be a warning rather than an error?

> ...
> If your second example produced a type of unit -> unit for g, I think 
> your first example should fail with an error (rather than be accepted 
> with a warning).

Yes, whereas the current approach can "fail" silently:

# f (fun () -> 1);;
- : unit = ()

Cheers,
Jon.

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-21 18:50 Jon Harrop
  2004-10-21 19:11 ` David Brown
@ 2004-10-21 19:12 ` Ville-Pertti Keinonen
  2004-10-21 19:20   ` Jon Harrop
  2004-10-23 11:41 ` Richard Jones
  2 siblings, 1 reply; 25+ messages in thread
From: Ville-Pertti Keinonen @ 2004-10-21 19:12 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

Jon Harrop wrote:

>Considering the following code objects to the expression in the "for" loop not 
>having the type "unit":
>
># for i=0 to 3 do 1 done;;
>Warning: this expression should have type unit.
>- : unit = ()
>
>why is "g" in the following code inferred to have the polymorphic type (unit 
>-> 'a) rather than (unit -> unit)?
>
># let f g = for i=0 to 3 do g () done;;
>val f : (unit -> 'a) -> unit = <fun>
>  
>
Consider the fact that it's a warning as opposed to an error.  An 
expression with a result that isn't used (fairly rare in OCaml) is a 
special case; any type is permitted, but an explicitly non-unit type 
generates a warning, since it's probably an error.

If the type system were more advanced, your second example would infer a 
type of "unit -> 'a <warning-if-non-unit>".

If your second example produced a type of unit -> unit for g, I think 
your first example should fail with an error (rather than be accepted 
with a warning).

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


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

* Re: [Caml-list] Polymorphism and the "for" loop
  2004-10-21 18:50 Jon Harrop
@ 2004-10-21 19:11 ` David Brown
  2004-10-22  1:19   ` skaller
  2004-10-21 19:12 ` Ville-Pertti Keinonen
  2004-10-23 11:41 ` Richard Jones
  2 siblings, 1 reply; 25+ messages in thread
From: David Brown @ 2004-10-21 19:11 UTC (permalink / raw)
  To: Jon Harrop; +Cc: caml-list

On Thu, Oct 21, 2004 at 07:50:43PM +0100, Jon Harrop wrote:
> 
> Considering the following code objects to the expression in the "for" loop not 
> having the type "unit":
> 
> # for i=0 to 3 do 1 done;;
> Warning: this expression should have type unit.
> - : unit = ()
> 
> why is "g" in the following code inferred to have the polymorphic type (unit 
> -> 'a) rather than (unit -> unit)?
> 
> # let f g = for i=0 to 3 do g () done;;
> val f : (unit -> 'a) -> unit = <fun>

Because its only a warning, not an error.  g is allowed to return any type.
It could be argued that the loop expression must be of type unit, then this
could also be inferred by type inference.

Dave

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


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

* [Caml-list] Polymorphism and the "for" loop
@ 2004-10-21 18:50 Jon Harrop
  2004-10-21 19:11 ` David Brown
                   ` (2 more replies)
  0 siblings, 3 replies; 25+ messages in thread
From: Jon Harrop @ 2004-10-21 18:50 UTC (permalink / raw)
  To: caml-list


Considering the following code objects to the expression in the "for" loop not 
having the type "unit":

# for i=0 to 3 do 1 done;;
Warning: this expression should have type unit.
- : unit = ()

why is "g" in the following code inferred to have the polymorphic type (unit 
-> 'a) rather than (unit -> unit)?

# let f g = for i=0 to 3 do g () done;;
val f : (unit -> 'a) -> unit = <fun>

Cheers,
Jon.

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


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

end of thread, other threads:[~2004-10-23 20:58 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-22 14:07 [Caml-list] Polymorphism and the "for" loop Harrison, John R
2004-10-22 14:45 ` John Carr
2004-10-23 20:58   ` Christophe Raffalli
2004-10-22 14:47 ` Andreas Rossberg
  -- strict thread matches above, loose matches on Subject: below --
2004-10-22 16:00 Harrison, John R
2004-10-21 18:50 Jon Harrop
2004-10-21 19:11 ` David Brown
2004-10-22  1:19   ` skaller
2004-10-22  2:22     ` Robert M. Solovay
2004-10-22  6:19       ` skaller
2004-10-22  7:38         ` Jon Harrop
2004-10-22  8:06           ` David Brown
2004-10-22  9:39             ` Andreas Rossberg
2004-10-22  8:13           ` William Lovas
2004-10-22  8:17             ` Jon Harrop
2004-10-22 14:02           ` skaller
2004-10-22 14:31             ` David Brown
2004-10-22 17:06         ` Brian Hurt
2004-10-22  4:17     ` William Lovas
2004-10-22  6:42       ` skaller
2004-10-21 19:12 ` Ville-Pertti Keinonen
2004-10-21 19:20   ` Jon Harrop
2004-10-21 19:42     ` Ville-Pertti Keinonen
2004-10-23 11:41 ` Richard Jones
2004-10-23 12:06   ` Matti Jokinen

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