Ur/Web Examples
Check-in [deb130b5b7]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:- change naming conventions (both in terms of files and functions) of examples pertaining random generation
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: deb130b5b772d56d96ce895bfadfc88e8ba903ef
User & Date: beyert 2015-02-14 04:03:23.707
Context
2015-02-14
04:06
- small cleanups check-in: c1b08406b6 user: beyert tags: trunk
04:03
- change naming conventions (both in terms of files and functions) of examples pertaining random generation check-in: deb130b5b7 user: beyert tags: trunk
03:27
- add two pseudo-random number generation examples: one is simple, the other generates random numbers "continuously" using periodicT check-in: 6d1c6f8af3 user: beyert tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to .fossil-settings/ignore-glob.
1
2
3
4
5
6
7
8
9
10
.fossilmsg
jsMove/.make-targets
jsMove/*.exe
iota/.make-targets
iota/*.exe
prand/.make-targets
prand/*.exe
prand/*.o
randGen/.make-targets
randGen/*.exe





|
|
|
|
|
1
2
3
4
5
6
7
8
9
10
.fossilmsg
jsMove/.make-targets
jsMove/*.exe
iota/.make-targets
iota/*.exe
pseudoRandom/.make-targets
pseudoRandom/*.exe
pseudoRandom/*.o
pseudoRandomIota/.make-targets
pseudoRandomIota/*.exe
Name change from prand/Makefile to pseudoRandom/Makefile.
1
2
3
4
5
CFLAGS := -I/usr/local/include/urweb

all: prandFFI.o
	urweb prandFFI
	urweb prandUI


|
|
|
1
2
3
4
5
CFLAGS := -I/usr/local/include/urweb

all: random.o
	urweb random
	urweb randomUI
Name change from prand/prandFFI.c to pseudoRandom/random.c.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include <urweb.h>

struct timeval ti;

uw_Basis_int uw_PrandFFI_srand(uw_context ctx, uw_unit u) {
  gettimeofday(&ti, NULL);
  srand(ti.tv_usec);
  // in very rare cases, usleep is needed here
  usleep(10); }

uw_Basis_int uw_PrandFFI_rand(uw_context ctx, uw_unit u) { return rand(); }

uw_Basis_int uw_PrandFFI_prand(uw_context ctx, uw_unit u) {
  uw_PrandFFI_srand(ctx, u);
  return uw_PrandFFI_rand(ctx, u); }









|





|

|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include <urweb.h>

struct timeval ti;

uw_Basis_int uw_Random_updateSeed(uw_context ctx, uw_unit u) {
  gettimeofday(&ti, NULL);
  srand(ti.tv_usec);
  // in very rare cases, usleep is needed here
  usleep(10); }

uw_Basis_int uw_Random_generateWeak(uw_context ctx, uw_unit u) { return rand(); }

uw_Basis_int uw_Random_generateSafe(uw_context ctx, uw_unit u) {
  uw_Random_updateSeed(ctx, u);
  return uw_Random_generateWeak(ctx, u); }
Name change from prand/prandFFI.h to pseudoRandom/random.h.
1
2
3
4
5
#include <urweb.h>

uw_Basis_int uw_PrandFFI_rand(uw_context, uw_unit);
uw_Basis_unit uw_PrandFFI_srand(uw_context, uw_unit);
uw_Basis_int uw_PrandFFI_prand(uw_context, uw_unit);


|
|
|
1
2
3
4
5
#include <urweb.h>

uw_Basis_unit uw_Random_updateSeed(uw_context, uw_unit);
uw_Basis_int uw_Random_generateWeak(uw_context, uw_unit);
uw_Basis_int uw_Random_generateSafe(uw_context, uw_unit);
Name change from prand/prandFFI.urp to pseudoRandom/random.urp.
1
2
3
4
5
6
ffi prandFFI
include prandFFI.h
link prandFFI.o
effectful PrandFFI.rand
effectful PrandFFI.srand
effectful PrandFFI.prand
|
|
|
|
|
|
1
2
3
4
5
6
ffi random
include random.h
link random.o
effectful RandomFFI.updateSeed
effectful RandomFFI.generateWeak
effectful RandomFFI.generateSafe
Name change from prand/prandFFI.urs to pseudoRandom/random.urs.
1
2
3
4
5
6
7
8
val rand : unit -> transaction int
(* Generates a random number, without a seed  *)

val srand : unit -> transaction unit
(* Initiates an effect that updates the random seed *)

val prand : unit -> transaction int
(* Generates a pseudo-random number, based on the newest seed *)
|
|

|


|

1
2
3
4
5
6
7
8
val generateWeak : unit -> transaction int
(* Generates a random number, without a seed (very weak) *)

val updateSeed : unit -> transaction unit
(* Initiates an effect that updates the random seed *)

val generateSafe : unit -> transaction int
(* Generates a pseudo-random number, based on the newest seed *)
Name change from prand/prandUI.ur to pseudoRandom/randomUI.ur.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


(* fun randomNumber () : transaction (xml ([Html = ()]) ([]) ([])) = *)
fun randomNumber () : transaction page =
  r <- PrandFFI.prand ();
  return <xml><body>{[r]}</body></xml>

(* fun randomNumbers () : transaction (xml ([Html = ()]) ([]) ([])) = *)
fun randomNumbers () : transaction page =
  n1 <- PrandFFI.prand ();
  n2 <- PrandFFI.prand ();
  n3 <- PrandFFI.prand ();
  n4 <- PrandFFI.prand ();
  n5 <- PrandFFI.prand ();
  return <xml><body>
    {[n1]}<br />
    {[n2]}<br />
    {[n3]}<br />
    {[n4]}<br />
    {[n5]}<br /></body></xml>

>
>


|




|
|
|
|
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(* randomUI *)

(* fun randomNumber () : transaction (xml ([Html = ()]) ([]) ([])) = *)
fun randomNumber () : transaction page =
  r <- Random.generateSafe ();
  return <xml><body>{[r]}</body></xml>

(* fun randomNumbers () : transaction (xml ([Html = ()]) ([]) ([])) = *)
fun randomNumbers () : transaction page =
  n1 <- Random.generateSafe ();
  n2 <- Random.generateSafe ();
  n3 <- Random.generateSafe ();
  n4 <- Random.generateSafe ();
  n5 <- Random.generateSafe ();
  return <xml><body>
    {[n1]}<br />
    {[n2]}<br />
    {[n3]}<br />
    {[n4]}<br />
    {[n5]}<br /></body></xml>

Name change from prand/prandUI.urp to pseudoRandom/randomUI.urp.
1
2
3
library prandFFI

prandUI
|

|
1
2
3
library random

randomUI
Name change from prand/prandUI.urs to pseudoRandom/randomUI.urs.
Name change from randGen/randGen.ur to pseudoRandomIota/pseudoRandomIota.ur.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(* randGen *)

open Dataflow

val timeInterval = 1
(* use deciseconds, or 100 ms *)
val unitMultiplier = 100

(* fun prand (x: int) : transaction int = r <- PrandFFI.prand (); return r *)
fun prand (x: int) : transaction int = bind (PrandFFI.prand ()) return

(* advances sequence, then sleeps for a certain number of ms *)
fun randomPeriodic (ms: int) (runOn: signal production) (outCell: source int)
  : transaction unit =
  (* periodicT ms (fn x => r <- rpc (prand x); return r) runOn outCell *)
  periodicT ms (fn x => bind (rpc (prand x)) return) runOn outCell

|







|
|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(* pseudoRandomIota *)

open Dataflow

val timeInterval = 1
(* use deciseconds, or 100 ms *)
val unitMultiplier = 100

(* fun prand (x: int) : transaction int = r <- Random.generateSafe (); return r *)
fun prand (x: int) : transaction int = bind (Random.generateSafe ()) return

(* advances sequence, then sleeps for a certain number of ms *)
fun randomPeriodic (ms: int) (runOn: signal production) (outCell: source int)
  : transaction unit =
  (* periodicT ms (fn x => r <- rpc (prand x); return r) runOn outCell *)
  periodicT ms (fn x => bind (rpc (prand x)) return) runOn outCell

Name change from randGen/randGen.urp to pseudoRandomIota/pseudoRandomIota.urp.
1
2
3
4
5
6
7
8
library ../prand/prandFFI
serverOnly RandGen.randomPeriodic
serverOnly Dataflow.periodicT
serverOnly PrandFFI.prand
library ../iota/dataflow
safeGet RandGen/main

randGen
|
|

|

|

|
1
2
3
4
5
6
7
8
library ../pseudoRandom/random
serverOnly PseudoRandomIota.randomPeriodic
serverOnly Dataflow.periodicT
serverOnly Random.generateSafe
library ../iota/dataflow
safeGet PseudoRandomIota/main

pseudoRandomIota
Name change from randGen/randGen.urs to pseudoRandomIota/pseudoRandomIota.urs.