| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Example use case for a trie: a wee generic lexer |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --sized-types #-} |
| 8 |
|
| 9 |
module README.Data.Trie.NonDependent where |
| 10 |
|
| 11 |
------------------------------------------------------------------------ |
| 12 |
-- Introduction |
| 13 |
|
| 14 |
-- A Trie is a tree of values indexed by words in a finite language. It |
| 15 |
-- allows users to quickly compute the Brzozowski derivative of that |
| 16 |
-- little mapping from words to values. |
| 17 |
|
| 18 |
-- In the most general case, values can depend upon the list of characters |
| 19 |
-- that constitutes the path leading to them. Here however we consider a |
| 20 |
-- non-dependent case (cf. README.Trie.Dependent for a dependent use case). |
| 21 |
|
| 22 |
-- We can recognize keywords by storing the list of characters they |
| 23 |
-- correspond to as paths in a Trie and the constructor they are decoded |
| 24 |
-- to as the tree's values. |
| 25 |
|
| 26 |
-- E.g. |
| 27 |
-- [ . ] is a root |
| 28 |
-- [ -- m --> ] is an m-labeled edge and is followed when reading 'm' |
| 29 |
-- [ (X) ] is a value leaf storing constructor X |
| 30 |
|
| 31 |
-- --> -- m --> -- m --> -- a --> (LEMMA) |
| 32 |
-- / |
| 33 |
-- -- l --> -- e --> -- t --> (LET) |
| 34 |
-- / |
| 35 |
-- / -- u --> -- t --> -- u --> -- a --> -- l --> (MUTUAL) |
| 36 |
-- / / |
| 37 |
-- .< -- m --> -- o --> -- d --> -- u --> -- l --> -- e --> (MODULE) |
| 38 |
-- \ |
| 39 |
-- -- w --> -- h --> -- e --> -- r --> -- e --> (WHERE) |
| 40 |
-- \ |
| 41 |
-- --> -- n --> (WHEN) |
| 42 |
|
| 43 |
|
| 44 |
-- after reading 'w', we get the derivative: |
| 45 |
|
| 46 |
-- . -- h --> -- e --> -- r --> -- e --> (WHERE) |
| 47 |
-- \ |
| 48 |
-- --> -- n --> (WHEN) |
| 49 |
|
| 50 |
|
| 51 |
open import Level |
| 52 |
open import Data.Unit |
| 53 |
open import Data.Bool |
| 54 |
open import Data.Char as Char |
| 55 |
import Data.Char.Properties as Char |
| 56 |
open import Data.List.Base as List using (List; []; _∷_) |
| 57 |
open import Data.List.Fresh as List# using (List#; []; _∷#_) |
| 58 |
open import Data.Maybe as Maybe |
| 59 |
open import Data.Product.Base as Product using (_×_; ∃; proj₁; _,_) |
| 60 |
open import Data.String.Base as String using (String) |
| 61 |
open import Data.String.Properties as String using (_≟_) |
| 62 |
open import Data.These as These |
| 63 |
|
| 64 |
open import Function.Base using (case_of_; _$_; _∘′_; id; _on_) |
| 65 |
open import Relation.Nary |
| 66 |
open import Relation.Binary.Core using (Rel) |
| 67 |
open import Relation.Nullary.Decidable using (¬?) |
| 68 |
|
| 69 |
open import Data.Trie Char.<-strictTotalOrder |
| 70 |
open import Data.Tree.AVL.Value |
| 71 |
|
| 72 |
------------------------------------------------------------------------ |
| 73 |
-- Generic lexer |
| 74 |
|
| 75 |
record Lexer t : Set (suc t) where |
| 76 |
field |
| 77 |
-- Our lexer is parametrised over the type of tokens |
| 78 |
Tok : Set t |
| 79 |
|
| 80 |
-- Keywords are distinguished strings associated to tokens |
| 81 |
Keyword : Set t |
| 82 |
Keyword = String × Tok |
| 83 |
|
| 84 |
-- Two keywords are considered distinct if the strings are not equal |
| 85 |
Distinct : Rel Keyword 0ℓ |
| 86 |
Distinct a b = ⌊ ¬? ((proj₁ a) String.≟ (proj₁ b)) ⌋ |
| 87 |
|
| 88 |
field |
| 89 |
|
| 90 |
-- We ask users to provide us with a fresh list of keywords to guarantee |
| 91 |
-- that no two keywords share the same string representation |
| 92 |
keywords : List# Keyword Distinct |
| 93 |
|
| 94 |
-- Some characters are special: they are separators, breaking a string |
| 95 |
-- into a list of tokens. Some are associated to a token value |
| 96 |
-- (e.g. parentheses) others are not (e.g. space) |
| 97 |
breaking : Char → ∃ λ b → if b then Maybe Tok else Lift _ ⊤ |
| 98 |
|
| 99 |
-- Finally, strings which are not decoded as keywords are coerced |
| 100 |
-- using a function to token values. |
| 101 |
default : String → Tok |
| 102 |
|
| 103 |
|
| 104 |
module _ {t} (L : Lexer t) where |
| 105 |
open Lexer L |
| 106 |
|
| 107 |
tokenize : String → List Tok |
| 108 |
tokenize = start ∘′ String.toList where |
| 109 |
|
| 110 |
mutual |
| 111 |
|
| 112 |
-- A Trie is defined for an alphabet of strictly ordered letters (here |
| 113 |
-- we have picked Char for letters and decided to use the strict total |
| 114 |
-- order induced by their injection into ℕ as witnessed by the statement |
| 115 |
-- open import Data.Trie Char.strictTotalOrder earlier in this file). |
| 116 |
|
| 117 |
-- It is parametrised by a set of Values indexed over list of letters. |
| 118 |
-- Because we focus on the non-dependent case, we pick the constant |
| 119 |
-- family of Value uniformly equal to Tok. It is trivially compatible |
| 120 |
-- with the notion of equality underlying the strict total order on Chars. |
| 121 |
|
| 122 |
Keywords : Set _ |
| 123 |
Keywords = Trie (const _ Tok) _ |
| 124 |
|
| 125 |
-- We build a trie from the association list so that we may easily |
| 126 |
-- compute the successive derivatives obtained by eating the |
| 127 |
-- characters one by one |
| 128 |
|
| 129 |
init : Keywords |
| 130 |
init = fromList $ List.map (Product.map₁ String.toList) $ proj₁ $ List#.toList keywords |
| 131 |
|
| 132 |
-- Kickstart the tokeniser with an empty accumulator and the initial |
| 133 |
-- trie. |
| 134 |
start : List Char → List Tok |
| 135 |
start = loop [] init |
| 136 |
|
| 137 |
-- The main loop |
| 138 |
loop : (acc : List Char) → -- chars read so far in this token |
| 139 |
(toks : Keywords) → -- keyword candidates left at this point |
| 140 |
(input : List Char) → -- list of chars to tokenize |
| 141 |
List Tok |
| 142 |
-- Empty input: finish up, check whether we have a non-empty accumulator |
| 143 |
loop acc toks [] = push acc [] |
| 144 |
-- At least one character |
| 145 |
loop acc toks (c ∷ cs) = case breaking c of λ where |
| 146 |
-- if we are supposed to break on this character, we do |
| 147 |
(true , m) → push acc $ maybe′ _∷_ id m $ start cs |
| 148 |
-- otherwise we see whether it leads to a recognized keyword |
| 149 |
(false , _) → case lookupValue toks (c ∷ []) of λ where |
| 150 |
-- if so we can forget about the current accumulator and |
| 151 |
-- restart the tokenizer on the rest of the input |
| 152 |
(just tok) → tok ∷ start cs |
| 153 |
-- otherwise we record the character we read in the accumulator, |
| 154 |
-- compute the derivative of the map of keyword candidates and |
| 155 |
-- keep going with the rest of the input |
| 156 |
nothing → loop (c ∷ acc) (lookupTrie toks c) cs |
| 157 |
|
| 158 |
-- Grab the accumulator and, unless it is empty, push it on top of |
| 159 |
-- the decoded list of tokens |
| 160 |
push : List Char → List Tok → List Tok |
| 161 |
push [] ts = ts |
| 162 |
push cs ts = default (String.fromList (List.reverse cs)) ∷ ts |
| 163 |
|
| 164 |
|
| 165 |
------------------------------------------------------------------------ |
| 166 |
-- Concrete instance |
| 167 |
|
| 168 |
-- A small set of keywords for a language with expressions of the form |
| 169 |
-- `let x = e in b`. |
| 170 |
|
| 171 |
module LetIn where |
| 172 |
|
| 173 |
data TOK : Set where |
| 174 |
LET EQ IN : TOK |
| 175 |
LPAR RPAR : TOK |
| 176 |
ID : String → TOK |
| 177 |
|
| 178 |
keywords : List# (String × TOK) (λ a b → ⌊ ¬? ((proj₁ a) String.≟ (proj₁ b)) ⌋) |
| 179 |
keywords = ("let" , LET) |
| 180 |
∷# ("=" , EQ) |
| 181 |
∷# ("in" , IN) |
| 182 |
∷# [] |
| 183 |
|
| 184 |
-- Breaking characters: spaces (thrown away) and parentheses (kept) |
| 185 |
breaking : Char → ∃ (λ b → if b then Maybe TOK else Lift 0ℓ ⊤) |
| 186 |
breaking c = if isSpace c then true , nothing else parens c where |
| 187 |
|
| 188 |
parens : Char → _ |
| 189 |
parens '(' = true , just LPAR |
| 190 |
parens ')' = true , just RPAR |
| 191 |
parens _ = false , _ |
| 192 |
|
| 193 |
default : String → TOK |
| 194 |
default = ID |
| 195 |
|
| 196 |
letIn : Lexer 0ℓ |
| 197 |
letIn = record { LetIn } |
| 198 |
|
| 199 |
open import Agda.Builtin.Equality |
| 200 |
|
| 201 |
-- A test case: |
| 202 |
|
| 203 |
open LetIn |
| 204 |
_ : tokenize letIn "fix f x = let b = fix f in (f b) x" |
| 205 |
≡ ID "fix" |
| 206 |
∷ ID "f" |
| 207 |
∷ ID "x" |
| 208 |
∷ EQ |
| 209 |
∷ LET |
| 210 |
∷ ID "b" |
| 211 |
∷ EQ |
| 212 |
∷ ID "fix" |
| 213 |
∷ ID "f" |
| 214 |
∷ IN |
| 215 |
∷ LPAR |
| 216 |
∷ ID "f" |
| 217 |
∷ ID "b" |
| 218 |
∷ RPAR |
| 219 |
∷ ID "x" |
| 220 |
∷ [] |
| 221 |
_ = refl |