Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Function forward declaration?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Christopher Dutchyn <cdutchyn@c...>
Subject: Re: [Caml-list] Function forward declaration?

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