如何编写带有“或”条件的let表达式

时间:2019-12-10 09:31:22

标签: alloy

在书上实现问题A.3.6时,我需要表达以下条件:

  

戴上手套后,因为手套戴在手上的东西会发生变化,而另一只手套戴上的东西则保持不变。

由于手套和手套的结构相同,我如何整齐地表达条件而不指定手套在右手和手套在左手的条件。

我的代码在这里:https://github.com/huanhulan/alloy-exercises/blob/master/book/appendixA/gloves.als#L137

2 个答案:

答案 0 :(得分:0)

一种选择是将公式写在谓词中,然后传入实例化之间不同的部分(在这种情况下,leftHand和rightHand绑定到该关系)。

答案 1 :(得分:0)

也许不能直接回答您的问题,但是我无法抗拒建立一个比您似乎正在研究的模型更简单的模型。我通常不使用Time模型,因为它几乎使任何问题都难以理解。如果您选择这条路线,则可能要结帐Electrum。这是一个叉子,可有效隐藏Time原子,并具有用于约束过去和未来的漂亮关键字。

module austere_surgery

open util/relation
open util/ordering[Operation]

abstract sig Surface {}

abstract sig Human extends Surface {}
one sig Surgeon extends Human {}
sig Patient extends Human {}

sig Glove {
    inside   : disj Inside,
    outside  : disj Outside
} 
sig Inside, Outside extends Surface {}

sig Operation {
    patient  : disj Patient,
    -- surfaces is a path from surgeon -> glove* -> patient
    surfaces : Surface -> lone Surface,
    gloves   : set Glove,
    contaminated : Human-> Surface
} {
    -- constrain to be a proper path (could be improved)
    dom[surfaces] = Surgeon + (gloves.(inside+outside)-ran[surfaces])
    ran[surfaces] = patient + (gloves.(inside+outside)-dom[surfaces])
    all g : gloves | g.inside in dom[surfaces] iff g.outside in ran[surfaces]

    -- and no patient must come into contact with the blood of another patient. 
    surfaces.patient not in ran[contaminated - patient->Surface]

    -- the surgeon must not come into contact with the blood of any patient,
    Surgeon -> patient not in surfaces 
    Surgeon.surfaces not in Patient.contaminated
}

pred surgery {

    Surface = Glove.inside + Glove.outside + Human

    no first.contaminated

    all o' : Operation-first, o : o'.prev {

        o'.contaminated = o.contaminated 
            + o.touches[o.patient] 
            + o.touches[Surgeon]
            + o.touches[ran[o.contaminated-Surgeon->Surface]]   
    }
}

fun Operation.touches[ dirty : set Surface ] : (Patient+Surgeon)-> Surface {
    { from : dirty, to : Surface | from->to in this.surfaces or to->from in this.surfaces }
}


-- A surgeon must operate on three patients, but xe has only two pairs of gloves.
run surgery for 10 but exactly 3 Patient, exactly 3 Operation, exactly 2 Glove

解决方案:

┌──────────────┬─────────────────┬────────┬──────┬─────────────────┐
│this/Operation│surfaces         │patient │gloves│contaminated     │
├──────────────┼────────┬────────┼────────┼──────┼─────────────────┤
│Operation⁰    │Inside⁰ │Inside¹ │Patient²│Glove⁰│                 │
│              ├────────┼────────┼────────┼──────┤                 │
│              │Outside¹│Patient²│        │Glove¹│                 │
│              ├────────┼────────┤        ├──────┤                 │
│              │Surgeon⁰│Outside⁰│        │      │                 │
├──────────────┼────────┼────────┼────────┼──────┼────────┬────────┤
│Operation¹    │Inside⁰ │Patient¹│Patient¹│Glove¹│Patient²│Outside¹│
│              ├────────┼────────┼────────┼──────┼────────┼────────┤
│              │Surgeon⁰│Outside⁰│        │      │Surgeon⁰│Outside⁰│
├──────────────┼────────┼────────┼────────┼──────┼────────┼────────┤
│Operation²    │Inside⁰ │Outside¹│Patient⁰│Glove⁰│Patient¹│Inside⁰ │
│              ├────────┼────────┼────────┼──────┼────────┼────────┤
│              │Inside¹ │Patient⁰│        │Glove¹│Patient²│Outside¹│
│              ├────────┼────────┤        ├──────┼────────┼────────┤
│              │Surgeon⁰│Outside⁰│        │      │Surgeon⁰│Outside⁰│
└──────────────┴────────┴────────┴────────┴──────┴────────┴────────┘