From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p8OK69JZ023662 for ; Sat, 24 Sep 2011 22:06:10 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AoICAN83fk7UGyoClGdsb2JhbABDmS+ObRQBAQEBCQsJCRQDI4FTAQEEATIBOwsFCwshBCEPHRkSBhMSh2YCBLdYgxiDcwSZE4t0 X-IronPort-AV: E=Sophos;i="4.68,435,1312149600"; d="scan'208";a="110365371" Received: from smtp2-g21.free.fr ([212.27.42.2]) by mail4-smtp-sop.national.inria.fr with ESMTP; 24 Sep 2011 22:06:02 +0200 Received: from UNKNOWN (unknown [172.20.243.134]) by smtp2-g21.free.fr (Postfix) with ESMTP id 907864B000F; Sat, 24 Sep 2011 22:05:55 +0200 (CEST) Received: by UNKNOWN (Postfix, from userid 0) id 796DB9ACDFF94; Sat, 24 Sep 2011 22:05:54 +0200 (CEST) Received: from ([66.108.127.87]) by imp.free.fr (IMP) with HTTP for ; Sat, 24 Sep 2011 22:05:54 +0200 Message-ID: <1316894754.4e7e38225d52e@imp.free.fr> Date: Sat, 24 Sep 2011 22:05:54 +0200 From: pierrchp@free.fr To: Walter Cazzola Cc: OCaML Mailing List References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit User-Agent: Internet Messaging Program (IMP) 3.2.8 X-Originating-IP: 66.108.127.87 Subject: Re: [Caml-list] polymorphic stack Hello, Your problem here is that when you created your module stack, you wrote let empty = { c=[]} ;; which creates a global variable. Writing let s = empty creates a new stack by copying the value empty, therefore putting a type constraint, that s and empty must have the same type. In order to do what you want, you need to reevaluate the expression { c=[]} every time you create a new stack, and not just copy its result. This is achieved by using a function instead of a global variable: let empty () = { c=[]} an create a new stack with let s = empty () Hope it is not too confusing Cheers -Pierre Selon Walter Cazzola : > Hi all, > thanks a lot for your help in this travel through OCaML but I have still > a question. I have tried to write a polymorphic stack code attached but > I don't understand its behavior: > > # open Stack;; > # let s = empty;; > val s : '_a Stack.stack = {c = []} > # push s 7;; > - : unit = () > # push s 25;; > - : unit = () > # let s1 = empty;; > val s1 : int Stack.stack = {c = [25; 7]} > # push s1 "Hello";; > Error: This expression has type string but an expression was expected of > type > int > > Apparently seems that I can have only a variable of type stack and any > other call to its constructor links the new variable to the old one. > This means that once I have put an int inside I can't have a second > stack for characters or what else? This behavior is completely > unexpected and I can't explain it. > > I'm sure I'm doing something wrong but I can't say what it is. Do you > have any idea about? > > TIA > Walter > > -- > -- > Caml-list mailing list. Subscription management and archives: > https://sympa-roc.inria.fr/wws/info/caml-list > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > >