Version française
Home     About     Download     Resources     Contact us    
Browse thread
value restriction
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Carette <carette@m...>
Subject: value restriction
I am wondering if there is a (nice) way around the value restriction in 
a particular case we have encountered.
Inside a Functor, we compute a value as

let make_result =
	match IF.R.fin with
	| Some rank_action ->
	function m -> perform
		      rank <-- rank_action ();
		      ret (Tuple.tup2 m.matrix rank)
        | None -> failwith "Rank is not computed here"

(where the above uses the campl4 extension for monadic notation).
By design, we _want_ the failure to happen at Functor instantiation time 
(if it will fail), so the usual 'fix' of having make_result () does not 
help.
But the code above does not work because of the value restriction :-(

Instead, we currently resort to

let make_result m = perform
    rank <-- fromJust IF.R.fin ();
    ret (Tuple.tup2 m.matrix rank)
(* Initialization: check the preconditions of instantiation of this struct*)
let () = notNone IF.R.fin "Rank is not computed here"

where
let fromJust = function Some x -> x | None -> failwith "Can't happen"
let notNone v str = match v with None -> failwith str | Some _ -> ()

which is clearly very ugly.  So ugly that when 'Obj.magic' came up as a 
possibility around this, it was not immediately rejected.  Yes, that ugly.

The one possibility we have considered is to make IF.R.fin of type  unit 
-> 'a monad (no option) and instead of having None, we'd have
    fin () = failwith "Rank is not computed"

Then we would have to do

let make_result m = perform
    rank <-- IF.R.fin ();
    ret (Tuple.tup2 m.matrix rank)
(* Initialization: check the preconditions of instantiation of this struct*)
let _ = IF.R.fin ()

That would invoke IF.R.fin speculatively, and if that works, then invoke 
it 'for real' (but monadically the second time) later, when we know it 
will work.  That certainly works, and has a certain appeal.  And yet...

Jacques