From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.5 required=5.0 tests=AWL,DNS_FROM_RFC_ABUSE, SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 99EC6BC68 for ; Sat, 28 Oct 2006 19:17:39 +0200 (CEST) Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.174]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id k9SHHdrt008098 for ; Sat, 28 Oct 2006 19:17:39 +0200 Received: by ug-out-1314.google.com with SMTP id 78so867751ugn for ; Sat, 28 Oct 2006 10:17:38 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:subject:from:to:in-reply-to:references:content-type:date:message-id:mime-version:x-mailer:content-transfer-encoding; b=DbqrzFXQJJMjO5HEMZn1A78GIobKgXNaEqnm/lngIlefKPXkh948+MwYOwDhrCxWMKfmUK9tnZ3g/mbf75/g420D7vdzRB2F4zomD7x/1N5owqbDOt7ZYfoXg3MJNIsmsbAcCIu3ixlqmaMv1YXkwbuUbWPCj2gTG0QmG2GImb0= Received: by 10.67.30.6 with SMTP id h6mr1477722ugj; Sat, 28 Oct 2006 10:17:38 -0700 (PDT) Received: from ?10.0.0.41? ( [88.154.194.131]) by mx.google.com with ESMTP id z40sm839704ugc.2006.10.28.10.17.37; Sat, 28 Oct 2006 10:17:38 -0700 (PDT) Subject: Re: [Caml-list] weird type behavior From: Kirill To: caml-list@yquem.inria.fr In-Reply-To: <20061028161226.GA28781@furbychan.cocan.org> References: <1162050549.23148.26.camel@nfnl> <20061028161226.GA28781@furbychan.cocan.org> Content-Type: text/plain Date: Sat, 28 Oct 2006 19:17:19 +0200 Message-Id: <1162055839.23148.56.camel@nfnl> Mime-Version: 1.0 X-Mailer: Evolution 2.8.1 Content-Transfer-Encoding: 7bit X-j-chkmail-Score: MSGID : 454390B3.000 on discorde : j-chkmail score : X : 0/20 1 X-Miltered: at discorde with ID 454390B3.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; beginners':01 lambda:01 ocaml:01 pred:01 prev:01 prev:01 pred:01 val:01 lambda:01 val:01 0100,:01 0200,:01 ocaml:01 foo:01 foo:01 (resending to the mailing list - sorry for the duplicate message) Hello Richard and thanks for your answer. This does explain quite a bit, but I am still stuck with other, seemingly related, problems, which I think are too complex for beginners' list, so I'll post it here in hope someone has an answer. What I was really trying to do is learn Church's lambda arithmetic by implementing it in Ocaml. The things went quite well, until I approached PRED operation, for which results turned out to be unexpected. I won't post here all functions in the program - only the 3 most important ones. I'm sure no one here will have problems with car, cdr and cons functions. let rec create_Lnum = function | 0 -> let num f x = x in num | n -> let prev = create_Lnum (n-1) in let num f x = f (prev f x) in num ;; let succ lnum = let num f x = f (lnum f x) in num ;; let pred lnum = let calc_last_pair = lnum ( let next_pair pair = let curr = car pair in cons (succ curr) curr in next_pair ) ( let first_pair = let zero = create_Lnum 0 in cons zero zero in first_pair ) in cdr calc_last_pair ;; Now, there are many things that don't work right when I'm trying to use the above code, so I'll just throw the first one right off and let's see, where it gets us. For this, I'll define a short helper function inc: # let inc x = x + 1;; val inc : int -> int = Now I create a lambda number and use inc function to make sure it really calls inc 5 times: # let five = create_Lnum 5;; val five : ('_a -> '_a) -> '_a -> '_a = # five inc 10;; - : int = 15 # five;; - : (int -> int) -> int -> int = So far, so good, everything worked as expected. I repeat the same thing, but now also get a five's predecessor: # let five = create_Lnum 5;; val five : ('_a -> '_a) -> '_a -> '_a = # let four = pred five;; val four : ('_a -> '_a) -> '_a -> '_a = # five inc 10;; This expression has type int -> int but is here used with type (((('a -> 'a) -> 'a -> 'a) -> (('a -> 'a) -> 'a -> 'a) -> ('a -> 'a) -> 'a -> 'a) -> ('a -> 'a) -> 'a -> 'a) -> ((('a -> 'a) -> 'a -> 'a) -> (('a -> 'a) -> 'a -> 'a) -> ('a -> 'a) -> 'a -> 'a) -> ('a -> 'a) -> 'a -> 'a It doesn't work now, and the error message isn't really helpful =( Q1: why? Q2: how can I make it work in the expected way? Thanks in advance, -Kirill On Sat, 2006-10-28 at 17:12 +0100, Richard Jones wrote: > On Sat, Oct 28, 2006 at 05:49:08PM +0200, Kirill wrote: > > I'm a novice user of OCaml and functional languages in general. While > > playing with the interpreter, writing all kinds of functions, I've run > > into a behavior that doesn't look quite right to me. Please excuse me, > > if it is explained somewhere and I simply haven't gotten there yet. > > > > In the following example, I define a simple function foo that returns > > function bar, which, in turn, accepts 2 parameters. The weird part is > > that after bar is being called for the first time, its signature changes > > from polymorphic types to ints. Right after (foo 3) call it's > > '_a -> '_b -> '_b = > > But after being called, it becomes > > (int -> int) -> int -> int = > > Type variables like '_a are covered here: > > http://caml.inria.fr/pub/old_caml_site/FAQ/FAQ_EXPERT-eng.html#variables_de_types_faibles > > There's a mailing list for beginners' questions: > > http://tech.groups.yahoo.com/group/ocaml_beginners/ > > Rich. >