module StructurallyRecursiveDescentParsing.PBM where
import Data.Vec as Vec
open Vec using (Vec; _++_; [_])
import Data.List as List
open import Data.Nat
import Data.String as String
open String using (String) renaming (_++_ to _<+>_)
import Data.Char as Char
open Char using (Char; _==_)
open import Data.Function
open import Data.Bool
open import Data.Unit
open import Data.Maybe
import Data.Nat.Show as N
open import Coinduction
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
open import StructurallyRecursiveDescentParsing
open Token Char.decSetoid
data Colour : Set where
white : Colour
black : Colour
Matrix : Set → ℕ → ℕ → Set
Matrix A rows cols = Vec (Vec A cols) rows
record PBM : Set where
field
rows : ℕ
cols : ℕ
matrix : Matrix Colour rows cols
open PBM
makePBM : ∀ {rows cols} → Matrix Colour rows cols → PBM
makePBM m = record { rows = _; cols = _; matrix = m }
showColour : Colour → Char
showColour white = '0'
showColour black = '1'
show : PBM → String
show i = "P1 # Generated using Agda.\n" <+>
N.show (cols i) <+> " " <+> N.show (rows i) <+> "\n" <+>
showMatrix (matrix i)
where
showMatrix = String.fromList ∘
Vec.toList ∘
Vec.concat ∘
Vec.map ((λ xs → xs ++ [ '\n' ]) ∘ Vec.map showColour)
comment = tt <$ theToken '#'
<⊛ sat' (not ∘ _==_ '\n') ⋆
<⊛ theToken '\n'
colour = white <$ theToken '0'
∣ black <$ theToken '1'
pbm =
w∣c ⋆ ⊛>
theString (String.toVec "P1") ⊛>
w∣c ⋆ ⊛>
number !>>= λ cols → ♯₁
(w∣c + ⊛>
number >>= λ rows →
w∣c ⊛>
(makePBM <$> exactly rows (exactly cols (w∣c ⋆ ⊛> colour))) <⊛
any ⋆)
where w∣c = whitespace ∣ comment
module Example where
open Vec
image = makePBM ((white ∷ black ∷ []) ∷
(black ∷ white ∷ []) ∷
(black ∷ black ∷ []) ∷ [])
ex₁ : parseComplete (⟦ pbm ⟧ emptyGrammar)
(String.toList (show image)) ≡
List.[_] image
ex₁ = refl