From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA05532; Tue, 6 Apr 2004 19:26:54 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA05512 for ; Tue, 6 Apr 2004 19:26:53 +0200 (MET DST) Received: from pedigree.cs.ubc.ca (pedigree.cs.ubc.ca [142.103.6.50]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i36HRhjq008980 for ; Tue, 6 Apr 2004 19:27:44 +0200 Received: from cascade.cs.ubc.ca (cascade.cs.ubc.ca [142.103.7.7]) by pedigree.cs.ubc.ca (8.12.10/8.11.4) with ESMTP id i36HQoXd005611; Tue, 6 Apr 2004 10:26:50 -0700 (PDT) Date: Tue, 6 Apr 2004 10:26:50 -0700 (PDT) From: Christopher Dutchyn To: Timo.Tapiola@tietoenator.com cc: caml-list@inria.fr Subject: Re: [Caml-list] Function forward declaration? In-Reply-To: <60532B15DF92FD4693AA89B2F7E01D8F013F29EC@tmex02> Message-ID: References: <60532B15DF92FD4693AA89B2F7E01D8F013F29EC@tmex02> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; ubc:99 caml-list:01 typecore:01 letrec:01 allocating:01 2004:99 funct:01 expr:01 expr:01 bug:01 faq:01 faq:01 beginner's:01 beginners:01 bin:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 76 I've seen something like this in the ocaml source (cf. typing/typecore.ml): let my_fun_ = ref ((fun x -> assert false) : (int -> int));; let my_fun = fun x -> !my_fun_ x (* can't be just !my_fun_ *);; (* elsewhere*) my_fun_ := (fun x -> x + 1) ;; (* elsewhere again *) (my_fun 2);; This breaks the lexical connection that letrec requires, by explicitly allocating and mutating the reference cell. In this way, the declaration and definition can be separated. Of course, the typechecker can't ensure that you're using my_fun_ only after it is replaced with it's real value. Chris D. On Tue, 6 Apr 2004 Timo.Tapiola@tietoenator.com wrote: > Hi, > > could someone tell me if there is some way to forward declare functions in > OCaml? > > Code example, some parts of code removed: > > ---------------------------------------------------------------------------- > ---------------------------------------------------------------------------- > -------------- > > let evaluate_funcall funsig = > let func = Funspace.get_funref funsig in > match !func with > Funct(s, block) -> interpret_block block; > ValNone > ;; > > ---------------------------------------------------------------------------- > ---------------------------------------------------------------------------- > -------------- > > let rec evaluate_expr expr = > match expr with > ExpInt(int) -> > ValInt(int) > | ExpFloat(float) -> > ValFloat(float) > | ExpString(str) -> > ValString(str) > | ExpBool(bool) -> > ValBool(bool) > | ExpVar(var) -> let > varval = Varspace.get_varvalue var in > > if varval = ValNone then > > raise (Interpret_error("Error: variable not declared.\n")) > > else > > varval > | ExpBinary(ex1, bin, ex2) -> > let value1 = evaluate_expr ex1 in > > let value2 = evaluate_expr ex2 in > > evaluate_binary value1 bin value2 > | ExpUnary(un, ex) -> let > value1 = evaluate_expr ex in > > evaluate_unary un value1 > | ExpCast(vartype, ex) -> let value > = evaluate_expr ex in > > evaluate_cast vartype value > | ExpFunCall(funsig) -> > evaluate_funcall funsig > > ;; > > ---------------------------------------------------------------------------- > ---------------------------------------------------------------------------- > -------------- > > let interpret_phrase phrase = > match phrase with > PhrExpression(exp) -> evaluate_expr exp; () > | PhrStatement(stmt) -> interpret_stmt stmt > | PhrCommand(cmd) -> interpret_cmd cmd; > ;; > > ---------------------------------------------------------------------------- > ---------------------------------------------------------------------------- > -------------- > > let rec interpret_phrases phrases = > match phrases with > [] -> () > | p::r -> interpret_phrase p; interpret_phrases r > ;; > > ---------------------------------------------------------------------------- > ---------------------------------------------------------------------------- > -------------- > > let interpret_block block = > match block with > Block([]) -> () > | Block(phrases) -> interpret_phrases phrases > ;; > > ---------------------------------------------------------------------------- > ---------------------------------------------------------------------------- > -------------- > > Call path starting from function interpret_block: > > interpret_block > interpret_phrases > interpret_phrase > interpret_expr > evaluate_funcall > interpret_block > > > How should I deal with this situation if there is no way to forward declare > functions? > > Thanks in advance, > > Timo > > ------------------- > 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 > ------------------- 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