caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Random.self_init in Jane Street Core
@ 2014-05-21 12:10 Ollie Frolovs
  2014-05-21 12:18 ` Dmitry Grebeniuk
  2014-05-23  0:12 ` Nathan Mishra Linger
  0 siblings, 2 replies; 9+ messages in thread
From: Ollie Frolovs @ 2014-05-21 12:10 UTC (permalink / raw)
  To: caml users

Hello

I’ve been trying to use Random.self_init in Jane Street’s Core but every time I run my program it returns the same result as if the self_init is in fact completely deterministic. Two questions – what am I doing wrong and how do I make it “random” (as in returning different values on each run of the application).

I compile the following source into native code with “corebuild”.

When I run the program, the result is ALWAYS
1 0                                                                            
1 1                                                                             
0 1
0 2
0 3

I’ve also upload the code and the output from "opam list -i” on GitHub, if that’s more convenient https://gist.github.com/olliefr/d6312d8195e9a30aa80c

I believe I have the latest compiler/libraries. The system is OS X Mavericks.

Many thanks,

Ollie

--
(* SOURCE CODE BEGINS *) 

open Core.Std                                                                   
                                                                                
let _ = Random.self_init                                                        

(*                                                                              
  FIXME there must be something in the standard library to do this!             
                                                                                
  Iterate a function over a value, tail-recursively.                            
  n: how many times                                                             
  f: function to apply                                                          
  a: initial value of the argument                                              
*)                                                                              
let rec iterate n f a =                                                         
  if n<=0                                                                       
  then a                                                                        
  else iterate (n-1) f (f a)                                                    

(* Wandering Light *)                                                           
let light = (0,0)

let wander (x,y) =                                                              
  match (1 + Random.int 4) with                                                 
      1 -> (x+1, y)                                                             
    | 2 -> (x, y+1)                                                             
    | 3 -> (x-1, y)                                                             
    | 4 -> (x, y+1)                                                             
    | _ -> failwith "random direction is not 1 to 4, wtf?"                      
                                                                                
let render (x,y) = printf "%i %i\n" x y                                         
                                                                                
let step light =                                                                
  let newlight = wander light in                                                
  render newlight;                                                              
  newlight                                                                      
                                                                                
let _ = iterate 5 step (0,0)

(* THE END *)

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

* Re: [Caml-list] Random.self_init in Jane Street Core
  2014-05-21 12:10 [Caml-list] Random.self_init in Jane Street Core Ollie Frolovs
@ 2014-05-21 12:18 ` Dmitry Grebeniuk
  2014-05-21 12:25   ` Ollie Frolovs
  2014-05-23  0:12 ` Nathan Mishra Linger
  1 sibling, 1 reply; 9+ messages in thread
From: Dmitry Grebeniuk @ 2014-05-21 12:18 UTC (permalink / raw)
  To: Ollie Frolovs; +Cc: caml users

Hello.

> let _ = Random.self_init

  That's why I almost never use "let _ = ...", or
constrain "_" to some type when I use it.
  Try to replace it with "let () = ..." and follow compiler
errors.  Or with "let (_ : unit) = ...".

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

* Re: [Caml-list] Random.self_init in Jane Street Core
  2014-05-21 12:18 ` Dmitry Grebeniuk
@ 2014-05-21 12:25   ` Ollie Frolovs
  2014-05-21 12:28     ` David House
  0 siblings, 1 reply; 9+ messages in thread
From: Ollie Frolovs @ 2014-05-21 12:25 UTC (permalink / raw)
  To: caml users

oh dear! i think i know what happened. self_init has never been called in the first place! it requires a unit argument which i did not give it, so the “alias” to Random.self_init was assigned to nothing, so to speak, instead of calling the function. 

Many thanks, Dmitry! I’ve amended that line to let () = Random.self_init () and it works.

On 21 May 2014, at 13:18, Dmitry Grebeniuk <gdsfh1@gmail.com> wrote:

> Hello.
> 
>> let _ = Random.self_init
> 
>  That's why I almost never use "let _ = ...", or
> constrain "_" to some type when I use it.
>  Try to replace it with "let () = ..." and follow compiler
> errors.  Or with "let (_ : unit) = ...".


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

