Functional Design Explained - David Sankel - CppCon 2015
Functional Design Explained - David Sankel - CppCon 2015
Functional Design Explained - David Sankel - CppCon 2015
Design
Explained
David Sankel - Stellar Science
[email protected]
CppCon 2015
1
Math Engineering
2
Quiz
Are the following two C++ programs the same?
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
std::cout << “Hello World\n”; std:: cout << “Hello World\n”;
} }
3+2=5
5=3+2
int f( int c )
{
if( false )
return 45;
else
return c + 5;
}
⊥ is “bottom”
f(b+2c) + f(2b-c)
where f(x) = x(x+a)
0.6
0.4
0.2
0
Haskell C++
struct Zero {
Zero() = delete;
};
struct AAndB {
A a;
B b;
};
struct AAndB {
std::unique_ptr<A> a;
std::unique_ptr<B> b;
};
struct AOrB {
bool hasA;
union {
A a;
B b;
} contents; // ‘a’ when ‘hasA==true’
// otherwise ‘b’.
};
μ⟦ expression ⟧ : mathExpression
The type of “expression” is the math expression
μ⟦ int ⟧ = ℤ
μ⟦ 3 ⟧ : ℤ
μ⟦ 3 ⟧ = 3
μ⟦ std::pair<e₁,e₂> ⟧ = μ⟦ e₁ ⟧ ⊗ μ⟦ e₂ ⟧
μ⟦ double ⟧ = ℝ
or maybe
μ⟦ double ⟧ = ℝ ⊕ 1⊕ 1⊕ 1
where the extra states are -∞, +∞, and NaN
Operations:
μ⟦ always<e> ⟧ : μ⟦ e ⟧ → μ⟦ Movie<e> ⟧
μ⟦ always<e>(a) ⟧ = λ t. μ⟦ a ⟧
μ⟦ snapshot<e> ⟧ : μ⟦Movie<e>⟧ → ℝ → A
μ⟦ snapshot<e>(movie, time) ⟧ = μ⟦ movie ⟧ ( μ⟦ time ⟧ )
μ⟦ timeMovie ⟧ : μ⟦ Movie<double> ⟧
μ⟦ timeMovie ⟧ = λ t. t
μ⟦ sink<e> ⟧ = μ⟦ e ⟧ → Action
μ⟦ source<e> ⟧ = (μ⟦ e ⟧ → Action) → Action
μ⟦ applyToSink<a,b> ⟧
: μ⟦ transform<a,b> ⟧ → μ⟦ sink<b> ⟧ → μ⟦ sink<a> ⟧
μ⟦ applyToSource<a,b> ⟧
: μ⟦ transform<a,b> ⟧ → μ⟦ source<a> ⟧ → μ⟦ source<b> ⟧
μ⟦ so >> t ⟧ = μ⟦ applyToSource<a,b> ⟧( t, so );
μ⟦ t >> si ⟧ = μ⟦ applyToSink<a,b> ⟧( t, si );
μ⟦ so >> si ⟧ = μ⟦ connect<t> ⟧( so, si );
Hrm…
μ⟦ Parser<a,b> ⟧ = ListOf μ⟦ a ⟧ → μ⟦ b ⟧
μ⟦ CommandLineProcessor<a> ⟧ = μ⟦ Parser<String,b> ⟧
struct CommandLineParse {
std::vector< boost::variant< HelpFlag, UserFlag > > globalFlags;
boost::variant< ListAccounts, ListJob > mode;
};
• Beautiful API’s
• Screaming Speed