-
Notifications
You must be signed in to change notification settings - Fork 0
/
QALogic.lhs
168 lines (131 loc) · 5.17 KB
/
QALogic.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
We now bring questions into the picture, i.e.~into the language.
In order not to go back and change stuff above we start mainly from scratch.
\begin{code}
module QALogic where
\end{code}
There are some bits we can reuse though:
\begin{code}
import AGTYPES (implies,AgentName,Prop,Partition,World,Label)
\end{code}
\subsection{Syntax and Semantics}
Language, Agent Types and Models have to be redefined.
Definition 4: Language PQLTT
\begin{code}
data QForm =
Top | Prp Prop | AgentName `IsA` AgentType
| Neg QForm | Conj [QForm]
| K AgentName QForm
| Announce AgentName QForm QForm
| Ask AgentName QForm QForm
| Answer AgentName QForm
deriving (Eq,Show)
\end{code}
Abbreviations:
\begin{code}
disj :: [QForm] -> QForm
disj fs = Neg $ Conj (map Neg fs)
impl :: QForm -> QForm -> QForm
impl f g = disj [Neg f, g]
kw :: AgentName -> QForm -> QForm
kw a f = disj [K a f, K a (Neg f)]
-- diamonds
diam :: (QForm -> QForm) -> QForm -> QForm
diam act f = Neg $ act (Neg f)
diamAnnounce :: AgentName -> QForm -> QForm -> QForm
diamAnnounce a f = diam $ Announce a f
\end{code}
We also have to define agent types again because now they map different types of formulas.
\begin{code}
data AgentType = AgT String (AgentName -> QForm -> QForm)
instance (Eq AgentType) where
(==) (AgT s _) (AgT t _) = s == t
instance (Show AgentType) where
show (AgT s _) = s
truthTeller, liar, bluffer, subjTruthTeller, subjLiar :: AgentType
truthTeller = AgT "truthTeller" (const id)
liar = AgT "liar" (const Neg)
bluffer = AgT "bluffer" (const $ const Top)
subjTruthTeller = AgT "subjTruthTeller" K
subjLiar = AgT "subjLiar" (\ i f -> Neg $ K i f)
labelAt :: Scene -> Label
labelAt ((ws,_),w) = l where Just (l,_) = lookup w ws
typeAtOf :: Scene -> AgentName -> AgentType
typeAtOf ((ws,_),w) a = t where
Just (_,tOf) = lookup w ws
Just t = lookup a tOf
partAtOf :: Scene -> AgentName -> [World]
partAtOf ((_,pss),w) i = head $ filter (elem w) ps where Just ps = lookup i pss
type Model = ( [(World, (Label, [(AgentName, AgentType)]))],
[(AgentName, Partition World)] )
type Scene = (Model, World)
\end{code}
Definition 5: PQLTT Semantics
\begin{code}
type Context = Maybe (AgentName,QForm)
qIsTrue :: Scene -> Context -> QForm -> Bool
qIsTrue _ _ Top = True
qIsTrue sc _ (Prp p) = p `elem` labelAt sc
qIsTrue sc _ (a `IsA` t) = typeAtOf sc a == t
qIsTrue sc c (Neg f) = not (qIsTrue sc c f)
qIsTrue sc c (Conj fs) = all (qIsTrue sc c) fs
qIsTrue (m,w) c (K a f) = all (\v -> qIsTrue (m,v) c f) (partAtOf (m,w) a)
qIsTrue sc _ (Ask a f g) = qIsTrue sc (Just (a,f)) g
qIsTrue _ Nothing Announce{} = error "Nobody asked anything!"
qIsTrue sc@(m,w) c@(Just (a1,q)) (Announce a2 f g) =
(a1 == a2) && f `elem` [q,Neg q] && qCanSay sc a1 f
`implies` qIsTrue (qAnnounce m c (Just a1) f, w) Nothing g
\end{code}
Note that the semantic clause for answers quantifies over all formulas.
This is not feasible in the implementation as there are infinitely many formulas.
But we know that only the two formulas which count as an answer, i.e.~the formula representing the question and its negation, are relevant.
For all other formulas the Announce-Box will be trivially true
\cite[page 138]{liuWang2013:agentTypesHLPE}.
\begin{code}
qIsTrue sc c (Answer a g) =
all (\f -> qIsTrue sc c (Announce a f g)) relevantFormulas where
relevantFormulas = case c of
Nothing -> []
Just (_,q) -> [q,Neg q]
qCanSay :: Scene -> AgentName -> QForm -> Bool
qCanSay sc a f = qIsTrue sc Nothing (agtf a f) where AgT _ agtf = typeAtOf sc a
qAnnounce :: Model -> Context -> Maybe AgentName -> QForm -> Model
qAnnounce oldm@(oldws,oldpss) c ma f = (ws,pss) where
ws = filter check oldws
check (v,_) = case ma of
Nothing -> qIsTrue (oldm,v) c f
Just a -> qIsTrue (oldm,v) Nothing (agtf a f) where AgT _ agtf = typeAtOf (oldm,v) a
pss = map (fmap strip) oldpss
strip set = filter ([]/=) $ map (filter (`elem` map fst ws)) set
\end{code}
As usual, validity is truth at all worlds:
\begin{code}
qIsValid :: Model -> QForm -> Bool
qIsValid m f = and [ qIsTrue (m,w) Nothing f | w <- map fst (fst m) ]
\end{code}
% TODO: translations back and forth!
\subsection{Example 5: Death or Freedom with questions}
\begin{code}
example5m1 :: Model
example5m1 =
( [ (1,([1],[("A",truthTeller),("B",liar)]))
, (2,([1],[("A",liar),("B",truthTeller)]))
, (3,([ ],[("A",truthTeller),("B",liar)]))
, (4,([ ],[("A",liar),("B",truthTeller)]))
], [ ("C", [[1..4]]) ] )
ex5q1, ex5q2 :: QForm -> QForm
-- Will the other man tell me that your path leads to Freedom?
ex5q1 = Ask "A" ( Ask "B" (Prp 1) (diamAnnounce "B" (Prp 1) Top) )
-- Will you say 'yes' if you are asked whether your path leads to Freedom?
ex5q2 = Ask "A" ( Ask "A" (Prp 1) (diamAnnounce "A" (Prp 1) Top) )
-- Both questions ensure that C learns whether Prp 1 holds.
ex5validity :: QForm
ex5validity = Conj [ action $ Answer "A" $ kw "C" (Prp 1) | action <- [ex5q1,ex5q2] ]
\end{code}
Result:
\begin{showCode}
*QALogic> qIsValid example5m1 ex5validity
True
\end{showCode}
% TODO: verify via translations, p. 141
% NEXT: Definition 6, p. 142
% TODO: translate all to PAL, then use symbolic methods???