| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Unsafe String operations and proofs |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --with-K #-} |
| 8 |
|
| 9 |
module Data.String.Unsafe where |
| 10 |
|
| 11 |
import Data.List.Base as List using (List; _∷_; _++_; length; replicate; tail) |
| 12 |
import Data.List.Properties as Listₚ using (length-replicate; length-++) |
| 13 |
open import Data.Maybe.Base using (maybe′) |
| 14 |
open import Data.Nat.Base using (zero; suc; _+_) |
| 15 |
open import Data.Product.Base using (proj₂) |
| 16 |
open import Data.String.Base |
| 17 |
using (String; toList; fromList; length; tail; _++_; replicate) |
| 18 |
open import Function.Base using (_∘′_) |
| 19 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; cong) |
| 20 |
open import Relation.Binary.PropositionalEquality.Properties |
| 21 |
using (module ≡-Reasoning) |
| 22 |
open import Relation.Binary.PropositionalEquality.TrustMe using (trustMe) |
| 23 |
|
| 24 |
open ≡-Reasoning |
| 25 |
|
| 26 |
------------------------------------------------------------------------ |
| 27 |
-- Properties of tail |
| 28 |
|
| 29 |
length-tail : ∀ s → length s ≡ maybe′ (suc ∘′ length) zero (tail s) |
| 30 |
length-tail s = trustMe |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
-- Properties of conversion functions |
| 34 |
|
| 35 |
toList∘fromList : ∀ s → toList (fromList s) ≡ s |
| 36 |
toList∘fromList s = trustMe |
| 37 |
|
| 38 |
fromList∘toList : ∀ s → fromList (toList s) ≡ s |
| 39 |
fromList∘toList s = trustMe |
| 40 |
|
| 41 |
toList-++ : ∀ s t → toList (s ++ t) ≡ toList s List.++ toList t |
| 42 |
toList-++ s t = trustMe |
| 43 |
|
| 44 |
length-++ : ∀ s t → length (s ++ t) ≡ length s + length t |
| 45 |
length-++ s t = begin |
| 46 |
length (s ++ t) ≡⟨⟩ |
| 47 |
List.length (toList (s ++ t)) ≡⟨ cong List.length (toList-++ s t) ⟩ |
| 48 |
List.length (toList s List.++ toList t) ≡⟨ Listₚ.length-++ (toList s) ⟩ |
| 49 |
length s + length t ∎ |
| 50 |
|
| 51 |
length-replicate : ∀ n {c} → length (replicate n c) ≡ n |
| 52 |
length-replicate n {c} = let cs = List.replicate n c in begin |
| 53 |
length (replicate n c) ≡⟨ cong List.length (toList∘fromList cs) ⟩ |
| 54 |
List.length cs ≡⟨ Listₚ.length-replicate n ⟩ |
| 55 |
n ∎ |