Version française
Home     About     Download     Resources     Contact us    
Browse thread
Void type?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: skaller <skaller@u...>
Subject: Re: [Caml-list] Re: Void type?
On Sun, 2007-07-29 at 22:06 +0200, Arnaud Spiwack wrote:

> One reason why it might be useful, is that it gives an equivalence 
> between the types  t -> void   and  t -> 'a  (for any type t). Earlier 
> in this thread it was mentioned that these sort of functions could be a 
> reason to use a void type.

It is used like this in Felix:

  type void
  type proc = unit -> void
  exception Ok
  let squash () : void = raise Ok
  let call (f:proc) : unit = try ignore(f ()) with Ok -> ()
  let seq (fs: proc list): proc = 
	fun () -> squash (List.iter call fs)

  (* example *)

  let x = ref 0
  let f () : void = squash (incr x)
  let incr3 : proc = seq [f;f;f];;
  call incr3;;
  print_endline ("x=" ^ string_of_int !x);;

You can write combinators such as 'cond' and 'loop' as well.
The combinators are purely functional and lazy, except 'call'
which forces side-effecting.

The advantage is the type system prevents some accidents:

	let f () = incr x;;
	f (f ());; (* woops *)

by using the fact that void has no values, to prevent using
the application of a procedure as the argument of an function.

Given a purely functional library .. you could probably
design a purely functional camlp4/5 syntax, then you'd
actually get referential transparency. 

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net