-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unicode alphabet, subalphabets, and easier tests (#21)
- Loading branch information
Showing
3 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
open import Cubical.Foundations.Prelude | ||
open import Cubical.Foundations.HLevels | ||
|
||
module String.SubAlphabet where | ||
|
||
open import Cubical.Foundations.Structure | ||
open import Cubical.Foundations.Isomorphism | ||
open import Cubical.Foundations.Equiv | ||
|
||
open import Cubical.Functions.Embedding | ||
|
||
open import Cubical.Relation.Nullary.Base | ||
open import Cubical.Relation.Nullary.Properties | ||
|
||
open import Agda.Builtin.Char | ||
open import Agda.Builtin.String | ||
|
||
open import Cubical.Data.Bool | ||
open import Cubical.Data.Maybe | ||
open import Cubical.Data.Empty as Empty | ||
open import Cubical.Data.Sigma | ||
open import Cubical.Data.FinSet | ||
open import Cubical.Data.FinSet.Constructors | ||
open import Cubical.Data.FinSet.DecidablePredicate | ||
|
||
open import Cubical.HITs.PropositionalTruncation as PT | ||
|
||
open import Helper | ||
|
||
-- Pick out a subalphabet of another alphabet | ||
module _ | ||
(Alphabet Alphabet' : hSet ℓ-zero) | ||
(embed : ⟨ Alphabet ⟩ ↪ ⟨ Alphabet' ⟩) | ||
where | ||
SubAlphabet' : Type ℓ-zero | ||
SubAlphabet' = Σ[ c ∈ ⟨ Alphabet' ⟩ ] fiber (embed .fst) c | ||
|
||
SubAlphabetIso : | ||
Iso ⟨ Alphabet ⟩ (Σ ⟨ Alphabet' ⟩ (λ v → fiber (λ z → embed .fst z) v)) | ||
SubAlphabetIso = | ||
iso | ||
(λ x → (embed .fst x) , (x , refl)) | ||
(λ x → x .snd .fst) | ||
(λ b → | ||
Σ≡Prop (λ x → isEmbedding→hasPropFibers (embed .snd) x) | ||
(b .snd .snd)) | ||
(λ _ → refl) | ||
|
||
open Iso | ||
isSetSubAlphabet : isSet SubAlphabet' | ||
isSetSubAlphabet = | ||
isSetRetract (SubAlphabetIso .inv) (SubAlphabetIso .fun) | ||
(SubAlphabetIso .rightInv) (Alphabet .snd) | ||
|
||
SubAlphabet : hSet ℓ-zero | ||
SubAlphabet .fst = SubAlphabet' | ||
SubAlphabet .snd = isSetSubAlphabet | ||
|
||
module _ (isFinSetAlphabet : isFinSet ⟨ Alphabet ⟩) where | ||
isFinSetSubAlphabet : isFinSet SubAlphabet' | ||
isFinSetSubAlphabet = | ||
EquivPresIsFinSet (isoToEquiv SubAlphabetIso) isFinSetAlphabet | ||
|
||
module _ (DiscreteAlphabet' : Discrete ⟨ Alphabet' ⟩) where | ||
maybe-SubAlphabet : ⟨ Alphabet' ⟩ → Maybe ⟨ SubAlphabet ⟩ | ||
maybe-SubAlphabet c' = | ||
decRec | ||
(λ ∣in-image∣ → | ||
just | ||
(c' , | ||
(PT.rec (isEmbedding→hasPropFibers (embed .snd) c') | ||
(λ in-image → in-image) ∣in-image∣))) | ||
(λ ¬|in-image∣ → nothing) | ||
(DecProp∃ | ||
(_ , isFinSetAlphabet) | ||
(λ c → (_ , Alphabet' .snd _ _) , | ||
(DiscreteAlphabet' (embed .fst c) c')) | ||
.snd ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
open import Cubical.Foundations.Prelude | ||
open import Cubical.Foundations.HLevels | ||
|
||
module String.Unicode where | ||
|
||
open import Cubical.Foundations.Structure | ||
open import Cubical.Foundations.Isomorphism | ||
open import Cubical.Foundations.Equiv | ||
|
||
open import Cubical.Functions.Embedding | ||
|
||
open import Cubical.Relation.Nullary.Base | ||
open import Cubical.Relation.Nullary.Properties | ||
|
||
open import Agda.Builtin.Char | ||
open import Agda.Builtin.String | ||
|
||
open import Cubical.Data.Bool | ||
open import Cubical.Data.Maybe as Maybe | ||
open import Cubical.Data.List | ||
open import Cubical.Data.Empty as Empty | ||
open import Cubical.Data.Sigma | ||
open import Cubical.Data.FinSet | ||
open import Cubical.Data.FinSet.Constructors | ||
|
||
open import String.SubAlphabet | ||
|
||
|
||
UnicodeChar = Char | ||
UnicodeString = String | ||
|
||
-- Char equality is computed externally by JavaScript | ||
-- Internalize a path oracle | ||
postulate | ||
mkUnicodeCharPath-yes : (c c' : UnicodeChar) → primCharEquality c c' ≡ true → c ≡ c' | ||
mkUnicodeCharPath-no : (c c' : UnicodeChar) → primCharEquality c c' ≡ false → ¬ (c ≡ c') | ||
|
||
DiscreteUnicodeChar : Discrete UnicodeChar | ||
DiscreteUnicodeChar c c' = | ||
decRec | ||
(λ t → yes (mkUnicodeCharPath-yes c c' t)) | ||
(λ f → no (mkUnicodeCharPath-no c c' (flip (primCharEquality c c') f))) | ||
(primCharEquality c c' ≟ true) | ||
where | ||
flip : (b : Bool) → ¬ (b ≡ true) → b ≡ false | ||
flip false _ = refl | ||
flip true ¬t=t = Empty.rec (¬t=t refl) | ||
|
||
isSetUnicodeChar : isSet UnicodeChar | ||
isSetUnicodeChar = Discrete→isSet DiscreteUnicodeChar | ||
|
||
-- The unicode alphabet | ||
Unicode : hSet ℓ-zero | ||
Unicode .fst = UnicodeChar | ||
Unicode .snd = isSetUnicodeChar | ||
|
||
-- I thought this would be good to define alphabets, but | ||
-- that cannot be true because you cannot pattern match on the characters | ||
-- in an alphabet due to the paths in the fibers underlying the subalphabet | ||
-- definitions | ||
-- However, this should still be good to write test cases more concisely? | ||
-- Still likely no. Maybe if Eq was used instead of path? | ||
module _ | ||
(Alphabet : hSet ℓ-zero) | ||
(embed : ⟨ Alphabet ⟩ ↪ ⟨ Unicode ⟩) | ||
(isFinSetAlphabet : isFinSet ⟨ Alphabet ⟩) | ||
where | ||
UnicodeSubAlphabet : hSet ℓ-zero | ||
UnicodeSubAlphabet = SubAlphabet Alphabet Unicode embed | ||
|
||
-- This should probably be a stricter check that none of the | ||
-- characters were from outside of the subalphabet | ||
toString : String → List ⟨ UnicodeSubAlphabet ⟩ | ||
toString w = | ||
filterMap | ||
(maybe-SubAlphabet Alphabet Unicode embed | ||
isFinSetAlphabet DiscreteUnicodeChar) | ||
(primStringToList w) |