Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Pattern lang
- = NonVariablePattern (lang (Pattern lang))
- | VariablePattern Var
- pat :: lang (Pattern lang) -> Pattern lang
Documentation
A pattern can be either a variable or an non-variable expression of patterns.
A NonVariablePattern
will only match an expression if the lang
constructor matches an expression and all child patterns match the expression children.
A VariablePattern
matches any expression.
Example
The expression
expr :: Fix Sym expr = BinOp Add (Sym "x") (Const 2.0) -- i.e. x + 2
Would be matched against the following patterns
pat1 :: Pattern Sym pat1 = VariablePattern 1 pat2 :: Pattern Sym pat2 = NonVariablePattern (BinOp Add (VariablePattern 1) (VariablePattern 2)) pat3 :: Pattern Sym pat3 = NonVariablePattern (BinOp Add (VariablePattern 1) (NonVariablePattern (Const 2)))
But would not be matched against the following patterns
pat4 :: Pattern Sym pat4 = NonVariablePattern (Const 5) pat5 :: Pattern Sym pat5 = NonVariablePattern (BinOp Add (NonVariablePattern (Sym "y")) (NonVariablePattern (Const 2))) pat6 :: Pattern Sym pat6 = NonVariablePattern (BinOp Add (NonVariablePattern (Sym "x")) (NonVariablePattern (Const 3)))
IsString
Pattern
instances IsString
, which means one can write a variable pattern simply as a string.
It works by using hashString
to create a unique integer for a VariablePattern
For example, we could write the following pattern that would match a+a
and b+b
but not a+b
pat7 :: Pattern Sym
pat7 = pat
(BinOp Add "x" "x")
NonVariablePattern (lang (Pattern lang)) | |
VariablePattern Var | Should be a >0 positive number |