Warning: Software development, Computer Science, and Type Theory don't agree on what stuff means at time
Source: Wikipedia: https://en.wikipedia.org/wiki/Algebraic_data_type
In computer programming, especially functional programming and type theory, an algebraic data type is a kind of composite type, i.e., a type formed by combining other types.
Source: Wikipedia: https://en.wikipedia.org/wiki/Algebraic_data_type
In computer programming, especially functional programming and type theory, an algebraic data type is a kind of composite type, i.e., a type formed by combining other types.
Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e., tagged or disjoint unions, coproduct types or variant types).
Source: Wikipedia: https://en.wikipedia.org/wiki/Algebraic_data_type
Product types allow you to have more than one value in a single structure, at the same time.
<?php
class BoolTuple2 {
public bool $boolean1;
public bool $boolean2;
public function __construct(bool $boolean1, bool $boolean2) {
$this->boolean1 = $boolean1;
$this->boolean2 = $boolean2;
}
}
<?php
class BoolTuple2 {
public function __construct(public bool $boolean1, public bool $boolean2) {}
}
<?php
class BoolTuple2 {
public function __construct(public bool $boolean1, public bool $boolean2) {}
}
<?php
class BoolTuple2 {
public function __construct(public bool $boolean1, public bool $boolean2) {}
}
<?php
$trueTrue = new BoolTuple2(true, true);
$trueFalse = new BoolTuple2(true, false);
$falseTrue = new BoolTuple2(false, true);
$falseFalse = new BoolTuple2(false, false);
<?php
class BoolTuple2 {
public function __construct(public bool $boolean1, public bool $boolean2) {}
}
<?php
class Digit {
public int $digit;
public function __construct(int $digit) {
if ($digit < 0 || $digit > 9) {
throw new ValueError('Value must be a digit');
}
$this->digit = $digit;
}
}
class Composite {
public function __construct(public BoolTuple2 $boolTuple2, public Digit $digit) {}
}
<?php
class Digit {
public int $digit;
public function __construct(int $digit) {
if ($digit < 0 || $digit > 9) {
throw new ValueError('Value must be a digit');
}
$this->digit = $digit;
}
}
class Composite {
public function __construct(public BoolTuple2 $boolTuple2, public Digit $digit) {}
}
<?php
class Digit {
public int $digit;
public function __construct(int $digit) {
if ($digit < 0 || $digit > 9) {
throw new ValueError('Value must be a digit');
}
$this->digit = $digit;
}
}
class Composite {
public function __construct(public BoolTuple2 $boolTuple2, public Digit $digit) {}
}
<?php
class Digit {
public int $digit;
public function __construct(int $digit) {
if ($digit < 0 || $digit > 9) {
throw new ValueError('Value must be a digit');
}
$this->digit = $digit;
}
}
class Composite {
public function __construct(public BoolTuple2 $boolTuple2, public Digit $digit) {}
}
<?php
class Digit {
public int $digit;
public function __construct(int $digit) {
if ($digit < 0 || $digit > 9) {
throw new ValueError('Value must be a digit');
}
$this->digit = $digit;
}
}
class Composite {
public function __construct(public BoolTuple2 $boolTuple2, public Digit $digit) {}
}
<?php
class Digit {
public int $digit;
public function __construct(int $digit) {
if ($digit < 0 || $digit > 9) {
throw new ValueError('Value must be a digit');
}
$this->digit = $digit;
}
}
class Composite {
public function __construct(public BoolTuple2 $boolTuple2, public Digit $digit) {}
}
Sum types are types where your value must be one of a fixed set of options.
<?php
class BoolOrDigit {
public function __construct(public bool|Digit $boolOrDigit) {}
}
<?php
class BoolOrDigit {
public function __construct(public bool|Digit $boolOrDigit) {}
}
<?php
class BoolOrDigit {
public function __construct(public bool|Digit $boolOrDigit) {}
}
<?php
class BoolOrDigit {
public function __construct(public bool|Digit $boolOrDigit) {}
}
<?php
class BoolOrDigit {
public function __construct(public bool|Digit $boolOrDigit) {}
}
Source: Wikipedia: https://en.wikipedia.org/wiki/Pattern_matching
In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern.
Source: Wikipedia: https://en.wikipedia.org/wiki/Pattern_matching
In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituents of some pattern.
Source: Wikipedia: https://en.wikipedia.org/wiki/Pattern_matching
The patterns generally have the form of either sequences or tree structures.
<?php
class Nil {}
class Leaf {
public function __construct(public int $value) {}
}
class Node {
public function __construct(public Tree $left, public Tree $right) {}
}
class Tree {
public function __construct(public Nil|Leaf|Node $val) {}
}
<?php
class Nil {}
class Leaf {
public function __construct(public int $value) {}
}
class Node {
public function __construct(public Tree $left, public Tree $right) {}
}
class Tree {
public function __construct(public Nil|Leaf|Node $val) {}
}
function depth(Tree $tree): int {
$val = $tree->val;
return match ($val::class) {
Nil::class => 0,
Leaf::class => 1,
Node::class => 1 + max(depth($val->left), depth($val->right)),
};
}
<?php
$treeExample = new Tree(new Node(
new Tree(new Leaf(5)),
new Tree(new Node(
new Tree(new Node(
new Tree(new Nil()),
new Tree(new Leaf(2)),
)),
new Tree(new Leaf(6)),
))
));
print depth($treeExample);
<?php
$treeExample = new Tree(new Node(
new Tree(new Leaf(5)),
new Tree(new Node(
new Tree(new Node(
new Tree(new Nil()),
new Tree(new Leaf(2)),
)),
new Tree(new Leaf(6)),
))
));
print depth($treeExample);
5
2
6
<?php
$treeExample = new Tree(new Node(
new Tree(new Leaf(5)),
new Tree(new Node(
new Tree(new Node(
new Tree(new Nil()),
new Tree(new Leaf(2)),
)),
new Tree(new Leaf(6)),
))
));
print depth($treeExample);
5
2
6
4
<?php
$treeExample = new Tree(new Node(
new Tree(new Leaf(5)),
new Tree(new Node(
new Tree(new Node(
new Tree(new Nil()),
new Tree(new Leaf(2)),
)),
new Tree(new Leaf(6)),
))
));
print depth($treeExample);
5
2
6
4
Algebraic Data Types: Things I wish someone had explained about functional programming
Blog post by James Sinclair
Thinking Functionally in PHP
Book by Larry Garfield
ย