Title: Theory / Unimportant / CS / Programming / Python / PEP / 634-636 Structural Pattern Matching
Tags: automatedNewLineAfterCommaSpace

Name: The subject
Definition: `foo`, the part after `match`
Page: https://www.python.org/dev/peps/pep-0343/
Variables: match foo:
    case bar:
        block
Context: Python match

Name: A pattern
Definition: `bar`, the part after `case`
Page: https://www.python.org/dev/peps/pep-0343/
Variables: match foo:
    case bar:
        block
Context: Python match

Name: Matching sub against pat
Definition: The process by which the variables of pat are assigned depending on components of sub
Page: https://www.python.org/dev/peps/pep-0343/
Variables: pat a pattern
sub a subject
Context: Python match

Instruction: match subject:
  case pat_1:
    block_1
  ..
  case patn:
    block_n
Page: https://www.python.org/dev/peps/pep-0343/
Effect: let i the smallest value such that subject matches against pat_i. Then execute block_i
Language: Python
Context: match
Name: structural pattern matching

Instruction: some_literal
Page: https://www.python.org/dev/peps/pep-0636/
Context: Pattern matching
Returns: a pattern matched with `is` if it is True, False or None. Otherwise with ==
Name: Literal pattern

Instruction: a variable
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: A pattern, always succeeding, with the value assigned to the variable
Name: Capture pattern

Instruction: [pat_1, ..., pat_n]
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: A pattern for sequence of n elements, with i-th element matched against pat_i

Instruction: [pat_1, ..., pat_n, *args]
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: A pattern for sequence of at least n elements, with i-th element matched against pat_i. The remaining elements are put in *args

Instruction: _
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: A pattern matching against anything, the value matched is not binded to anything
Name: Wilcard pattern

Instruction: pat_1 | pat_2
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: A pattern where a value matches if it matches either with pat_1 or pat_2
Assuming: pat_1 and pat_2 must have the same variable
Name: or pattern

Instruction: pat as var
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: A pattern, matching similarly as pat, and the matched expression is binded to var
Name: as pattern

Instruction: where foo
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: The pattern matches only if foo holds
Where to use it: after a pattern
Name: guard

Instruction: ClassName(
  attr1=pat_1,
  ...,
  attr_n=pat_n
)
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: Pattern matches with value of type ClassName having attributes attr_i matching against pat_i

Instruction: ClassName(
  pat_1,
  ...,
  pat_n
)
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: Pattern matches with value of type ClassName, whose attribute ClassName.__match_args__[i] match against pat_i
Intuition: Match against ClassName and named tuple automatically. Indeed,
  they have __match_args__

Question: How to match constants and enum value
Answer: Use a qualified name, so that the name of the constant is not interpreted as a capture pattern
Page: https://www.python.org/dev/peps/pep-0636/
Context: python pattern

Instruction: {
  "key_1": pat_1,
  ...
  "key_n": pat_n,
  **kwargs
}
Page: https://www.python.org/dev/peps/pep-0636/
Language: python
Context: pattern
Returns: pattern that matches against map having key_i whose value matches against pat_i. Other values are put in kwargs

Page: https://www.python.org/dev/peps/pep-0636/
Syntactic sugar: {
  "key_1": pat_1,
  ...
  "key_n": pat_n
}
Implementation: {
  "key_1": pat_1,
  ...
  "key_n": pat_n,
  **fresh_variable
}
Language: python
Context: pattern

Question: how to create a pattern that check that the value is of type T and bind the value to a variable
Answer: str(var) int(var) ... for most built-in type
Page: https://www.python.org/dev/peps/pep-0636/
Context: python pattern
Answer2: Class() as var

Question: What occurs in case of partial pattern matching
Answer: the partial binding may be kept or not depending on implementation
Page: https://www.python.org/dev/peps/pep-0634/
Context: python pattern

Name: An irrefutable pattern
Definition: A pattern that always succeed
Page: https://www.python.org/dev/peps/pep-0634/
Context: python pattern
Example: _
Example2: A variable

Question: Constraint on variable in pattern
Answer: each variable that appears in a branch of a OR-pattern appears in all other branches
Page: https://www.python.org/dev/peps/pep-0634/
Context: python pattern
Answer2: Each variable occurs at most once in a pattern, except in the previous case

Instruction: (pattern)
Page: https://www.python.org/dev/peps/pep-0634/
Language: python
Context: pattern
Returns: same as pattern
Name: group_pattern