caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christopher Dutchyn <cdutchyn@cs.ubc.ca>
To: Timo.Tapiola@tietoenator.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Function forward declaration?
Date: Tue, 6 Apr 2004 10:26:50 -0700 (PDT)	[thread overview]
Message-ID: <Pine.GSO.4.53.0404061009360.19072@cascade.cs.ubc.ca> (raw)
In-Reply-To: <60532B15DF92FD4693AA89B2F7E01D8F013F29EC@tmex02>


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


      parent reply	other threads:[~2004-04-06 17:26 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-06 12:14 Timo.Tapiola
2004-04-06 12:20 ` Andrew Birkett
2004-04-06 12:37 ` Remi Vanicat
2004-04-06 13:00   ` Issac Trotts
2004-04-06 12:53 ` Correnson Loïc
2004-04-06 15:14   ` skaller
2004-04-06 17:39   ` brogoff
2004-04-06 17:53     ` Richard Jones
2004-04-06 19:28       ` Marcin 'Qrczak' Kowalczyk
2004-04-06 22:37         ` Jon Harrop
2004-04-07  2:18         ` skaller
2004-04-07  6:01         ` Nicolas Cannasse
2004-04-07  7:31           ` Marcin 'Qrczak' Kowalczyk
2004-04-07 16:40             ` brogoff
2004-04-07 13:52           ` skaller
2004-04-07 14:15             ` Richard Jones
2002-01-03 15:21               ` Issac Trotts
2004-04-07 15:51               ` skaller
2004-04-07 16:41                 ` Richard Jones
2004-04-07 17:31                   ` Remi Vanicat
2004-04-07 17:36                     ` Richard Jones
2004-04-07 17:54                       ` Marcin 'Qrczak' Kowalczyk
2004-04-07 19:27                     ` skaller
2004-04-07 20:24                       ` Christopher Dutchyn
2004-04-07 18:04                   ` Benjamin Geer
2004-04-07 19:21                   ` skaller
2004-04-07 16:52               ` Shawn Wagner
2004-04-07 17:26               ` Basile Starynkevitch
2004-04-07 17:46                 ` Marcin 'Qrczak' Kowalczyk
2004-04-07 18:03                   ` Kenneth Knowles
2004-04-07 18:44                 ` Christopher Dutchyn
2004-04-07 14:24             ` Ville-Pertti Keinonen
2004-04-07 15:12               ` skaller
2004-04-06 12:58 ` Jean-Christophe Filliatre
2004-04-06 17:26 ` Christopher Dutchyn [this message]

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.GSO.4.53.0404061009360.19072@cascade.cs.ubc.ca \
    --to=cdutchyn@cs.ubc.ca \
    --cc=Timo.Tapiola@tietoenator.com \
    --cc=caml-list@inria.fr \
    /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).