Skip to content

Instantly share code, notes, and snippets.

@hwayne
hwayne / fifo.tla
Created March 16, 2018 02:43
Bounded Fifo Spec
------------------------------ MODULE server_2 ------------------------------
EXTENDS TLC, Integers, Sequences
CONSTANTS MaxQueueSize
(*--algorithm message_queue
variable queue = <<>>;
define
BoundedQueue == Len(queue) <= MaxQueueSize
@hwayne
hwayne / example.py
Created March 16, 2018 23:39
Demos of contracts
from dpcontracts import require, ensure
# https://github.com/deadpixi/contracts
# with a regular postcondition
@ensure("List is sorted", lambda a, r: all((x <= y for (x, y) in zip(r, r[1:]))))
def bubble_sort(arr):
n = len(arr)
new_arr = arr[:]
for i in range(n):
for j in range(0, n-i-1):
@hwayne
hwayne / Unique.dfy
Last active April 11, 2018 20:31
Dafny
method Unique(a: seq<int>) returns (b: seq<int>)
ensures forall k :: 0 <= k < |a| ==> a[k] in b
ensures forall i, j :: 0 <= i < j < |b| ==> b[i] != b[j]
{
var index := 0;
b := [];
while index < |a|
invariant index <= |a|
invariant forall i, j :: 0 <= i < j < |b| ==> b[i] != b[j]
invariant forall k :: 0 <= k < index ==> a[k] in b
Snapshot
logout -> Login Page
Reports
summary -> Summary
students -> Students
standards -> Standards
Summary*
Students
answer -> Answers
Standards
Snapshot
logout -> Login Page
Reports
summary -> Summary
students -> Students
standards -> Standards
Summary*
Students
answer -> Answers
Standards
@hwayne
hwayne / binarysearch.dfy
Created October 29, 2018 16:22
Binarysearch
datatype Option<T> = None | Some(x: T)
method BinarySearch(haystack: array<int>, needle: int) returns (n: Option<int>)
requires forall i, j :: 0 <= i <= j < haystack.Length ==> haystack[i] <= haystack[j]
ensures n.Some? ==>
0 <= n.x < haystack.Length && haystack[n.x] == needle
ensures n.None? ==>
forall i :: 0 <= i < haystack.Length ==> haystack[i] != needle
//This is the one that Daniel Jackson wrote
abstract sig Node {inputs: set Node}
sig Source extends Node {} {no inputs}
sig Sink extends Node {} {one inputs}
abstract sig Gate extends Node {}
sig And, Or, Xor extends Gate {} {#inputs = 2}
sig Not extends Gate {} {one inputs}
@hwayne
hwayne / properties.pm
Created March 8, 2019 17:51
PRISM example
const int total_dropped;
S =? [ dropped = total_dropped]
P =? [ (F queue = MAX) & (F "done" & dropped = total_dropped)]
@hwayne
hwayne / decisiontables.md
Last active April 27, 2019 04:52
Gilded Rose Kata

Constraints:

  • At most one of sulfuras?, pass?, and brie? are true

Assuming whenever they say "increases or decreases" without a qualifier, they mean 1. This is a BIG assumption (it's not specified).

sulfuras? pass? brie? days_left change
T - - - 0
F T - 11- +1
@hwayne
hwayne / snippet thingy
Created May 3, 2019 18:22
Language snippet
proc write(key: str, value: T) modifies store {
store[key] := value;
}
fun read(key: str) returns (out:T) {
out := store[key];
}
// ....