Skip to content

Query Examples

Vladislav Zavialov edited this page Oct 12, 2020 · 1 revision

Regular expressions are tricky to get right, so whatever you are searching for, get ready for both false positives and false negatives. With that in mind, here are some interesting queries:

Standalone kind signatures

^type\b\s+\w+\s+::\s

Matches lines such as:

type Sigma :: forall s -> (s ~> Type) -> Type

Here's how it works:

  1. The ^ matches the start of a line (rules out some false positives)
  2. The word type is a keyword that begins a standalone kind signature
  3. \s matches a single whitespace character, and \s+ matches one or more
  4. \w+ matches a one or more word characters, i.e. an alphanumeric identifier
  5. :: matches the double colon syntax of type signatures

The kind of type lists

:: \[\*\]

Matches lines such as:

data HList :: [*] -> * where

Here's how it works:

  1. :: matches the double colon syntax of type signatures
  2. \[ and \] match the square brackets (list syntax)
  3. \* matches the star symbol used to denote the type of types

Promoted list constructor

\s':\s

Matches lines such as:

Map f (x ': xs) = (Apply f x) ': xs

Side note: this quotation mark is the DataKinds syntax for namespace disambiguation, and it's always redundant in case of :. And yet people like to write it anyway, as evidenced by the results of this query!

Identifiers that use pseudo-keywords

^(forall|qualified|as|family|role) =

Matches lines such as:

forall = token (Internal.forall Internal.Unicode)

Here's how it works:

  1. The ( and ) are not escaped, so they are part of regex syntax
  2. The | stands for logical ‘or’.

Plus in operator sections

\(\+\d+\)

Matches lines such as:

TM.adjust (+1) someKey mapRef

Here's how it works:

  1. \( and \) are escaped, so they match the parentheses
  2. \+ is escaped, to it matches the + symbol
  3. \d+ matches a sequence of one or more digits

Side note: while (-1) is a negative number, (+1) is an operator section. It would be more clear to write it as (+ 1) to avoid confusion, but this query reveals that there's plenty of code that doesn't use that extra space.