Update demos and adds <|
This commit is contained in:
20
demos/interactionTrees/environment.tri
Normal file
20
demos/interactionTrees/environment.tri
Normal file
@@ -0,0 +1,20 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- Environment effects: ask and local.
|
||||
-- ask reads the current environment value.
|
||||
-- local f action runs action with the env transformed by f.
|
||||
--
|
||||
-- The CLI starts with an empty (Leaf) environment. This demo uses
|
||||
-- local to inject a real string so that ask returns something readable.
|
||||
|
||||
main = io <|
|
||||
(bind
|
||||
local (_ : "sandbox")
|
||||
(bind ask (env :
|
||||
bind (putStrLn (append "working in env: " env)) (_ :
|
||||
pure "inside-done"))))
|
||||
(outside :
|
||||
bind (putStrLn (append "local returned: " outside)) (_ :
|
||||
pure t))
|
||||
18
demos/interactionTrees/forkAwait.tri
Normal file
18
demos/interactionTrees/forkAwait.tri
Normal file
@@ -0,0 +1,18 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- Basic fork and await.
|
||||
-- fork spawns a concurrent task and returns a handle.
|
||||
-- await blocks until the task completes and returns its value.
|
||||
|
||||
worker = (msg :
|
||||
bind (putStrLn (append "working: " msg)) (_ :
|
||||
pure (append msg "-result")))
|
||||
|
||||
main = io <|
|
||||
(bind (fork (worker "job1")) (h1 :
|
||||
bind (fork (worker "job2")) (h2 :
|
||||
bind (await h1) (r1 :
|
||||
bind (await h2) (r2 :
|
||||
putStrLn (append "Got " (append r1 (append " and " r2))))))))
|
||||
10
demos/interactionTrees/greet.tri
Normal file
10
demos/interactionTrees/greet.tri
Normal file
@@ -0,0 +1,10 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- Greet and return a pure value.
|
||||
-- putStrLn writes to stdout; pure lifts "done" into IO.
|
||||
|
||||
main = io (bind
|
||||
(putStrLn (append "Hello, " "tricu"))
|
||||
(_ : pure ""))
|
||||
16
demos/interactionTrees/safeRead.tri
Normal file
16
demos/interactionTrees/safeRead.tri
Normal file
@@ -0,0 +1,16 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- readFile returns a Result. matchResult branches on ok / err.
|
||||
-- Run with --allow-read PATH or --unsafe-io.
|
||||
|
||||
safeRead = (path :
|
||||
bind (readFile path)
|
||||
(result :
|
||||
matchResult
|
||||
(err rest : pure "ERROR: Unable to read file")
|
||||
(contents rest : pure contents)
|
||||
result))
|
||||
|
||||
main = io (safeRead "demos/interactionTrees/greet.tri")
|
||||
23
demos/interactionTrees/shout.tri
Normal file
23
demos/interactionTrees/shout.tri
Normal file
@@ -0,0 +1,23 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- Transform an IO result.
|
||||
-- mapIO applies a pure function to the value produced by an action.
|
||||
-- Run with --allow-read PATH or --unsafe-io.
|
||||
|
||||
safeRead = (path :
|
||||
bind (readFile path)
|
||||
(result :
|
||||
matchResult
|
||||
(err rest : pure "missing")
|
||||
(contents rest : pure contents)
|
||||
result))
|
||||
|
||||
shout = (path :
|
||||
mapIO (safeRead path)
|
||||
(text : append text "!!!"))
|
||||
|
||||
main = io (bind
|
||||
(shout "demos/interactionTrees/greet.tri")
|
||||
(text : putStrLn text))
|
||||
22
demos/interactionTrees/state.tri
Normal file
22
demos/interactionTrees/state.tri
Normal file
@@ -0,0 +1,22 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- Mutable state via get and put.
|
||||
-- get reads the current state.
|
||||
-- put replaces the state.
|
||||
--
|
||||
-- The CLI starts with an empty (Leaf) state. This demo puts
|
||||
-- readable strings and prints them back out.
|
||||
|
||||
main = io <|
|
||||
bind (put "idle") (_ :
|
||||
bind get (s1 :
|
||||
bind (putStrLn (append "state: " s1)) (_ :
|
||||
bind (put "running") (_ :
|
||||
bind get (s2 :
|
||||
bind (putStrLn (append "state: " s2)) (_ :
|
||||
bind (put "done") (_ :
|
||||
bind get (s3 :
|
||||
bind (putStrLn (append "state: " s3)) (_ :
|
||||
pure t)))))))))
|
||||
20
demos/interactionTrees/writeThenRead.tri
Normal file
20
demos/interactionTrees/writeThenRead.tri
Normal file
@@ -0,0 +1,20 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- Write a file, then read it back.
|
||||
-- thenIO discards the writeFile Result and continues.
|
||||
-- Run with --unsafe-io (needs both read and write permissions).
|
||||
|
||||
writeThenRead = (path text :
|
||||
thenIO
|
||||
(writeFile path text)
|
||||
(readFile path))
|
||||
|
||||
main = io <|
|
||||
(bind (writeThenRead "/tmp/tricu-demo.txt" "hello from tricu")
|
||||
(result :
|
||||
matchResult
|
||||
(err rest : putStrLn "error")
|
||||
(contents rest : putStrLn contents)
|
||||
result))
|
||||
33
demos/interactionTrees/yield.tri
Normal file
33
demos/interactionTrees/yield.tri
Normal file
@@ -0,0 +1,33 @@
|
||||
!import "../../lib/base.tri" !Local
|
||||
!import "../../lib/list.tri" !Local
|
||||
!import "../../lib/io.tri" !Local
|
||||
|
||||
-- Cooperative scheduling with yield.
|
||||
-- yield returns control to the scheduler so other tasks can run.
|
||||
--
|
||||
-- Two tasks print alternately because each yields after every line.
|
||||
|
||||
--chatter = (name n :
|
||||
-- bind (putStrLn (append name " says 1")) (_ :
|
||||
-- bind yield (_ :
|
||||
-- bind (putStrLn (append name " says 2")) (_ :
|
||||
-- bind yield (_ :
|
||||
-- bind (putStrLn (append name " says 3")) (_ :
|
||||
-- pure n))))))
|
||||
|
||||
chatter = name n : bind <|
|
||||
putStrLn (append name " says 1") (_ :
|
||||
bind yield (_ :
|
||||
bind (putStrLn (append name " says 2")) (_ :
|
||||
bind yield (_ :
|
||||
bind (putStrLn (append name " says 3")) (_ :
|
||||
pure n)))))
|
||||
|
||||
|
||||
main = io <|
|
||||
bind (fork (chatter "A" "doneA")) (ha :
|
||||
bind (fork (chatter "B" "doneB")) (hb :
|
||||
bind yield (_ :
|
||||
bind (await ha) (a :
|
||||
bind (await hb) (b :
|
||||
putStrLn (append "Finished: " (append a (append " " b))))))))
|
||||
Reference in New Issue
Block a user