* Re: [Caml-list] Random.self_init in Jane Street Core
  2014-05-21 12:25   ` Ollie Frolovs
@ 2014-05-21 12:28     ` David House
  2014-05-21 16:40       ` Martin Jambon
  0 siblings, 1 reply; 9+ messages in thread
From: David House @ 2014-05-21 12:28 UTC (permalink / raw)
  To: Ollie Frolovs; +Cc: caml users

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

Relatedly, one should be careful using the [ignore] function. Always give
its argument a type signature. E.g. if you do this:

  ignore (my_function foo);

Then this will start silently not calling [my_function] whenever someone
adds a second argument. You should instead use:

  ignore (my_function foo : Foo.t);


On 21 May 2014 13:25, Ollie Frolovs <ollie.frolovs.2012@my.bristol.ac.uk>wrote:

> oh dear! i think i know what happened. self_init has never been called in
> the first place! it requires a unit argument which i did not give it, so
> the “alias” to Random.self_init was assigned to nothing, so to speak,
> instead of calling the function.
>
> Many thanks, Dmitry! I’ve amended that line to let () = Random.self_init
> () and it works.
>
> On 21 May 2014, at 13:18, Dmitry Grebeniuk <gdsfh1@gmail.com> wrote:
>
> > Hello.
> >
> >> let _ = Random.self_init
> >
> >  That's why I almost never use "let _ = ...", or
> > constrain "_" to some type when I use it.
> >  Try to replace it with "let () = ..." and follow compiler
> > errors.  Or with "let (_ : unit) = ...".
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

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

* Re: [Caml-list] Random.self_init in Jane Street Core
  2014-05-21 12:28     ` David House
@ 2014-05-21 16:40       ` Martin Jambon
  2014-05-22  9:01         ` Ben Millwood
  0 siblings, 1 reply; 9+ messages in thread
From: Martin Jambon @ 2014-05-21 16:40 UTC (permalink / raw)
  To: David House; +Cc: caml users, Ollie Frolovs

On Wed 21 May 2014 05:28:00 AM PDT, David House wrote:
> Relatedly, one should be careful using the [ignore] function. Always
> give its argument a type signature. E.g. if you do this:
>
>   ignore (my_function foo);
>
> Then this will start silently not calling [my_function] whenever
> someone adds a second argument. You should instead use:
>
>   ignore (my_function foo : Foo.t);

I think it used to be a problem but it no longer is. Recent versions of 
OCaml give a warning when passing a function, so there's no need for a 
type annotation:

$ ocaml
        OCaml version 4.01.0

# ignore print_endline;;
Warning 5: this function application is partial,
maybe some arguments are missing.
- : unit = ()


>
> On 21 May 2014 13:25, Ollie Frolovs
> <ollie.frolovs.2012@my.bristol.ac.uk
> <mailto:ollie.frolovs.2012@my.bristol.ac.uk>> wrote:
>
>     oh dear! i think i know what happened. self_init has never been
>     called in the first place! it requires a unit argument which i did
>     not give it, so the “alias” to Random.self_init was assigned to
>     nothing, so to speak, instead of calling the function.
>
>     Many thanks, Dmitry! I’ve amended that line to let () =
>     Random.self_init () and it works.
>
>     On 21 May 2014, at 13:18, Dmitry Grebeniuk <gdsfh1@gmail.com
>     <mailto:gdsfh1@gmail.com>> wrote:
>
>     > Hello.
>     >
>     >> let _ = Random.self_init
>     >
>     >  That's why I almost never use "let _ = ...", or
>     > constrain "_" to some type when I use it.
>     >  Try to replace it with "let () = ..." and follow compiler
>     > errors.  Or with "let (_ : unit) = ...".
>
>
>     --
>     Caml-list mailing list.  Subscription management and archives:
>     https://sympa.inria.fr/sympa/arc/caml-list
>     Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>     Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>



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

