在 erlang 中表达动作的时间逻辑。有什么自然的方法吗?

发布于 2024-10-18 01:06:07 字数 1721 浏览 5 评论 0原文

我想翻译 TLA 中指定的一些操作 Erlang。你能想到在 Erlang 中直接执行此操作的任何自然方法或任何可用的框架吗?简而言之(非常小的一个),TLA 动作是变量的条件,其中一些是预先准备的,这意味着它们代表下一个状态中变量的值。例如:

Action(x,y,z) ->
    and PredicateA(x),
    and or PredicateB(y)
        or PredicateC(z)
    and x' = x+1

此操作意味着,只要系统状态满足变量 xPredicateA 为 true,并且变量 PredicateB 为 true, yPredicateC 对于 z 为 true,那么系统可能会更改其状态,以便除了 x 之外一切都保持不变code> 更改为当前值加 1。

在 Erlang 中表达这一点需要大量的管道,至少以我发现的方式。例如,通过在触发条件之前评估条件的循环,例如:

what_to_do(State,NewInfo) ->
    PA = IsPredicateA(State,NewInfo),
    PB = IsPredicateB(State,NewInfo),
    PC = IsPredicateC(State,NewInfo),
    [{can_do_Action1, PA and (PB or PC}, %this is the action specified above.
     {can_do_Action2, PA and PC},        %this is some other action
     {can_do_Action3, true}]             %this is some action that may be executed at any time.

 loop(State) ->
     NewInfo = get_new_info(),
     CanDo = what_to_do(State,NewInfo),
     RandomAction = rand_action(CanDo),

     case RandDomAction of
          can_do_Action1 -> NewState = Action(x,y,z);
          can_do_Action2 -> NewState = Action2(State);
          can_do_Action3 -> NewState = Action3(State)
     end,
     NewestState = clean_up_old_info(NewState,NewInfo),
     loop(NewestState).

我正在考虑编写一个框架来隐藏此管道,将消息传递合并到 get_new_info() 函数中,并希望仍然能够实现符合 OTP 标准。如果您知道任何已经做到这一点的框架,或者您能想到一种简单的方法来实现这一点,我将很高兴听到它。

I would like to translate some actions specified in TLA in Erlang. Can you think of any natural way of doing this directly in Erlang or of any framework available for such? In a nutshell (a very small one), TLA actions are conditions on variables, some of which are primed, meaning that they represent the values of the variables in the next state. For example:

Action(x,y,z) ->
    and PredicateA(x),
    and or PredicateB(y)
        or PredicateC(z)
    and x' = x+1

This action means that, whenever the state of the system is such that PredicateA is true for variable x and either PredicateB is true for y or PredicateC is true for z, then the system may change it's state so that everything remains the same except that x changes to the current value plus 1.

Expressing that in Erlang requires a lot of plumbing, at least in the way I've found. For example, by having a loop that evaluates conditions before triggering them, like:

what_to_do(State,NewInfo) ->
    PA = IsPredicateA(State,NewInfo),
    PB = IsPredicateB(State,NewInfo),
    PC = IsPredicateC(State,NewInfo),
    [{can_do_Action1, PA and (PB or PC}, %this is the action specified above.
     {can_do_Action2, PA and PC},        %this is some other action
     {can_do_Action3, true}]             %this is some action that may be executed at any time.

 loop(State) ->
     NewInfo = get_new_info(),
     CanDo = what_to_do(State,NewInfo),
     RandomAction = rand_action(CanDo),

     case RandDomAction of
          can_do_Action1 -> NewState = Action(x,y,z);
          can_do_Action2 -> NewState = Action2(State);
          can_do_Action3 -> NewState = Action3(State)
     end,
     NewestState = clean_up_old_info(NewState,NewInfo),
     loop(NewestState).

I am thinking writing a framework to hide this plumbing, incorporating message passing within the get_new_info() function and, hopefully, still making it OTP compliant. If you know of any framework that already does that or if you can think of a simple way of implementing this, I would appreciate to hear about it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2024-10-25 01:06:07

我相信 gen_fsm(3) 行为可能会使你的生活稍微轻松一些。

来自有限状态机的 FSM,而不是飞行面条怪物,尽管后者也可以提供帮助。

I believe gen_fsm(3) behaviour could probably make your life slightly easier.

FSM from Finite State Machine, not Flying Spaghetti Monster, though the latter could help, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文