module Data.Crjdt
(
iter
, next
, key
, doc
, var
, Command(..)
, yield
, keys
, values
, insert
, delete
, bind
, (-<)
, assign
, (=:)
, string
, emptyMap
, emptyList
, Eval.eval
, Eval.execute
, Void
, (.>)
, (&)
, sync
, module Core
) where
import Data.Text as T
import Data.Set (Set)
import Data.Void
import Data.Function
import Data.Bifunctor (bimap)
import Control.Monad.Free (liftF)
import Control.Applicative (liftA2)
import Data.Crjdt.Context as Core
import Data.Crjdt.Types as Core
import qualified Data.Crjdt.Eval as Eval (execute, eval)
import Data.Crjdt.Eval as Core hiding (execute, eval)
import Data.Crjdt.Internal
(.>) :: b -> (b -> a) -> a
(.>) = (&)
infixl 6 .>
emptyMap :: Val
emptyMap = EmptyObject
emptyList :: Val
emptyList = EmptyArray
yield :: Command ()
yield = liftF (Yield ())
string :: Text -> Val
string = StringLit
iter :: Expr -> Expr
iter = Iter
next :: Expr -> Expr
next = Next
key :: Key Void -> Expr -> Expr
key = flip GetKey
doc :: Expr
doc = Doc
var :: Text -> Expr
var = Var . Variable
insert :: Val -> Expr -> Command ()
insert v e = liftF (InsertAfter e v ())
delete :: Expr -> Command ()
delete e = liftF (Delete e ())
keys :: Expr -> Command (Set (Key Void))
keys e = liftF (Keys e id)
values :: Expr -> Command [Val]
values e = liftF (Values e id)
assign, (=:) :: Expr -> Val -> Command ()
assign e v = liftF (Assign e v ())
(=:) = assign
infixr 5 =:
bind, (-<) :: Text -> Expr -> Command Expr
bind t e = liftF (Let t e id)
(-<) = bind
sync :: (ReplicaId, Command a) -> (ReplicaId, Command b) -> Either EvalError (Eval a, Eval b)
sync (rid1, first) (rid2, second) =
let (rFirst, sFirst) = run rid1 (Eval.execute first)
(rSecond, sSecond) = run rid2 (Eval.execute second)
synced which replica = do
Eval.execute which
addReceivedOps (queue replica)
Eval.execute yield
in case (liftA2 (,) rFirst rSecond) of
Right (a, b) -> pure $ bimap (a <$) (b <$) (synced first sSecond, synced second sFirst)
Left ex -> Left ex