* Re: [Caml-list] Random.self_init in Jane Street Core
  2014-05-21 16:40       ` Martin Jambon
@ 2014-05-22  9:01         ` Ben Millwood
  2014-05-22 17:22           ` Martin Jambon
  0 siblings, 1 reply; 9+ messages in thread
From: Ben Millwood @ 2014-05-22  9:01 UTC (permalink / raw)
  To: Martin Jambon; +Cc: David House, caml users, Ollie Frolovs

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

On 21 May 2014 17:40, Martin Jambon <martin.jambon@ens-lyon.org> wrote:

> On Wed 21 May 2014 05:28:00 AM PDT, David House wrote:
>
>> Relatedly, one should be careful using the [ignore] function. Always
>> give its argument a type signature. E.g. if you do this:
>>
>>   ignore (my_function foo);
>>
>> Then this will start silently not calling [my_function] whenever
>> someone adds a second argument. You should instead use:
>>
>>   ignore (my_function foo : Foo.t);
>>
>
> I think it used to be a problem but it no longer is. Recent versions of
> OCaml give a warning when passing a function, so there's no need for a type
> annotation:
>
> $ ocaml
>        OCaml version 4.01.0
>
> # ignore print_endline;;
> Warning 5: this function application is partial,
> maybe some arguments are missing.
> - : unit = ()
>

You're right that this warning exists and mitigates the problem somewhat,
but it doesn't save you from the scenario where, say, a function changes
from returning unit and throwing exceptions in the case of error to
returning some error value, or vice versa. It seems just generally if the
return type of a function you use changes that's probably something you'd
want noise made about.


>> On 21 May 2014 13:25, Ollie Frolovs
>> <ollie.frolovs.2012@my.bristol.ac.uk
>> <mailto:ollie.frolovs.2012@my.bristol.ac.uk>> wrote:
>>
>>     oh dear! i think i know what happened. self_init has never been
>>     called in the first place! it requires a unit argument which i did
>>     not give it, so the “alias” to Random.self_init was assigned to
>>     nothing, so to speak, instead of calling the function.
>>
>>     Many thanks, Dmitry! I’ve amended that line to let () =
>>     Random.self_init () and it works.
>>
>>     On 21 May 2014, at 13:18, Dmitry Grebeniuk <gdsfh1@gmail.com
>>     <mailto:gdsfh1@gmail.com>> wrote:
>>
>>     > Hello.
>>     >
>>     >> let _ = Random.self_init
>>     >
>>     >  That's why I almost never use "let _ = ...", or
>>     > constrain "_" to some type when I use it.
>>     >  Try to replace it with "let () = ..." and follow compiler
>>     > errors.  Or with "let (_ : unit) = ...".
>>
>>
>>     --
>>     Caml-list mailing list.  Subscription management and archives:
>>     https://sympa.inria.fr/sympa/arc/caml-list
>>     Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>>     Bug reports: http://caml.inria.fr/bin/caml-bugs
>>
>>
>>
>
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

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

* Re: [Caml-list] Random.self_init in Jane Street Core
  2014-05-22  9:01         ` Ben Millwood
@ 2014-05-22 17:22           ` Martin Jambon
  0 siblings, 0 replies; 9+ messages in thread
From: Martin Jambon @ 2014-05-22 17:22 UTC (permalink / raw)
  To: Ben Millwood; +Cc: David House, caml users, Ollie Frolovs

On Thu 22 May 2014 02:01:01 AM PDT, Ben Millwood wrote:
> On 21 May 2014 17:40, Martin Jambon <martin.jambon@ens-lyon.org
> <mailto:martin.jambon@ens-lyon.org>> wrote:
>
>     On Wed 21 May 2014 05:28:00 AM PDT, David House wrote:
>
>         Relatedly, one should be careful using the [ignore] function.
>         Always
>         give its argument a type signature. E.g. if you do this:
>
>           ignore (my_function foo);
>
>         Then this will start silently not calling [my_function] whenever
>         someone adds a second argument. You should instead use:
>
>           ignore (my_function foo : Foo.t);
>
>
>     I think it used to be a problem but it no longer is. Recent
>     versions of OCaml give a warning when passing a function, so
>     there's no need for a type annotation:
>
>     $ ocaml
>            OCaml version 4.01.0
>
>     # ignore print_endline;;
>     Warning 5: this function application is partial,
>     maybe some arguments are missing.
>     - : unit = ()
>
>
> You're right that this warning exists and mitigates the problem
> somewhat, but it doesn't save you from the scenario where, say, a
> function changes from returning unit and throwing exceptions in the
> case of error to returning some error value, or vice versa. It seems
> just generally if the return type of a function you use changes that's
> probably something you'd want noise made about.

I can see it's a problem when the function that throws an exception 
does not return unit. However if the original function returns unit, we 
don't use ignore in the first place and we get a warning if the return 
type changes. If the original function returns something but then 
switches to unit + exception, I guess the compiler could warn against 
that too:

  Warning X: this 'ignore' is useless


>         On 21 May 2014 13:25, Ollie Frolovs
>         <ollie.frolovs.2012@my.__bristol.ac.uk
>         <mailto:ollie.frolovs.2012@my.bristol.ac.uk>
>         <mailto:ollie.frolovs.2012@my.__bristol.ac.uk
>         <mailto:ollie.frolovs.2012@my.bristol.ac.uk>>> wrote:
>
>             oh dear! i think i know what happened. self_init has never
>         been
>             called in the first place! it requires a unit argument
>         which i did
>             not give it, so the “alias” to Random.self_init was
>         assigned to
>             nothing, so to speak, instead of calling the function.
>
>             Many thanks, Dmitry! I’ve amended that line to let () =
>             Random.self_init () and it works.
>
>             On 21 May 2014, at 13:18, Dmitry Grebeniuk
>         <gdsfh1@gmail.com <mailto:gdsfh1@gmail.com>
>             <mailto:gdsfh1@gmail.com <mailto:gdsfh1@gmail.com>>> wrote:
>
>             > Hello.
>             >
>             >> let _ = Random.self_init
>             >
>             >  That's why I almost never use "let _ = ...", or
>             > constrain "_" to some type when I use it.
>             >  Try to replace it with "let () = ..." and follow compiler
>             > errors.  Or with "let (_ : unit) = ...".
>
>
>             --
>             Caml-list mailing list.  Subscription management and archives:
>         https://sympa.inria.fr/sympa/__arc/caml-list
>         <https://sympa.inria.fr/sympa/arc/caml-list>
>             Beginner's list:
>         http://groups.yahoo.com/group/__ocaml_beginners
>         <http://groups.yahoo.com/group/ocaml_beginners>
>             Bug reports: http://caml.inria.fr/bin/caml-__bugs
>         <http://caml.inria.fr/bin/caml-bugs>
>
>
>
>
>
>     --
>     Caml-list mailing list.  Subscription management and archives:
>     https://sympa.inria.fr/sympa/__arc/caml-list
>     <https://sympa.inria.fr/sympa/arc/caml-list>
>     Beginner's list: http://groups.yahoo.com/group/__ocaml_beginners
>     <http://groups.yahoo.com/group/ocaml_beginners>
>     Bug reports: http://caml.inria.fr/bin/caml-__bugs
>     <http://caml.inria.fr/bin/caml-bugs>
>
>



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

* Re: [Caml-list] Random.self_init in Jane Street Core
  2014-05-21 12:10 [Caml-list] Random.self_init in Jane Street Core Ollie Frolovs
  2014-05-21 12:18 ` Dmitry Grebeniuk
