Skip to content

Commit

Permalink
Start on API protocols in indexed codata
Browse files Browse the repository at this point in the history
  • Loading branch information
noelwelsh committed Aug 7, 2024
1 parent 89e0d71 commit 54175e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pages/indexed-types/codata.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ The constraint is made of two parts: using clauses, which we learned about in [@

### API Protocols

An API protocol defines the order in which methods on codata must be called. In the case of `Switch`, the protocol is we can only call `off` after calling `on` and vice versa. This protocol is illustrated in Figure [@img:indexed-types:switch]. Many common types have similar protocols. For example, files can only be read once they are opened and cannot be read once they have been closed.

![The switch API protocol](src/pages/indexed-types/switch.pdf) {#img:indexed-types:switch}

Indexed codata allows us to enforce API protocols at compile-time.
19 changes: 19 additions & 0 deletions src/pages/indexed-types/switch.gv
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Disable for switch FSM
//
// run with
// dot switch.gv -Tpdf -o switch.pdf

digraph switch_fsm {
rankdir="LR";
ratio=0.25;

node [shape = circle];

On[label="On"];
Off[label="Off"];

On->Off [label="off"];
// Make the two visible edges symmetric by inserting this invisible edge between them
On->Off [style=invis];
Off->On [label="on"];
}
Binary file added src/pages/indexed-types/switch.pdf
Binary file not shown.

0 comments on commit 54175e0

Please sign in to comment.