caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Thomas Fischbacher <Thomas.Fischbacher@Physik.Uni-Muenchen.DE>
To: William Lovas <wlovas@stwing.upenn.edu>
Cc: caml-list@inria.fr
Subject: Re: fancy types (was Re: [Caml-list] ocaml killer)
Date: Thu, 29 Jan 2004 18:17:31 +0100 (CET)	[thread overview]
Message-ID: <Pine.LNX.4.58.0401291736560.3416@seekar.cip.physik.uni-muenchen.de> (raw)
In-Reply-To: <20040129162048.GA29094@force.stwing.upenn.edu>


On Thu, 29 Jan 2004, William Lovas wrote:

> I've passed functions to themselves before without ever having to make use
> of such a function

Look here:

# let fac n = let do_rec specialist n = if n = 0 then 1 else n * specialist specialist (n-1) in do_rec do_rec n;;

Characters 74-84:
  let fac n = let do_rec specialist n = if n = 0 then 1 else n * 
specialist specialist (n-1) in do_rec do_rec n;;
                                                                            
^^^^^^^^^^
This expression has type 'a -> 'b -> 'c but is here used with type 'a

# let fac n = let do_rec specialist n = if n = 0 then 1 else n * (Obj.magic specialist) specialist (n-1) in do_rec do_rec n;;
val fac : int -> int = <fun>

# fac 8;;
- : int = 40320

This is just one stupid example where the type system gets in the way 
while it should not. Yes, here it does not make too much sense, as I 
would use let rec if I wanted to just create a factorial, but it shows 
the general principle: whenever I have a function that handles a "base 
case" and takes a specialist to pass the recursive case on to, which may 
in turn again require a specialist, I cannot give the specialist itself as 
further specialist, "because unification would give infinite type", as 
other functional languages with similar type system (Haskell or SML, say),
like to put it. and there are quite some cases where one would like to 
employ such techniques. (In Common LISP, I use it regularly when working 
with the "screamer" nondeterministic evaluation module: screamer's biggest 
advantage is that it is readily available, but it is conceptually broken 
at many levels. As it in particular cannot code walk a LABELS, I have to 
resort to such techniques for manual resolution of fixed points to make 
recursive nondeterministic functions...)

As one particular consequence, I also cannot easily map some combinators 
to ocaml:

# let l_combinator x y = x (y y);;

Characters 28-29:
  let l_combinator x y = x (y y);;
                              ^
This expression has type 'a -> 'b but is here used with type 'a

But there are more issues... Look here:

# type ('a,'b) mypair = Nil | Cons of 'a * 'b;;
type ('a, 'b) mypair = Nil | Cons of 'a * 'b


Try defining something like this on it:


# let rec my_len x = match x with | Nil -> 0 | (Cons (p,q)) -> 1+my_len q;;

Characters 70-71:
  let rec my_len x = match x with | Nil -> 0 | (Cons (p,q)) -> 1+my_len q;;
                                                                        ^
This expression has type 'a but is here used with type ('b, 'a) mypair


OF COURSE opinions may differ whether this is really a problem or not. 
Certainly, you can implement every algorithm you want in ocaml, but I 
personally feel that a Hindley-Milner like type system restricts my 
freedom too much and occasionally gets in the way when I want to implement
solutions to problems that are most naturally viewed from a lambda 
calculus point of view. On the other hand, I experience the benefits of 
such a type system as limited; it is able to catch a lot of mistakes I 
would not have made anyway. Nah, I'm rather a LISP hacker.

> (which already exists in the standard library, it being
> mysteriously named Obj.magic and its use being highly discouraged).

For good. I bet this feature exists precisely to keep the LISP wizards 
happy who know about and are not afraid of these dark corners, and have 
(maybe out of stubbornness :-) ) a strong desire writing LISP code in 
ocaml. If there were only honest and upright ocaml programmers, such a 
feature certainly wouuld not exist, and hence it is perhaps a good idea to 
strongly discourage its use.

[I still do not have a good overview over the ocaml library and thus did 
not know this function. Thanks for that piece of information.]


BTW: I also strongly assume that internally, ocaml uses type tagging 
anyway, at least for the garbage collector; hence it may be possible to 
use just the engine of ocaml and build a dynamically typed lisp on top of 
it...? Would be terrific...


