| title | writing correct programs |
|---|---|
| author | Kristoffel Pirard |
In C++ of course
Common theme: 'correct' programs
- keynote Lisa Lippincot: 'truth' of a procedure
- talk: Mateusz Pusz: Concepts/Ranges
- talk: Phil Nash: Option(al) is not a Failure (video cppcon)
- secret lightning talk: Dan Saks: Reframing our Craft as a Discipline
(Lisa Lippincot)
- goal: 'automated' verification
- add a layer:
interfacevs.implementation interfacecontains pre/postconditionsinterfacecallsimplementation
- add a layer:
--
template<class F> auto fib(int a, int b, F yield) {
if(yield(a)) return;
if(yield(b)) return;
while(true) {
auto next = a + b;
if(yield(next)) return;
std::tie(a, b) = std::tie(b, next);
}
}
--
namespace checked {
template<class F> auto fib(int a, int b, F yield) {
/// prologue(
/// claim a > 0,
/// claim b > 0,
/// claim(x > maxint/2) => !yield(x)
/// );
::fib(a, b, yield);
/// epilogue();
}
}
--
- Common voc => better understanding
- Learn from logicians => reuse proven material!
- From task-locally towards thinking function-locally
- We forget the edge cases (the devil in the details)
- Does this function always end?
note: "theorem" <-> program: cf. Dan Saks
--
Emphasize the importance of
- the border between "us" and "them"
- clear responsibility
- scope often becomes smaller
- the design process
- forgiving on your input
- strict on your output
--
Logic as a science is cumbersome to me. Lots of funny words and symbols, lots of 'famous' scientists with proven theory are lost to us (Incompleteness, CAP, Curry-Howard, ...)
(Mateusz Pusz)
Way too much encyclopedic knowledge on one talk. Too good to throw away, though. Cppcon 2018: Stroustrup talk
Here: just touching "Concepts"
--
"Named sets of requirements on a type"
Primary goal: Semantic vs. syntactical info
--
Library dev: can express expectations explicitly
template<Monoid T>
auto sum(vector<T> ms) {...}
--
Application dev: gets a 'real' error message
s = sum(ds);
Decent error message:
$ g++-8 -std=gnu++2a -fconcepts concepts.cxx
constraints not satisfied
auto sum(std::vector<T> ms) {
^~~
within ‘template<class T> concept const bool
Monoid<T> [with T= Distance]’
template<typename T> concept bool Monoid =
^~~~~~
...
the required expression ‘T::identity()’ would be ill-formed
--
- compiler does some 'concept' check
- ~= automated 'prologue' verification
(Phil Nash)
- finally an evaluation method
- 50% of devs: no exceptions!
- Sum types to the rescue!
--
"Sum" type: contains the union of all possible values of other types.
- card (int + bool) = 32484 + 2
- card (char + int + bool) = 256 + 32484 + 2
- card (my_type + {nothing}) = card my_type + 1
optional<int> i = parse("abcd");
optional<int> ii = i ? optional<int>(*i * *i) : nullopt;
--
- With a "Score Card"
- overhead happy path
- overhead error path
- safety (discardable)
- noise
- separate paths
- reasonability
- composability
- message
- Still a bit subjective
--
if (create_dir("images") || errno == E_EXISTS) {
// happy path
} else {
// error path
}
--
create_dir("images");
if (errno == 0 || errno == E_EXISTS) {
// happy path
} else {
// error path
}
--
try {
create_dir("images");
// happy path
} catch(const exception &e) {
// error path
}
--
auto d = create_dir("images");
if (d) {
happy(*d);
} else {
cerr << d.error();
}
--
auto d = create_dir("images")
| [](auto d) { return happy(d); }
| [](auto f) { return stuffwith(f); }
;
--
<style> .reveal table { font-size: .6em; } </style>| errcode | try/catch | optional | expected | monad | |
|---|---|---|---|---|---|
| happy | 9 | 10 | 8 | 8 | 8 |
| error | 9 | 1 | 8 | 8 | 8 |
| safety | 3 | 6 | 6 | 7 | 9 |
| noise | 3 | 8 | 1 | 2 | 5 |
| separate | 1 | 10 | 1 | 1 | 8 |
| reason | 8 | 5 | 10 | 10 | 10 |
| compose | 3 | 9 | 5 | 5 | 10 |
| message | 1 | 10 | 10 | 10 | 10 |
| TOTAL | 37 | 59 | 49 | 51 | 68 |
(Dan Saks)
How we can barely call ourselves "engineers" let alone "scientists"
Cf. the tabs/spaces, vim/emacs, C++/D/Rust/C... discussions
TODO:
- arguing/losing
- we want it to be a discipline ... because?
- we can use Linguistic Framing to achieve this
--
--
Gut feeling is valuable when experimenting!
We abuse the word "theory" for "gut feeling"
"In theory, this could happen" means: it's not going to matter.
--
That's science!
We can save time & effort by taking conscious decisions