MyNixOS website logo
Description

Quasiquoters that act on a sequence of patterns and compiles these view into patterns and expressions.

any-pat

Build Status of the package by GitHub actions Hackage version badge

Combine multiple patterns in a single pattern and range membership checks.

Usage

This package ships with three QuasiQuoters: anypat, maypat and rangepat.

anypat and maypat

anypat and maypat have the same purpose. Defining multiple possible patterns in a single clause. Indeed, consider the following example:

mightBe ∷ (Int, a, a) → Maybe a
mightBe (0, a, _) = Just a
mightBe (1, _, a) = Just a
mightBe _ = Nothing

the first two clauses have some repetitive elements. We can combine the two through the anypat or maypat quasiquoter:

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ViewPatterns #-}

mightBe ∷ (Int, a, a) → Maybe a
mightBe [anypat|(0, a, _), (1, _, a)|] = Just a
mightBe _ = Nothing

or with maypat:

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ViewPatterns #-}

mightBe ∷ (Int, a, a) → Maybe a
mightBe [maypat|(0, a _), (1, _, a), _|] = a

and that's it. No, there is no need to wrap a in a Just in the last code example.

We can also use the maypat and anypat to generate expressions, with:

{-# LANGUAGE QuasiQuotes #-}

mightBe ∷ (Int, a, a) → Maybe a
mightBe = [maypat|(0, a _), (1, _, a), _|]

If it is used as pattern, the ViewPatterns language extension should be enabled. Furthermore the QuasiQuotes extension should of course be enabled for any use of the quasi quoters.

The difference between the two QuasiQuoters (anypat and maypat) is in how they handle variable names. Variable names defined in the patterns are used in the body of the function, so it makes sense that if the clause "fires", they have a value. This means that a reasonable condition is that all patterns have the same set of variable names and that the variable names have the same type.

The anypat requires that all patterns have the same variables. So, [anypat|(0, a), (1, _)|] will raise an error at compile time. This is because if the second pattern (1, _) "fires", it will not provide a value for the a variable. This is a problem, because the body of the function expects a value for the a variable.

One possible solution would be to pass a value like undefined or an infinite loop (i.e. y = let x = x in x for example) as the value for the a variable. However, this is not a good solution, because it would cause a lot of trouble.

Therefore, maypat comes with a different solution. It performs analysis on the variables used in the different patterns. Variables that occur in all patterns are simply passed with the real value. Variables that occur only in a (strict) subset of the listed patterns are passed as a Maybe a value. If the first pattern that "fires" (left-to-right) for the value has that variable, it will be wrapped in a Just. Otherwise, it will pass Nothing as that variable.

Some functions in the base package, for example, have a simple equivalent with anypat or maypat, for example listToMaybe ∷ [a] → Maybe a can be implemented as:

{-# LANGUAGE QuasiQuotes #-}

listToMaybe ∷ [a] → Maybe a
listToMaybe = [maypat|(a:_), _|]

hashpat

hashpat is a quasi quoter for patterns to make lookups on HashMaps more convenient. Indeed, we can for example create a function:

sumab ∷ HashMap String Int → Int
sumab [hashpat|"a" → a, "b" → b|] = a + b
sumab _ = 0

this will "fire" the first clause given the HashMap has both an "a" and "b" as key, and it will thus perform the corresponding lookups and unify a and b with the corresponding output. This thus means that a HashMap that contains "a" and "b", we will sum up the values for that HashMap, and if not return 0.

The keys can take arbitrary expressions, and we thus can for example use "a" ++ "b" as key. Furthermore we can use an arbitrary pattern at the right side of the arrow, such that it only "fires" if it matches a given pattern. For example:

bifanothing ∷ HashMap String (Maybe Int) → Int
bifanothing [hashpat|"a" ++ "b" → Nothing, "b" → Just x|] = x
bifanothing _ = 0

this will thus fire the first clause if the HashMap has a key "ab" that maps to Nothing, and a key "b" that maps to a Just x, and in that case return the x. Essentially it thus compiles the pattern, which is a sequence of view patterns into a function that will perform lookups and then pattern match on the result of these lookups.

Since HashMaps with strings as keys are common, and unpacking these into variables with the same name is a common usecase, one can also just list variable names. These will then be translated into lookups with strings, or if the OverloadedStrings extension is enabled, a HashMap where the key type is both a member of Hashable and IsString:

{-# LANGUAGE OverloadedStrings, QuasiQuotes, ViewPatterns #-}

sumab :: (Hashable s, IsString s, Num n) => HashMap s n -> n
sumab [hashpat|a, b|] = a + b
sumab _ = 0

this thus makes it possible to handle optional named paramters, although these all have to have the same type of corresponding values. The dictionary can thus to some extend ???

rangepat

rangepat defines patterns for range memberships. For example:

{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ViewPatterns #-}

isInRange ∷ Int → Bool
isInRange [rangepat|0, 5 .. 50|] = True
isInRange _ = False

This will check in constant time if the number is in the given range (here [0, 5 .. 50]). The pattern has however some caveats, especially with floating point numbers, and likely any other type where fromEnum en toEnum are not bijective.

Package structure

The package has only one module: Data.Pattern.Any that exports the two QuasiQuoters named anypat and maypat together with some utility functions to obtain the variables names from a pattern.

Behind the curtains

The package transforms a sequence of patterns to a view pattern, or an expression, depending on where the quasi quoter is used. If we create a pattern [anypat|p1, p2, …, pn|], it will create a view pattern that looks like:

\case
  p1 → Just n⃗
  p2 → Just n⃗
  ⋮   ⋮    ⋮
  pn → Just n⃗
  _ → Nothing

with n⃗ the (sorted) tuple of names found in the patterns. It then makes a view pattern e → n⃗ that thus maps the found values for the variables to the names that can then be used in the body of the function.

There are some (small) optimizations that for example are used if no variable names are used in the patterns, or only one. If a wildcard pattern is used, it can also omit the Maybe data type.

For rangepat, it first converts the range to a RangeObj, and then checks membership in constant time (given we assume that operations on Int run in constant time).

any-pat is inferred safe Haskell

It can not be marked safe, since the modules it depends on are not marked safe, but its safeness can be inferred by the compiler.

Contribute

You can contribute by making a pull request on the GitHub repository.

You can contact the package maintainer by sending a mail to [email protected].


This package is dedicated to professor De Causmaecker, who taught most of the basic programming courses at university, not Haskell however.

Metadata

Version

0.4.0.0

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows