| 1 |
{-# OPTIONS --safe --cubical-compatible #-} |
| 2 |
|
| 3 |
module TakeWhile where |
| 4 |
|
| 5 |
open import Level |
| 6 |
open import Data.List.Base hiding (takeWhile) |
| 7 |
open import Data.List.Relation.Unary.All as List using ([]; _∷_) |
| 8 |
open import Data.List.Relation.Binary.Pointwise using (Pointwise; []; _∷_; refl) |
| 9 |
open import Data.List.Relation.Ternary.Appending.Propositional |
| 10 |
open import Data.Product.Base using (_×_; proj₁) |
| 11 |
open import Data.Maybe.Relation.Unary.All as Maybe using (nothing; just) |
| 12 |
import Data.Nat |
| 13 |
open import Relation.Unary |
| 14 |
open import Relation.Binary.PropositionalEquality as P using (_≡_) |
| 15 |
open import Relation.Nullary |
| 16 |
open import Relation.Nullary.Product |
| 17 |
|
| 18 |
-- Original bug reported in #1765 by James Wood |
| 19 |
_ : Appending (3 ∷ []) (2 ∷ []) (3 ∷ 2 ∷ []) |
| 20 |
_ = P.refl ∷ []++ P.refl ∷ [] |
| 21 |
|
| 22 |
|
| 23 |
variable |
| 24 |
a p : Level |
| 25 |
A : Set a |
| 26 |
P : A → Set p |
| 27 |
|
| 28 |
infix 1 _,_,_ |
| 29 |
record TakeWhile {A : Set a} (P : A → Set p) xs : Set (a ⊔ p) where |
| 30 |
constructor _,_,_ |
| 31 |
field |
| 32 |
{prefix rest} : List A |
| 33 |
goodPrefix : List.All P prefix |
| 34 |
isPrefix : Appending prefix rest xs |
| 35 |
isBiggest : Maybe.All (proj₁ ⊢ ∁ P) (uncons rest) |
| 36 |
open TakeWhile public |
| 37 |
|
| 38 |
takeWhile : Decidable P → Π[ TakeWhile P ] |
| 39 |
takeWhile P? [] = [] , []++ [] , nothing |
| 40 |
takeWhile P? (x ∷ xs) with P? x |
| 41 |
... | yes px = let (pxs' , prf , biggest) = takeWhile P? xs in |
| 42 |
px ∷ pxs' , P.refl ∷ prf , biggest |
| 43 |
... | no ¬px = [] , []++ refl P.refl , just ¬px |
| 44 |
|
| 45 |
open import Data.Char |
| 46 |
open import Data.String using (toList) |
| 47 |
|
| 48 |
lower? : (c : Char) → Dec ('a' ≤ c × c ≤ 'z') |
| 49 |
lower? c = 'a' ≤? c ×-dec c ≤? 'z' |
| 50 |
|
| 51 |
_ : takeWhile lower? (toList "helLo") |
| 52 |
≡ record { prefix = toList "hel" |
| 53 |
; isPrefix = P.refl ∷ P.refl ∷ P.refl ∷ []++ P.refl ∷ P.refl ∷ [] |
| 54 |
} |
| 55 |
_ = P.refl |