urbit/pkg/arvo/lib/strand.hoon

168 lines
4.0 KiB
Plaintext
Raw Normal View History

2019-09-26 07:12:58 +03:00
|%
2019-11-19 07:36:21 +03:00
+$ card card:agent:gall
2019-09-26 07:12:58 +03:00
+$ input
$% [%poke =cage]
[%sign =wire =sign-arvo]
2019-11-19 07:36:21 +03:00
[%agent =wire =sign:agent:gall]
2019-11-07 09:19:32 +03:00
[%watch =path]
2019-09-26 07:12:58 +03:00
==
+$ strand-input [=bowl in=(unit input)]
+$ tid @tatid
2019-11-10 10:27:07 +03:00
+$ bowl
$: our=ship
src=ship
tid=tid
mom=(unit tid)
2019-11-19 07:36:21 +03:00
wex=boat:gall
sup=bitt:gall
2019-11-10 10:27:07 +03:00
eny=@uvJ
now=@da
byk=beak
==
2019-09-26 07:12:58 +03:00
::
:: cards: cards to send immediately. These will go out even if a
:: later stage of the computation fails, so they shouldn't have
:: any semantic effect on the rest of the system.
:: Alternately, they may record an entry in contracts with
:: enough information to undo the effect if the computation
:: fails.
:: wait: don't move on, stay here. The next sign should come back
:: to this same callback.
:: skip: didn't expect this input; drop it down to be handled
:: elsewhere
:: cont: continue computation with new callback.
:: fail: abort computation; don't send effects
:: done: finish computation; send effects
::
++ strand-output-raw
2019-09-26 07:12:58 +03:00
|* a=mold
$~ [~ %done *a]
$: cards=(list card)
$= next
$% [%wait ~]
[%skip ~]
[%cont self=(strand-form-raw a)]
2019-09-26 07:12:58 +03:00
[%fail err=(pair term tang)]
[%done value=a]
==
==
::
++ strand-form-raw
2019-09-26 07:12:58 +03:00
|* a=mold
$-(strand-input (strand-output-raw a))
2019-09-26 07:12:58 +03:00
::
:: Abort strand computation with error message
2019-09-26 07:12:58 +03:00
::
++ strand-fail
2019-09-26 07:12:58 +03:00
|= err=(pair term tang)
|= strand-input
2019-09-27 20:40:22 +03:00
[~ %fail err]
2019-09-26 07:12:58 +03:00
::
:: Asynchronous transcaction monad.
::
:: Combo of four monads:
:: - Reader on input
:: - Writer on card
:: - Continuation
:: - Exception
::
++ strand
2019-09-26 07:12:58 +03:00
|* a=mold
|%
++ output (strand-output-raw a)
2019-09-26 07:12:58 +03:00
::
:: Type of an strand computation.
2019-09-26 07:12:58 +03:00
::
++ form (strand-form-raw a)
2019-09-26 07:12:58 +03:00
::
:: Monadic pure. Identity computation for bind.
::
++ pure
|= arg=a
^- form
|= strand-input
2019-09-26 07:12:58 +03:00
[~ %done arg]
::
:: Monadic bind. Combines two computations, associatively.
::
++ bind
|* b=mold
|= [m-b=(strand-form-raw b) fun=$-(b form)]
2019-09-26 07:12:58 +03:00
^- form
|= input=strand-input
=/ b-res=(strand-output-raw b)
2019-09-26 07:12:58 +03:00
(m-b input)
^- output
:- cards.b-res
?- -.next.b-res
%wait [%wait ~]
2019-09-26 08:43:14 +03:00
%skip [%skip ~]
2019-09-26 07:12:58 +03:00
%cont [%cont ..$(m-b self.next.b-res)]
%fail [%fail err.next.b-res]
%done [%cont (fun value.next.b-res)]
==
::
:: The strand monad must be evaluted in a particular way to maintain
2019-09-26 07:12:58 +03:00
:: its monadic character. +take:eval implements this.
::
++ eval
|%
:: Indelible state of a strand
2019-09-26 07:12:58 +03:00
::
+$ eval-form
$: =form
==
::
:: Convert initial form to eval-form
::
++ from-form
|= =form
^- eval-form
form
::
:: The cases of results of +take
::
+$ eval-result
$% [%next ~]
[%fail err=(pair term tang)]
[%done value=a]
==
::
:: Take a new sign and run the strand against it
2019-09-26 07:12:58 +03:00
::
++ take
:: cards: accumulate throughout recursion the cards to be
:: produced now
=| cards=(list card)
|= [=eval-form =strand-input]
2019-09-26 07:12:58 +03:00
^- [[(list card) =eval-result] _eval-form]
=* take-loop $
:: run the strand callback
2019-09-26 07:12:58 +03:00
::
=/ =output (form.eval-form strand-input)
2019-09-26 07:12:58 +03:00
:: add cards to cards
::
=. cards
%+ welp
cards
:: XX add tag to wires?
cards.output
:: case-wise handle next steps
::
?- -.next.output
%wait [[cards %next ~] eval-form]
2019-11-22 23:46:30 +03:00
%skip [[cards %next ~] eval-form]
2019-09-26 07:12:58 +03:00
%fail [[cards %fail err.next.output] eval-form]
%done [[cards %done value.next.output] eval-form]
%cont
:: recurse to run continuation with initialization input
::
%_ take-loop
form.eval-form self.next.output
strand-input [bowl.strand-input ~]
2019-09-26 07:12:58 +03:00
==
==
--
--
--