9 | import Data.List.Quantifiers
10 | import Decidable.Equality
12 | import public Search.Negation
13 | import public Search.HDecidable
14 | import public Search.Properties
23 | record Diagram (labels : Type) (state : Type) where
26 | transFn : (labels, state) -> List (labels, state)
30 | %name Diagram
td,td1,td2
34 | pComp : {Lbls1, Lbls2 : _} -> {Sts : _}
35 | -> (td1 : Diagram Lbls1 Sts)
36 | -> (td2 : Diagram Lbls2 Sts)
37 | -> Diagram (Lbls1, Lbls2) Sts
38 | pComp (TD transFn1 iState1) (TD transFn2 iState2) =
39 | TD compTransFn (iState1, iState2)
41 | compTransFn : ((Lbls1, Lbls2), Sts) -> List ((Lbls1, Lbls2), Sts)
42 | compTransFn = (\ ((l1, l2), st) =>
43 | map (\ (l1', st') => ((l1', l2), st')) (transFn1 (l1, st)) ++
44 | map (\ (l2', st') => ((l1, l2'), st')) (transFn2 (l2, st)))
48 | HiHorse : Diagram () Nat
49 | HiHorse = TD transFn ()
51 | transFn : ((), Nat) -> List ((), Nat)
52 | transFn (l, st) = [(l, (S st))]
56 | LoRoad : Diagram () Nat
57 | LoRoad = TD transFn ()
59 | transFn : ((), Nat) -> List ((), Nat)
60 | transFn (l, st) = [(l, pred st)]
66 | data LTE' : (n : Nat) -> (m : Nat) -> Type where
68 | LTEStep : LTE' n m -> LTE' n (S m)
71 | lteAltToLTE : {m : _} -> LTE' n m -> LTE n m
72 | lteAltToLTE {m=0} LTERefl = LTEZero
73 | lteAltToLTE {m=(S k)} LTERefl = LTESucc (lteAltToLTE LTERefl)
74 | lteAltToLTE {m=(S m)} (LTEStep s) = lteSuccRight (lteAltToLTE s)
77 | parameters (Lbls, Sts : Type)
80 | data CT : Type where
81 | At : (Lbls, Sts) -> Inf (List CT) -> CT
86 | model : Diagram Lbls Sts -> (st : Sts) -> CT
87 | model (TD transFn iState) st = follow (iState, st)
89 | follow : (Lbls, Sts) -> CT
91 | followAll : List (Lbls, Sts) -> List CT
93 | follow st = At st (Delay (followAll (transFn st)))
96 | followAll (st :: sts) = follow st :: followAll sts
102 | Formula = (depth : Nat) -> (tree : CT) -> Type
108 | data Models : (m : CT) -> (f : Formula) -> Type where
109 | ItModels : (d0 : Nat) -> ({d : Nat} -> (d0 `LTE'` d) -> f d m) -> Models m f
117 | record DepthInv (f : Formula) where
119 | prf : {n : Nat} -> {m : CT} -> f n m -> f (S n) m
124 | diModels : {n : Nat} -> {m : CT} -> {f : Formula} -> {auto d : DepthInv f}
125 | -> (p : f n m) -> Models m f
126 | diModels {n} {f} {m} @{(DI diPrf)} p = ItModels n (\ q => diLTE p q)
128 | diLTE : {n, n' : _} -> f n m -> (ltePrf' : n `LTE'` n') -> f n' m
129 | diLTE p LTERefl = p
130 | diLTE p (LTEStep x) = diPrf (diLTE p x)
134 | data TrueF : Formula where
135 | TT : {n : _} -> {m : _} -> TrueF n m
140 | TrueDI : DepthInv TrueF
141 | TrueDI = DI (const TT)
150 | data Guarded : (g : (st : Sts) -> (l : Lbls) -> Type) -> Formula where
151 | Here : {st : _} -> {l : _}
152 | -> {ms : Inf (List CT)} -> {depth : Nat}
154 | -> (guardOK : g st l)
155 | -> Guarded g depth (At (l, st) ms)
161 | diGuarded : {p : _} -> DepthInv (Guarded p)
162 | diGuarded {p} = DI prf
164 | prf : {n : _} -> {m : _} -> Guarded p n m -> Guarded p (S n) m
165 | prf (Here x) = Here x
172 | record AND' (f, g : Formula) (depth : Nat) (tree : CT) where
180 | diAND' : {f, g : Formula}
181 | -> {auto p : DepthInv f}
182 | -> {auto q : DepthInv g}
183 | -> DepthInv (AND' f g)
184 | diAND' @{(DI diP)} @{(DI diQ)} = DI (\ a' => MkAND' (diP a'.fst) (diQ a'.snd))
192 | data AlwaysUntil : (f, g : Formula) -> Formula where
194 | Here : {t : _} -> {n : _} -> g n t -> AlwaysUntil f g (S n) t
199 | There : {st : _} -> {infCTs : Inf _} -> {n : _}
200 | -> f n (At st infCTs)
201 | -> All ((AlwaysUntil f g) n) (Force infCTs)
202 | -> AlwaysUntil f g (S n) (At st infCTs)
208 | diAU : {f,g : _} -> {auto p : DepthInv f} -> {auto q : DepthInv g}
209 | -> DepthInv (AlwaysUntil f g)
210 | diAU @{(DI diP)} @{(DI diQ)} = DI prf
212 | prf : {d : _} -> {t : _}
213 | -> AlwaysUntil f g d t
214 | -> AlwaysUntil f g (S d) t
215 | prf (Here au) = Here (diQ au)
216 | prf (There au aus) = There (diP au) (mapAllAU prf aus)
219 | mapAllAU : {d : _} -> {lt : _}
220 | -> (prf : AlwaysUntil f g d t -> AlwaysUntil f g (S d) t)
221 | -> All (AlwaysUntil f g d) lt
222 | -> All (AlwaysUntil f g (S d)) lt
223 | mapAllAU prf [] = []
224 | mapAllAU prf (au :: aus) = (prf au) :: mapAllAU prf aus
234 | data ExistsUntil : (f, g : Formula) -> Formula where
236 | Here : {t : _} -> {n : _} -> g n t -> ExistsUntil f g (S n) t
240 | There : {st : _} -> {infCTs : Inf _} -> {n : _}
241 | -> f n (At st infCTs)
242 | -> Any (ExistsUntil f g n) (Force infCTs)
243 | -> ExistsUntil f g (S n) (At st infCTs)
249 | diEU : {f, g : _} -> {auto p : DepthInv f} -> {auto q : DepthInv g}
250 | -> DepthInv (ExistsUntil f g)
251 | diEU @{(DI diP)} @{(DI diQ)} = DI prf
253 | prf : {d : _} -> {t : _}
254 | -> ExistsUntil f g d t
255 | -> ExistsUntil f g (S d) t
256 | prf (Here eu) = Here (diQ eu)
257 | prf (There eu eus) = There (diP eu) (mapAnyEU prf eus)
260 | mapAnyEU : {d : _} -> {lt : _}
261 | -> (prf : ExistsUntil f g d t -> ExistsUntil f g (S d) t)
262 | -> Any (ExistsUntil f g d) lt
263 | -> Any (ExistsUntil f g (S d)) lt
264 | mapAnyEU prf (Here x) = Here (prf x)
265 | mapAnyEU prf (There x) = There (mapAnyEU prf x)
276 | AlwaysFinally : Formula -> Formula
277 | AlwaysFinally f = AlwaysUntil TrueF f
284 | ExistsFinally : Formula -> Formula
285 | ExistsFinally f = ExistsUntil TrueF f
289 | data Completed : Formula where
290 | IsCompleted : {st : _} -> {n : _} -> {infCTs : Inf _}
291 | -> (Force infCTs) === []
292 | -> Completed n (At st infCTs)
297 | diCompleted : DepthInv Completed
298 | diCompleted = DI prf
300 | prf : {d : _} -> {t : _} -> Completed d t -> Completed (S d) t
301 | prf (IsCompleted p) = IsCompleted p
305 | AlwaysGlobal : (f : Formula) -> Formula
306 | AlwaysGlobal f = AlwaysUntil f (f `AND'` Completed)
310 | ExistsGlobal : (f : Formula) -> Formula
311 | ExistsGlobal f = ExistsUntil f (f `AND'` Completed)
319 | MC : (f : Formula) -> Type
320 | MC f = (t : CT) -> (d : Nat) -> HDec (f d t)
324 | now : {g : (st : Sts) -> (l : Lbls) -> Type}
326 | -> {auto p : AnHDec hdec}
327 | -> ((st : Sts) -> (l : Lbls) -> hdec (g st l))
329 | now f (At (l, st) ms) d = [| Guards.Here (toHDec (f st l)) |]
333 | isCompleted : MC Completed
334 | isCompleted (At st ms) _ = IsCompleted <$> isEmpty (Force ms)
337 | isEmpty : {x : _} -> (n : List x) -> HDec (n === [])
338 | isEmpty [] = yes Refl
339 | isEmpty (_ :: _) = no
343 | mcAND' : {f, g : Formula} -> MC f -> MC g -> MC (f `AND'` g)
344 | mcAND' mcF mcG t d = [| MkAND' (mcF t d) (mcG t d) |]
351 | auSearch : {f, g : Formula} -> MC f -> MC g -> MC (AlwaysUntil f g)
352 | auSearch _ _ _ Z = no
353 | auSearch p1 p2 t@(At st ms) (S n) = [| AU.Here (p2 t n) |]
354 | <|> [| AU.There (p1 t n) rest |]
357 | rest : HDec (All (AlwaysUntil f g n) (Force ms))
358 | rest = HDecidable.List.all (toList ms) (\ m => auSearch p1 p2 m n)
365 | euSearch : {f, g : Formula} -> MC f -> MC g -> MC (ExistsUntil f g)
366 | euSearch _ _ _ Z = no
367 | euSearch p1 p2 t@(At st ms) (S n) = [| EU.Here (p2 t n) |]
368 | <|> [| EU.There (p1 t n) rest |]
370 | rest : HDec (Any (ExistsUntil f g n) ms)
371 | rest = HDecidable.List.any (Force ms) (\ m => euSearch p1 p2 m n)
375 | efSearch : {f : _} -> MC f -> MC (ExistsFinally f)
376 | efSearch g = euSearch (\ _, _ => yes TT) g
380 | afSearch : {f : _} -> MC f -> MC (AlwaysFinally f)
381 | afSearch g = auSearch (\ _, _ => yes TT) g
385 | egSearch : {f : _} -> MC f -> MC (ExistsGlobal f)
386 | egSearch g = euSearch g (g `mcAND'` isCompleted)
390 | agSearch : {f : _} -> MC f -> MC (AlwaysGlobal f)
391 | agSearch g = auSearch g (g `mcAND'` isCompleted)
399 | tree : CT ((), ()) Nat
400 | tree = model ((), ()) Nat (HiHorse `pComp` LoRoad) 0
407 | efSearch ((), ()) Nat
409 | (\st, _ => fromDec $
decEq st 10)) tree 20
414 | r10Proof : Models ((), ()) Nat
416 | (ExistsFinally ((), ()) Nat
417 | (Guarded ((), ()) Nat (\ st, _ => st === 10)))
418 | r10Proof = diModels ((), ()) Nat (reaches10.evidence Oh)