Ur/Web Examples
Check-in [56f4b59eef]
Not logged in

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

Overview
Comment:- attempt to provide server-side versions of periodic/periodicT, which were unsuccessful since sources and signals aren't allowed server side (eg. the error "Input to exported function 'PseudoRandomIota/randomPeriodic' involves one or more types that are disallowed for page handler inputs: Basis.signal, Basis.source")
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:56f4b59eef79febe22237514a06ffac8e9d2d1e8
User & Date: beyert 2015-03-01 23:07:32
Context
2015-03-01
23:54
- slight adjustment to required libraries check-in: d27e9105c6 user: beyert tags: trunk
23:07
- attempt to provide server-side versions of periodic/periodicT, which were unsuccessful since sources and signals aren't allowed server side (eg. the error "Input to exported function 'PseudoRandomIota/randomPeriodic' involves one or more types that are disallowed for page handler inputs: Basis.signal, Basis.source") check-in: 56f4b59eef user: beyert tags: trunk
2015-02-23
05:17
- in jsMove switch back to signal-based instructions check-in: d033bf1632 user: beyert tags: trunk
Changes
Hide Diffs Unified Diffs 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

pseudoRandom/.make-targets
pseudoRandom/*.exe
pseudoRandom/*.o
pseudoRandomIota/.make-targets
pseudoRandomIota/*.exe





>





1
2
3
4
5
6
7
8
9
10
11
.fossilmsg
jsMove/.make-targets
jsMove/*.exe
iota/.make-targets
iota/*.exe
iota/*.o
pseudoRandom/.make-targets
pseudoRandom/*.exe
pseudoRandom/*.o
pseudoRandomIota/.make-targets
pseudoRandomIota/*.exe

Added iota/Makefile.









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

all:	sleep.o
	urweb sleep

Changes to iota/dataflow.ur.

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
















(* loosely modeled after the periodic combinator in Netwire *)
fun periodic [a] (ms: int) (f: a -> a) (runOn: signal production)
  (outCell: source a) : transaction unit =
  runOn' <- current runOn;
  (case runOn' of Inhibit => return ()
                | Produce => x <- get outCell; set outCell (f x));
  sleep ms; periodic ms f runOn outCell

fun periodicT [a] (ms: int) (f: a -> transaction a)
  (runOn: signal production) (outCell: source a) : transaction unit =
  runOn' <- current runOn;
  (case runOn' of Inhibit => return ()
                | Produce => x <- get outCell; t <- f x; set outCell t);
  sleep ms; periodicT ms f runOn outCell






















|






|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

(* loosely modeled after the periodic combinator in Netwire *)
fun periodic [a] (ms: int) (f: a -> a) (runOn: signal production)
  (outCell: source a) : transaction unit =
  runOn' <- current runOn;
  (case runOn' of Inhibit => return ()
                | Produce => x <- get outCell; set outCell (f x));
  Sleep.msSleep ms; periodic ms f runOn outCell

fun periodicT [a] (ms: int) (f: a -> transaction a)
  (runOn: signal production) (outCell: source a) : transaction unit =
  runOn' <- current runOn;
  (case runOn' of Inhibit => return ()
                | Produce => x <- get outCell; t <- f x; set outCell t);
  Sleep.msSleep ms; periodicT ms f runOn outCell

(* loosely modeled after the periodic combinator in Netwire *)
fun periodic' [a] (ms: int) (f: a -> a) (runOn: signal production)
  (outCell: source a) : transaction unit =
  runOn' <- current runOn;
  (case runOn' of Inhibit => return ()
                | Produce => x <- get outCell; set outCell (f x));
  Basis.sleep ms; periodic' ms f runOn outCell

fun periodicT' [a] (ms: int) (f: a -> transaction a)
  (runOn: signal production) (outCell: source a) : transaction unit =
  runOn' <- current runOn;
  (case runOn' of Inhibit => return ()
                | Produce => x <- get outCell; t <- f x; set outCell t);
  sleep ms; periodicT' ms f runOn outCell

Changes to iota/dataflow.urp.



1


dataflow
>
>

1
2
3
library ./sleep

dataflow

Changes to iota/dataflow.urs.

3
4
5
6
7
8
9




val eq_production : eq production
val show_production : show production
val invert : production -> production
val periodic : t ::: Type -> int -> (t -> t) -> signal production -> source t
                          -> transaction unit
val periodicT : t ::: Type -> int -> (t -> transaction t) -> signal production
                           -> source t -> transaction unit











>
>
>
>
3
4
5
6
7
8
9
10
11
12
13
val eq_production : eq production
val show_production : show production
val invert : production -> production
val periodic : t ::: Type -> int -> (t -> t) -> signal production -> source t
                          -> transaction unit
val periodicT : t ::: Type -> int -> (t -> transaction t) -> signal production
                           -> source t -> transaction unit
val periodic' : t ::: Type -> int -> (t -> t) -> signal production -> source t
                           -> transaction unit
val periodicT' : t ::: Type -> int -> (t -> transaction t) -> signal production
                            -> source t -> transaction unit

Changes to iota/iota.ur.

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  let fun f (xs: int) : int = case (compare from to, compare xs to) of
        (Eq, _) => xs
      | (_, Eq) => from
      | (Lt, _) => xs + step
      | (Gt, _) => xs - step
  in
    case (compare from to, compare ms 0) of
      (Lt, Gt) => periodic ms f runOn outCell
    | (Gt, Gt) => periodic ms f runOn outCell
     (* infinite loop is OK here, because this runs over time *)
    | (Eq, Gt) => periodic ms f runOn outCell
    |        _ => error <xml>invalid sequence defined</xml> end

fun main () =
  result <- source (0: int);
  runOn <- source (Produce: production);

  return <xml><head>







|
|

|







16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  let fun f (xs: int) : int = case (compare from to, compare xs to) of
        (Eq, _) => xs
      | (_, Eq) => from
      | (Lt, _) => xs + step
      | (Gt, _) => xs - step
  in
    case (compare from to, compare ms 0) of
      (Lt, Gt) => periodic' ms f runOn outCell
    | (Gt, Gt) => periodic' ms f runOn outCell
     (* infinite loop is OK here, because this runs over time *)
    | (Eq, Gt) => periodic' ms f runOn outCell
    |        _ => error <xml>invalid sequence defined</xml> end

fun main () =
  result <- source (0: int);
  runOn <- source (Produce: production);

  return <xml><head>

Changes to iota/iota.urp.




1
2
3



compare
dataflow
iota
>
>
>



1
2
3
4
5
6
library sleep
safeGet Iota/main

compare
dataflow
iota

Added iota/sleep.c.

















>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
#include <unistd.h>

#include <urweb.h>

uw_Basis_unit uw_Sleep_uSleep(uw_context ctx, uw_Basis_int u) { usleep(u); }

uw_Basis_unit uw_Sleep_msSleep(uw_context ctx, uw_Basis_int ms) {
  usleep(ms * 1000); }

Added iota/sleep.h.









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

uw_Basis_unit uw_Sleep_uSleep(uw_context, uw_Basis_int);
uw_Basis_unit uw_Sleep_msSleep(uw_context, uw_Basis_int);

Added iota/sleep.urp.











>
>
>
>
>
1
2
3
4
5
ffi sleep
include sleep.h
link sleep.o
effectful Sleep.uSleep
effectful Sleep.msSleep

Added iota/sleep.urs.











>
>
>
>
>
1
2
3
4
5
val uSleep : int -> transaction unit
(* Sleep on server-side for n microseconds *)

val msSleep : int -> transaction unit
(* Sleep on server-side for n milliseconds *)

Changes to pseudoRandomIota/pseudoRandomIota.ur.

7
8
9
10
11
12
13

14
15
16
17
18
19
20
21
22
23
24


25
26
27
28
29
30
31
32
33
34
35
36
val unitMultiplier = 100

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 => bind (rpc (prand x)) return) runOn outCell

fun main () : transaction page =
  result <- source (0: int);
  runOn <- source (Produce: production);

  return <xml><head>
    <title>Generate random numbers, every
      {[show (timeInterval * unitMultiplier)]} ms:</title>
    </head>
    <body onload={let val ms = timeInterval * unitMultiplier


                  in spawn (randomPeriodic ms (signal runOn) result) end}>
      <p>status: <dyn signal={runOn' <- signal runOn;
                              return <xml>{[show runOn']}</xml>}/></p>
      <dyn signal={cur <- signal result; return <xml>{[show cur]}</xml>}/>
      <p><button value="Pause Random Number Generation"
        onclick={fn _ => set runOn Inhibit}/></p>
      <p><button value="Resume Random Number Generation"
        onclick={fn _ => set runOn Produce}/></p>
      <p><button value="Toggle Pause"
        onclick={fn _ => runOn' <- get runOn; set runOn (invert runOn')}/></p>
    </body>
    </xml>







>
|










>
>












7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
val unitMultiplier = 100

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 => bind (prand x) return) runOn outCell *)
  periodicT' ms (fn x => bind (rpc (prand x)) return) runOn outCell

fun main () : transaction page =
  result <- source (0: int);
  runOn <- source (Produce: production);

  return <xml><head>
    <title>Generate random numbers, every
      {[show (timeInterval * unitMultiplier)]} ms:</title>
    </head>
    <body onload={let val ms = timeInterval * unitMultiplier
                  (* in spawn (rpc (randomPeriodic ms (signal runOn)
                                                   result)) end}> *)
                  in spawn (randomPeriodic ms (signal runOn) result) end}>
      <p>status: <dyn signal={runOn' <- signal runOn;
                              return <xml>{[show runOn']}</xml>}/></p>
      <dyn signal={cur <- signal result; return <xml>{[show cur]}</xml>}/>
      <p><button value="Pause Random Number Generation"
        onclick={fn _ => set runOn Inhibit}/></p>
      <p><button value="Resume Random Number Generation"
        onclick={fn _ => set runOn Produce}/></p>
      <p><button value="Toggle Pause"
        onclick={fn _ => runOn' <- get runOn; set runOn (invert runOn')}/></p>
    </body>
    </xml>

Changes to pseudoRandomIota/pseudoRandomIota.urp.

1

2
3
4
5
library ../pseudoRandom/random

library ../iota/dataflow
safeGet PseudoRandomIota/main

pseudoRandomIota

>




1
2
3
4
5
6
library ../pseudoRandom/random
library ../iota/sleep
library ../iota/dataflow
safeGet PseudoRandomIota/main

pseudoRandomIota