BoolMatcher.agda 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. ------------------------------------------------------------------------
  2. -- Simple regular expression matcher (without soundness proof)
  3. ------------------------------------------------------------------------
  4. open import Eq
  5. open import Setoids
  6. open import Prelude
  7. import RegExps
  8. module BoolMatcher (D : Datoid) where
  9. private
  10. open module D' = Datoid D
  11. open module S' = Setoid setoid
  12. open module R = RegExps setoid
  13. infix 4 _∈‿⟦_⟧¿
  14. ------------------------------------------------------------------------
  15. -- Helper function
  16. decToBool : forall {x y} -> Dec (x ≈ y) -> Bool
  17. decToBool (yes _) = true
  18. decToBool (no _) = false
  19. ------------------------------------------------------------------------
  20. -- Regular expression matcher
  21. matches-⊙¿ : (xs₁ xs₂ : [ carrier ]) -> (re₁ re₂ : RegExp) -> Bool
  22. _∈‿⟦_⟧¿ : (xs : [ carrier ]) -> (re : RegExp) -> Bool
  23. [] ∈‿⟦ ε ⟧¿ = true
  24. x ∷ [] ∈‿⟦ • ⟧¿ = true
  25. x ∷ [] ∈‿⟦ sym y ⟧¿ = decToBool (x ≟ y)
  26. xs ∈‿⟦ re₁ ∣ re₂ ⟧¿ = xs ∈‿⟦ re₁ ⟧¿ ∨ xs ∈‿⟦ re₂ ⟧¿
  27. xs ∈‿⟦ re₁ ⊙ re₂ ⟧¿ = matches-⊙¿ [] xs re₁ re₂
  28. [] ∈‿⟦ re ⋆ ⟧¿ = true
  29. x ∷ xs ∈‿⟦ re ⋆ ⟧¿ = matches-⊙¿ (x ∷ []) xs re (re ⋆)
  30. _ ∈‿⟦ _ ⟧¿ = false
  31. matches-⊙¿ xs₁ xs₂ re₁ re₂ with xs₁ ∈‿⟦ re₁ ⟧¿ ∨ xs₂ ∈‿⟦ re₂ ⟧¿
  32. matches-⊙¿ xs₁ xs₂ re₁ re₂ | true = true
  33. matches-⊙¿ xs₁ [] re₁ re₂ | false = false
  34. matches-⊙¿ xs₁ (x ∷ xs₂) re₁ re₂ | false =
  35. matches-⊙¿ (xs₁ ++ x ∷ []) xs₂ re₁ re₂