@ 2014-05-23  0:12 ` Nathan Mishra Linger
  2014-05-23  0:15   ` Nathan Mishra Linger
  1 sibling, 1 reply; 9+ messages in thread
From: Nathan Mishra Linger @ 2014-05-23  0:12 UTC (permalink / raw)
  To: Ollie Frolovs; +Cc: caml users

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

I think the problem is that you aren't *calling* Random.self_init. To do so, change the line


  let _ = Random.self_init




to




  let () = Random.self_init ()




The value that _ is matching against in your program has type unit -> unit rather than just unit. 
—
Sent from Mailbox

On Wed, May 21, 2014 at 8:12 AM, Ollie Frolovs
<ollie.frolovs.2012@my.bristol.ac.uk> wrote:

> Hello
> I’ve been trying to use Random.self_init in Jane Street’s Core but every time I run my program it returns the same result as if the self_init is in fact completely deterministic. Two questions – what am I doing wrong and how do I make it “random” (as in returning different values on each run of the application).
> I compile the following source into native code with “corebuild”.
> When I run the program, the result is ALWAYS
> 1 0                                                                            
> 1 1                                                                             
> 0 1
> 0 2
> 0 3
> I’ve also upload the code and the output from "opam list -i” on GitHub, if that’s more convenient https://gist.github.com/olliefr/d6312d8195e9a30aa80c
> I believe I have the latest compiler/libraries. The system is OS X Mavericks.
> Many thanks,
> Ollie
> --
> (* SOURCE CODE BEGINS *) 
> open Core.Std                                                                   
>                                                                                 
> let _ = Random.self_init                                                        
> (*                                                                              
>   FIXME there must be something in the standard library to do this!             
>                                                                                 
>   Iterate a function over a value, tail-recursively.                            
>   n: how many times                                                             
>   f: function to apply                                                          
>   a: initial value of the argument                                              
> *)                                                                              
> let rec iterate n f a =                                                         
>   if n<=0                                                                       
>   then a                                                                        
>   else iterate (n-1) f (f a)                                                    
> (* Wandering Light *)                                                           
> let light = (0,0)
> let wander (x,y) =                                                              
>   match (1 + Random.int 4) with                                                 
>       1 -> (x+1, y)                                                             
>     | 2 -> (x, y+1)                                                             
>     | 3 -> (x-1, y)                                                             
>     | 4 -> (x, y+1)                                                             
>     | _ -> failwith "random direction is not 1 to 4, wtf?"                      
>                                                                                 
> let render (x,y) = printf "%i %i\n" x y                                         
>                                                                                 
> let step light =                                                                
>   let newlight = wander light in                                                
>   render newlight;                                                              
>   newlight                                                                      
>                                                                                 
> let _ = iterate 5 step (0,0)
> (* THE END *)
> -- 
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

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

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

