caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* value restriction
@ 2006-11-17  3:09 Jacques Carette
  0 siblings, 0 replies; 3+ messages in thread
From: Jacques Carette @ 2006-11-17  3:09 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: value restriction
  2010-01-01 23:05 Jacques Le Normand
@ 2010-01-02 16:25 ` Jacques Le Normand
  0 siblings, 0 replies; 3+ messages in thread
From: Jacques Le Normand @ 2010-01-02 16:25 UTC (permalink / raw)
  To: caml-list caml-list

[-- Attachment #1: Type: text/plain, Size: 772 bytes --]

on another note (but staying very much on the same topic), why won't the
following generalize:

# let foo =
    let counter  = ref 0 in
    let bar = !counter  in
    let baz = fun x -> bar
    in
      baz


val foo : '_a -> int = <fun>


baz clearly has a polymorphic type, yet foo doesn't.
Is there any way around this ?

--Jacques L.

On Fri, Jan 1, 2010 at 6:05 PM, Jacques Le Normand <rathereasy@gmail.com>wrote:

> Hello caml-list,
> with respect to the value restriction, what exactly constitutes a value?
> the textbook definition doesn't seem to hold, since the following
> generalizes:
>
> let f = let x = 1 in fun g h x -> g (h x);;
>
> while this won't:
>
> let f () = let x = (fun x -> x) (fun x -> x) in fun g h x -> g (h x);;
>
> cheers
>
> --Jacques L.
>

[-- Attachment #2: Type: text/html, Size: 1370 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* value restriction
@ 2010-01-01 23:05 Jacques Le Normand
  2010-01-02 16:25 ` Jacques Le Normand
  0 siblings, 1 reply; 3+ messages in thread
From: Jacques Le Normand @ 2010-01-01 23:05 UTC (permalink / raw)
  To: caml-list caml-list

[-- Attachment #1: Type: text/plain, Size: 327 bytes --]

Hello caml-list,
with respect to the value restriction, what exactly constitutes a value?
the textbook definition doesn't seem to hold, since the following
generalizes:

let f = let x = 1 in fun g h x -> g (h x);;

while this won't:

let f () = let x = (fun x -> x) (fun x -> x) in fun g h x -> g (h x);;

cheers

--Jacques L.

[-- Attachment #2: Type: text/html, Size: 490 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-01-02 16:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-17  3:09 value restriction Jacques Carette
2010-01-01 23:05 Jacques Le Normand
2010-01-02 16:25 ` Jacques Le Normand

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).