Re: How to escape the Ocaml's superfluous parentheses and type declarations?
- From: "Lukasz Stafiniak" <lukstafi@xxxxxxxxx>
- Date: Thu, 8 Sep 2005 23:35:02 +0000 (UTC)
andrewspencers@xxxxxxxxx wrote:
> # type 'a mylist = E | Mycons of 'a * 'a mylist;;
> type 'a mylist = E | Mycons of 'a * 'a mylist
> # let rec mylength x =
> match x with
> E -> 0
> | Mycons (i, l) -> 1 + mylength l;;
> val mylength : 'a mylist -> int = <fun>
> # mylength Mycons (1, Mycons (2, Mycons (3,E)));;
> This function is applied to too many arguments, maybe you forgot a `;'
>
> It demands parentheses?! What is this; Lisp?
Nonconstant constructors take tuples as arguments, that's why the inner
parentheses. Without the outer parenthese, f Mycons (1, ...) is
confused with applying f to a constant constructor Mycons and a tuple
(1, ...). If you don't like it, feel free to write your own syntax with
Camlp4. I myself sometimes get angry with writing, e.g.,
S(O(M(E(T(H(I(N(G)))))))). But then, Camlp4.
> Anyway, onward:
>
> # mylength (Mycons (3, Mycons ('a', E)));;
> This expression has type char but is here used with type int
>
> I thought that Ocaml was supposed to be able to infer types without my
> explicitly specifying them... why can't it infer the union of the types
> char and int?
Why would you need a thing which is either int or char, and you by no
means can tell which? If you do, I'd suggest you to use simply int
instead, and a coercion "int_of_char".
> Anyway, I'll define the union explicitly, so I can continue onward:
>
> # type charint = char | int;;
> Syntax error
> # type charint = Char of char | Int of int;;
> type charint = Char of char | Int of int
>
> Ok, so I have to type the type name twice...
Types are not classes. If you think OCaml is too simple, switch to
Haskell.
> Onward:
>
> # mylength (Mycons (3, Mycons ('a', E)));;
> This expression has type char but is here used with type int
>
> I've explicitly defined the union, and it STILL can't figure it out!
Well, it doesn't perform implicit injections to this defined disjoint
sum which is closest to your intended union type at that time.
> Let's make it even more explicit:
>
> # mylength (Mycons (Int 3, Mycons (Char 'a', E)));;
> - : int = 2
>
> Why is that necessary?
>
Because it is a programming language.
> # quit;;
> Unbound value quit
> # exit;;
> - : int -> 'a = <fun>
> # bye;;
> Unbound value bye
> # abort;;
> Unbound value abort
> # end;;
> Syntax error
> # logoff;;
> Unbound value logoff
> # logout;;
> Unbound value logout
> # help;;
> Unbound value help
>
And not a chat-bot.
> alt-tab
> >ps
> PID PPID PGID WINPID TTY UID STIME COMMAND
> I 956 1 956 1200 con 500 08:43:59
> /usr/bin/ocamlrun
> 376 1 376 376 con 500 13:58:28 /usr/bin/ps
> >kill 956
Well, you have learned some of Unix. Thank you anyway for your funny
letter, which happens to touch some deep issues concerning union types.
As for parentheses, I find OCaml syntax pleasurably light on them.
Kindest Regards,
Lukasz
.
- Follow-Ups:
- Re: How to escape the Ocaml's superfluous parentheses and type
- From: Henning Makholm
- Re: How to escape the Ocaml's superfluous parentheses and type
- References:
- How to escape the Ocaml's superfluous parentheses and type declarations?
- From: andrewspencers
- How to escape the Ocaml's superfluous parentheses and type declarations?
- Prev by Date: Can Monads be implemented in pure core SML?
- Next by Date: Re: How to escape the Ocaml's superfluous parentheses and type
- Previous by thread: Re: How to escape the Ocaml's superfluous parentheses and type declarations?
- Next by thread: Re: How to escape the Ocaml's superfluous parentheses and type
- Index(es):
Relevant Pages
|