urbit/base/lib/sole/core.hook

146 lines
4.1 KiB
Plaintext
Raw Normal View History

2015-02-09 00:22:33 +03:00
::
2015-03-28 13:57:02 +03:00
:::: /hook/core/sole/lib
2015-02-09 00:22:33 +03:00
::
:: This file is in the public domain.
::
/? 310
2015-03-28 13:57:02 +03:00
/- *sole
2015-02-09 09:25:00 +03:00
!:
2015-02-09 00:22:33 +03:00
::::
::
|%
++ cs :: shared-state engine
2015-03-28 13:57:02 +03:00
|_ sole-share
2015-02-09 00:22:33 +03:00
++ abet +<
++ apply
2015-03-28 13:57:02 +03:00
|= ted=sole-edit
2015-02-09 00:22:33 +03:00
^+ +>
?- -.ted
2015-02-12 05:57:33 +03:00
%del +>.$(buf (weld (scag p.ted buf) (slag +(p.ted) buf)))
%ins +>.$(buf (weld (scag p.ted buf) `_buf`[q.ted (slag p.ted buf)]))
2015-02-09 00:22:33 +03:00
%mor |- ^+ +>.^$
?~ p.ted
+>.^$
2015-02-09 09:25:00 +03:00
$(p.ted t.p.ted, +>.^$ ^$(ted i.p.ted))
2015-02-09 00:22:33 +03:00
%nop +>.$
2015-02-12 05:57:33 +03:00
%set +>.$(buf p.ted)
2015-02-09 00:22:33 +03:00
==
::
::::
:: ++transmute: symmetric operational transformation.
::
2015-03-28 13:57:02 +03:00
:: for any sole state +>, obeys
2015-02-09 00:22:33 +03:00
::
:: =+ [x=(transmute a b) y=(transmute b a)]
2015-04-14 03:20:54 +03:00
:: .= (apply:(apply a) x)
:: (apply:(apply b) y)
2015-02-09 00:22:33 +03:00
::
++ transmute :: dex as after sin
2015-03-28 13:57:02 +03:00
|= [sin=sole-edit dex=sole-edit]
^- sole-edit
2015-02-09 00:22:33 +03:00
?: ?=(%mor -.sin)
2015-03-28 13:57:02 +03:00
|- ^- sole-edit
2015-02-09 00:22:33 +03:00
?~ p.sin dex
$(p.sin t.p.sin, dex ^$(sin i.p.sin))
::
?: ?=(%mor -.dex)
:- %mor
2015-03-28 13:57:02 +03:00
|- ^- (list sole-edit)
2015-02-09 00:22:33 +03:00
?~ p.dex ~
[^$(dex i.p.dex) $(p.dex t.p.dex)]
::
?: |(?=(%nop -.sin) ?=(%nop -.dex)) dex
?: ?=(%set -.sin) [%nop ~]
?: ?=(%set -.dex) dex
::
?- -.sin
%del
?- -.dex
%del ?: =(p.sin p.dex) [%nop ~]
?:((lth p.sin p.dex) dex(p (dec p.dex)) dex)
%ins ?:((lte p.sin p.dex) dex(p (dec p.dex)) dex)
==
::
%ins
?- -.dex
2015-02-09 09:25:00 +03:00
%del ?:((lte p.sin p.dex) dex(p +(p.dex)) dex)
2015-02-09 00:22:33 +03:00
%ins ?: =(p.sin p.dex)
2015-04-15 05:01:26 +03:00
?:((lth q.sin q.dex) dex dex(p +(p.dex)))
2015-02-09 09:25:00 +03:00
?:((lte p.sin p.dex) dex(p +(p.dex)) dex)
2015-02-09 00:22:33 +03:00
==
==
::
++ commit :: local change
2015-03-28 13:57:02 +03:00
|= ted=sole-edit
^- sole-share
2015-03-23 23:56:00 +03:00
abet:(apply(own.ven +(own.ven), leg [ted leg]) ted)
2015-02-09 00:22:33 +03:00
::
::::
:: ++inverse: inverse of change in context.
::
2015-03-28 13:57:02 +03:00
:: for any sole state +>, obeys
::
:: =(+> (apply:(apply a) (inverse a)))
::
++ inverse :: relative inverse
2015-03-28 13:57:02 +03:00
|= ted=sole-edit
^- sole-edit
2015-04-15 05:01:26 +03:00
:: =. ted ?.(?=([%mor * ~] ted) ted i.p.ted) :: XX why?
~| [ted abet]
?- -.ted
%del [%ins p.ted (snag p.ted buf)]
%ins [%del p.ted]
%mor :- %mor
%- flop
2015-03-28 13:57:02 +03:00
|- ^- (list sole-edit)
?~ p.ted ~
:- ^$(ted i.p.ted)
$(p.ted t.p.ted, +>.^$ (apply i.p.ted))
%nop [%nop ~]
%set [%set buf]
==
::
++ receive :: naturalize event
2015-03-28 13:57:02 +03:00
|= sole-change
^- [sole-edit sole-share]
2015-04-14 23:38:37 +03:00
~| [ler ven]
?> &(=(his.ler his.ven) (lte own.ler own.ven))
2015-04-10 09:57:04 +03:00
?> |(!=(own.ler own.ven) =(haw (sham buf)) =(haw 0)) :: trust the clock
=. leg (scag (sub own.ven own.ler) leg)
2015-03-23 23:56:00 +03:00
:: ~? !=(own.ler own.ven) [%miss-leg leg]
2015-04-15 05:01:26 +03:00
=+ dat=(transmute [%mor (flop leg)] ted)
2015-03-23 23:56:00 +03:00
:: ~? !=(~ leg) [%transmute from/ted to/dat ~]
2015-04-15 05:01:26 +03:00
:: =- ~& (tufa buf) -
[dat abet:(apply(his.ven +(his.ven)) dat)]
::
++ remit :: conditional accept
2015-03-28 13:57:02 +03:00
|= [cal=sole-change ask=$+((list ,@c) ?)]
^- [(unit sole-change) sole-share]
2015-02-26 23:06:23 +03:00
=+ old=buf
=^ dat +>+<.$ (receive cal)
?: (ask buf)
[~ +>+<.$]
2015-02-26 23:06:23 +03:00
=^ lic +>+<.$ (transmit (inverse(buf old) dat))
[`lic +>+<.$]
2015-03-19 21:53:05 +03:00
::
++ transmit :: outgoing change
2015-03-28 13:57:02 +03:00
|= ted=sole-edit
^- [sole-change sole-share]
2015-04-15 05:01:26 +03:00
:: =- ~& (tufa buf) -
2015-03-19 21:53:05 +03:00
[[[his.ven own.ven] (sham buf) ted] (commit ted)]
::
++ transceive :: receive and invert
2015-03-28 13:57:02 +03:00
|= sole-change
^- [sole-edit sole-share]
2015-03-19 21:53:05 +03:00
=+ old=buf
=^ dat +>+<.$ (receive +<.$)
[(inverse(buf old) dat) +>+<.$]
::
++ transpose :: adjust position
|= pos=@ud
=+ dat=(transmute [%mor leg] [%ins pos `@c`0])
?> ?=(%ins -.dat)
p.dat
2015-02-09 00:22:33 +03:00
--
--