* Re: [Caml-list] Random.self_init in Jane Street Core
  2014-05-23  0:12 ` Nathan Mishra Linger
@ 2014-05-23  0:15   ` Nathan Mishra Linger
  0 siblings, 0 replies; 9+ messages in thread
From: Nathan Mishra Linger @ 2014-05-23  0:15 UTC (permalink / raw)
  To: Ollie Frolovs; +Cc: caml users

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

Oops. Nevermind. —
Sent from Mailbox

On Thu, May 22, 2014 at 8:12 PM, Nathan Mishra Linger
<nathan.mishralinger@gmail.com> wrote:

> I think the problem is that you aren't *calling* Random.self_init. To do so, change the line
>   let _ = Random.self_init
> to
>   let () = Random.self_init ()
> The value that _ is matching against in your program has type unit -> unit rather than just unit. 
> —
> Sent from Mailbox
> On Wed, May 21, 2014 at 8:12 AM, Ollie Frolovs
> <ollie.frolovs.2012@my.bristol.ac.uk> wrote:
>> Hello
>> I’ve been trying to use Random.self_init in Jane Street’s Core but every time I run my program it returns the same result as if the self_init is in fact completely deterministic. Two questions – what am I doing wrong and how do I make it “random” (as in returning different values on each run of the application).
>> I compile the following source into native code with “corebuild”.
>> When I run the program, the result is ALWAYS
>> 1 0                                                                            
>> 1 1                                                                             
>> 0 1
>> 0 2
>> 0 3
>> I’ve also upload the code and the output from "opam list -i” on GitHub, if that’s more convenient https://gist.github.com/olliefr/d6312d8195e9a30aa80c
>> I believe I have the latest compiler/libraries. The system is OS X Mavericks.
>> Many thanks,
>> Ollie
>> --
>> (* SOURCE CODE BEGINS *) 
>> open Core.Std                                                                   
>>                                                                                 
>> let _ = Random.self_init                                                        
>> (*                                                                              
>>   FIXME there must be something in the standard library to do this!             
>>                                                                                 
>>   Iterate a function over a value, tail-recursively.                            
>>   n: how many times                                                             
>>   f: function to apply                                                          
>>   a: initial value of the argument                                              
>> *)                                                                              
>> let rec iterate n f a =                                                         
>>   if n<=0                                                                       
>>   then a                                                                        
>>   else iterate (n-1) f (f a)                                                    
>> (* Wandering Light *)                                                           
>> let light = (0,0)
>> let wander (x,y) =                                                              
>>   match (1 + Random.int 4) with                                                 
>>       1 -> (x+1, y)                                                             
>>     | 2 -> (x, y+1)                                                             
>>     | 3 -> (x-1, y)                                                             
>>     | 4 -> (x, y+1)                                                             
>>     | _ -> failwith "random direction is not 1 to 4, wtf?"                      
>>                                                                                 
>> let render (x,y) = printf "%i %i\n" x y                                         
>>                                                                                 
>> let step light =                                                                
>>   let newlight = wander light in                                                
>>   render newlight;                                                              
>>   newlight                                                                      
>>                                                                                 
>> let _ = iterate 5 step (0,0)
>> (* THE END *)
>> -- 
>> Caml-list mailing list.  Subscription management and archives:
>> https://sympa.inria.fr/sympa/arc/caml-list
>> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>> Bug reports: http://caml.inria.fr/bin/caml-bugs

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

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

end of thread, other threads:[~2014-05-23  0:15 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-21 12:10 [Caml-list] Random.self_init in Jane Street Core Ollie Frolovs
2014-05-21 12:18 ` Dmitry Grebeniuk
2014-05-21 12:25   ` Ollie Frolovs
2014-05-21 12:28     ` David House
2014-05-21 16:40       ` Martin Jambon
2014-05-22  9:01         ` Ben Millwood
2014-05-22 17:22           ` Martin Jambon
2014-05-23  0:12 ` Nathan Mishra Linger
2014-05-23  0:15   ` Nathan Mishra Linger

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