on 7afa310 help sign in
Idris-dev/libs/base/Data/Vect/Quantifiers.idr
72 lines 2.8 kB history raw
1module Data.Vect.Quantifiers
3||| A proof that some element of a vector satisfies some property
4|||
5||| @ P the property to be satsified
6data Any : (P : a -> Type) -> Vect n a -> Type where
7 ||| A proof that the satisfying element is the first one in the `Vect`
8 Here : {P : a -> Type} -> {xs : Vect n a} -> P x -> Any P (x :: xs)
9 ||| A proof that the satsifying element is in the tail of the `Vect`
10 There : {P : a -> Type} -> {xs : Vect n a} -> Any P xs -> Any P (x :: xs)
12||| No element of an empty vector satisfies any property
13anyNilAbsurd : {P : a -> Type} -> Any P Nil -> Void
14anyNilAbsurd (Here _) impossible
15anyNilAbsurd (There _) impossible
17instance Uninhabited (Any p Nil) where
18 uninhabited = anyNilAbsurd
20anyElim : {xs : Vect n a} -> {P : a -> Type} -> (Any P xs -> b) -> (P x -> b) -> Any P (x :: xs) -> b
21anyElim _ f (Here p) = f p
22anyElim f _ (There p) = f p
24||| Given a decision procedure for a property, determine if an element of a
25||| vector satisfies it.
26|||
27||| @ P the property to be satisfied
28||| @ dec the decision procedure
29||| @ xs the vector to examine
30any : {P : a -> Type} -> (dec : (x : a) -> Dec (P x)) -> (xs : Vect n a) -> Dec (Any P xs)
31any _ Nil = No anyNilAbsurd
32any p (x::xs) with (p x)
33 | Yes prf = Yes (Here prf)
34 | No prf =
35 case any p xs of
36 Yes prf' => Yes (There prf')
37 No prf' => No (anyElim prf' prf)
39||| A proof that all elements of a vector satisfy a property. It is a list of
40||| proofs, corresponding element-wise to the `Vect`.
41data All : (P : a -> Type) -> Vect n a -> Type where
42 Nil : {P : a -> Type} -> All P Nil
43 (::) : {P : a -> Type} -> {xs : Vect n a} -> P x -> All P xs -> All P (x :: xs)
45||| If there does not exist an element that satifies the property, then it is
46||| the case that all elements do not satisfy.
47negAnyAll : {P : a -> Type} -> {xs : Vect n a} -> Not (Any P xs) -> All (\x => Not (P x)) xs
48negAnyAll {xs=Nil} _ = Nil
49negAnyAll {xs=(x::xs)} f = (\x => f (Here x)) :: negAnyAll (\x => f (There x))
51notAllHere : {P : a -> Type} -> {xs : Vect n a} -> Not (P x) -> All P (x :: xs) -> Void
52notAllHere _ Nil impossible
53notAllHere np (p :: _) = np p
55notAllThere : {P : a -> Type} -> {xs : Vect n a} -> Not (All P xs) -> All P (x :: xs) -> Void
56notAllThere _ Nil impossible
57notAllThere np (_ :: ps) = np ps
59||| Given a decision procedure for a property, decide whether all elements of
60||| a vector satisfy it.
61|||
62||| @ P the property
63||| @ dec the decision procedure
64||| @ xs the vector to examine
65all : {P : a -> Type} -> (dec : (x : a) -> Dec (P x)) -> (xs : Vect n a) -> Dec (All P xs)
66all _ Nil = Yes Nil
67all d (x::xs) with (d x)
68 | No prf = No (notAllHere prf)
69 | Yes prf =
70 case all d xs of
71 Yes prf' => Yes (prf :: prf')
72 No prf' => No (notAllThere prf')