howl-0.1.0.0: A small Wolfram Language interpreter and symbolic rewriting library
Safe HaskellNone
LanguageGHC2021

Howl.Pat

Description

Internal pattern representation used by the evaluator.

These patterns are compiled from Wolfram Language pattern expressions and then used during matching.

Synopsis

Documentation

data Pat #

TODO:

Composite Patterns - p..(Repeated), p...(RepeatedNull) - Except - Longest - Shortest - OptionsPattern, PatternSequence, Verbatim, HoldPattern - OrderlessPatternSequence - KeyValuePattern

Pattern Defaults _. (Optional) pattern with predefined default Default - predefined default arguments for a function

A compiled pattern.

Each constructor takes a list of variable names to bind to the result of the match. We need a list because we can have nested named patterns such as x:(y:(z:_)). If the list is empty, the pattern is unnamed.

Constructors

PatSymbol

A literal symbol pattern.

Fields

PatLit

A literal (numeric/string) pattern.

Fields

PatVar

A blank pattern, optionally constrained by a head symbol.

Fields

PatSeqVar

A sequence blank (__ or ___).

Fields

PatApp

A head application pattern with precomputed app attributes.

Fields

PatAlt

Alternatives, with precomputed missing names for each branch. When the left branch matches we bind missingInLeft to 'Sequence[]', and symmetrically for the right branch.

Fields

PatOptional

An optional pattern with a default expression.

Fields

PatCondition

A pattern with a /; condition.

Fields

Instances

Instances details
IsString Pat # 
Instance details

Defined in Howl.Pat

Methods

fromString :: String -> Pat #

Show Pat # 
Instance details

Defined in Howl.Pat

Methods

showsPrec :: Int -> Pat -> ShowS #

show :: Pat -> String #

showList :: [Pat] -> ShowS #

Eq Pat # 
Instance details

Defined in Howl.Pat

Methods

(==) :: Pat -> Pat -> Bool #

(/=) :: Pat -> Pat -> Bool #

Ord Pat # 
Instance details

Defined in Howl.Pat

Methods

compare :: Pat -> Pat -> Ordering #

(<) :: Pat -> Pat -> Bool #

(<=) :: Pat -> Pat -> Bool #

(>) :: Pat -> Pat -> Bool #

(>=) :: Pat -> Pat -> Bool #

max :: Pat -> Pat -> Pat #

min :: Pat -> Pat -> Pat #

PPrint Pat # 
Instance details

Defined in Howl.Pat

Methods

pPrint :: Pat -> String #

data PatAppType #

Application-type information attached to a compiled head pattern.

This records whether the matched head should be treated as free, commutative, associative, or associative-commutative.

Instances

Instances details
Show PatAppType # 
Instance details

Defined in Howl.Pat

Eq PatAppType # 
Instance details

Defined in Howl.Pat

Ord PatAppType # 
Instance details

Defined in Howl.Pat

data SeqType #

The multiplicity of a sequence pattern.

Constructors

ZeroOrMore 
OneOrMore 

Instances

Instances details
Show SeqType # 
Instance details

Defined in Howl.Pat

Eq SeqType # 
Instance details

Defined in Howl.Pat

Methods

(==) :: SeqType -> SeqType -> Bool #

(/=) :: SeqType -> SeqType -> Bool #

Ord SeqType # 
Instance details

Defined in Howl.Pat

outerNames :: Pat -> [Symbol] #

Get names from the outer-most constructor of a Pat.

patNames :: Pat -> Set Symbol #

All pattern variable names mentioned anywhere in a pattern.

mapNames :: ([Symbol] -> [Symbol]) -> Pat -> Pat #

Map a function over the names of the outermost constructor in a Pat.

addNames :: [Symbol] -> Pat -> Pat #

Add the given names to the outermost constructor of a pattern.

matchesUniqueExpr :: Pat -> Maybe Expr #

Check whether a pattern matches a unique expression and introduces no bindings.

For example, Foo[12] matches a unique expression, but Foo[x_] does not. This function is conservative and only uses syntax information, so there may be cases where the pattern does match a unique expression but this function does not detect it. For example, (x_/;SameQ[x,12]) matches only 12, but this function returns Nothing.

It is also important to reject patterns that introduce bindings because we are short-circuiting the pattern matching process, and so those bindings would not happen. TODO: we could pre-compute the bindings and store them.

patRootSymbol :: Pat -> Maybe Symbol #

Return the repeated head symbol of a pattern, if there is one.

This is used to deduce which symbol a rule should be associated with.

patFromExpr :: MonadFail m => (Symbol -> m PatAppType) -> Expr -> m Pat #

Compile a Wolfram Language pattern expression into a Pat.

The callback is used to determine the application type of head symbols while compiling application patterns.