Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] ocaml killer
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: William Lovas <wlovas@s...>
Subject: Re: fancy types (was Re: [Caml-list] ocaml killer)
On Thu, Jan 29, 2004 at 06:41:47PM +0100, Andreas Rossberg wrote:
> Also note that you can encode most of these examples without -rectypes, 
> using variant types.

In case it's nonobvious, have a gander at this:

    # type ('a, 'b) specialist = S of (('a, 'b) specialist -> 'a -> 'b);;
    type ('a, 'b) specialist = S of (('a, 'b) specialist -> 'a -> 'b)
    # let fac n =
        let do_rec (S specialist) n =
          if n = 0 then
            1
          else
            n * specialist (S specialist) n
        in
        do_rec (S do_rec) n;;
    val fac : int -> int = <fun>

It's also instructive to look at the new, improved type of do_rec:

    val do_rec : (int, int) specialist -> int -> int = <fun>

In my experience, type errors have never indicated a lack of flexibility in
the typechecker, but rather a lack of understanding on my own part of the
code i'm trying to write or the problem i'm trying to solve.  And it nearly
always pays off in the end to pause for a moment and attempt to assign
appropriate types to my functions -- then, when i'm finished, i actually
*understand* why the code should work.

This is probably why it so often seems to be the case that in typeful
programming, "once it typechecks, it works!"

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