-- 
regards,               tf@cip.physik.uni-muenchen.de                 (o_
Dr. Thomas Fischbacher -  http://www.cip.physik.uni-muenchen.de/~tf  //\
(lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y)              V_/_
(if (= x 0) y (g g (- x 1) (* x y)))) n 1))                     (Debian GNU)

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


  parent reply	other threads:[~2004-01-29 17:17 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-27  6:32 [Caml-list] ocaml killer Alexander Epifanov
2004-01-27  8:56 ` Alex Baretta
2004-01-27  9:43   ` Alexander Epifanov
2004-01-27 18:32     ` Shawn Wagner
2004-01-28  4:38       ` skaller
2004-01-28  5:30         ` james woodyatt
     [not found]   ` <40168498.6070708@tfb.com>
2004-01-27 19:10     ` Alex Baretta
2004-01-28 13:29       ` David Fox
2004-01-28 15:12         ` Eray Ozkural
2004-01-27  9:41 ` Alexander Danilov
2004-01-27  9:57   ` Alexander Epifanov
2004-01-27 16:43     ` Eric Stokes
2004-01-27 18:19       ` David Fox
2004-01-27 18:47       ` Richard Jones
2004-01-27 19:29         ` Eric Stokes
2004-01-28 13:30 ` Eray Ozkural
2004-01-28 23:26 ` Chet Murthy
2004-01-28 23:47   ` Martin Berger
2004-01-29  0:00     ` Chet Murthy
2004-01-29  0:04       ` Chet Murthy
2004-01-29  0:11       ` Martin Berger
2004-01-29  0:34         ` Chet Murthy
2004-01-29  0:47           ` [Caml-list] ocaml killer' Matt Gushee
2004-01-29  8:52           ` [Caml-list] ocaml killer Thomas Fischbacher
2004-01-29 16:20             ` fancy types (was Re: [Caml-list] ocaml killer) William Lovas
2004-01-29 17:13               ` james woodyatt
2004-01-29 17:26                 ` Benedikt Grundmann
2004-01-29 17:17               ` Thomas Fischbacher [this message]
2004-01-29 17:41                 ` Andreas Rossberg
2004-01-29 19:18                   ` William Lovas
2004-01-30 10:36                     ` Thomas Fischbacher
2004-01-31  3:39                       ` William Lovas
2004-02-01  2:11                         ` Vasile Rotaru
2004-02-02 11:08                           ` Florian Hars
2004-01-29 18:33                 ` Alex Baretta
2004-01-29 17:53         ` [Caml-list] ocaml killer skaller
2004-01-29  5:20     ` Brian Hurt
2004-01-29  6:36   ` Alexander Epifanov
2004-01-29  8:53   ` [Caml-list] ocaml and concurrency james woodyatt
2004-01-29  9:46     ` Vitaly Lugovsky
2004-01-29 10:37       ` Martin Berger
2004-01-29 11:51         ` Michael Hicks
2004-01-29 12:20         ` Alex Baretta
2004-01-29 12:43           ` Martin Berger
2004-01-29 15:42         ` Vitaly Lugovsky
2004-01-29 16:11           ` Martin Berger
2004-01-29 16:56             ` Andreas Rossberg
2004-01-29 17:19               ` james woodyatt
2004-01-29 17:43               ` Martin Berger
2004-01-29 17:54                 ` Andreas Rossberg
2004-01-29 18:08                   ` Martin Berger
2004-01-30  0:19                   ` Lauri Alanko
2004-01-29 19:37                 ` skaller
2004-01-30  0:05                   ` Martin Berger
2004-01-30  6:52                     ` Brian Hurt
2004-01-30  8:53                       ` Issac Trotts
2004-01-30 20:45                       ` skaller
2004-01-31  6:29                         ` Brian Hurt
2004-01-30 20:12                     ` skaller
2004-01-29 18:35         ` skaller
2004-01-29  9:56     ` Alex Baretta
2004-01-29 18:26     ` skaller

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=Pine.LNX.4.58.0401291736560.3416@seekar.cip.physik.uni-muenchen.de \
    --to=thomas.fischbacher@physik.uni-muenchen.de \
    --cc=caml-list@inria.fr \
    --cc=wlovas@stwing.upenn.edu \
    /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).