• Stars
    star
    585
  • Rank 76,419 (Top 2 %)
  • Language SystemVerilog
  • License
    Other
  • Created over 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Proving leftpad correct in a dozen different ways

Let's Prove Leftpad

This is a repository of provably-correct versions of Leftpad.

What is "provably-correct"?

Provably correct code is code that you can totally guarantee does what you say it does. You do this by providing a proof that a computer can check. If the proof is wrong, the code won't compile.

Compare to something like testing: even if you test your function for 1,000 different inputs, you still don't know for sure that the 1,001st test will pass. With a proof, though, you know your function will work for all inputs, regardless of whether you try a thousand or ten trillion different test cases. Proving code correct is really, really powerful. It's also mindbogglingly hard, which is why most programmers don't do it.

This is a sampler of all the different tools we can use to prove code correct, standardized by all being proofs for Leftpad.

What is "leftpad"?

Leftpad is a function that takes a character, a length, and a string, and pads the string to that length. It pads it by adding the character to the left. So it's adding padding on the left. Leftpad.

>> leftpad('!', 5, "foo")
!!foo
>> leftpad('!', 0, "foo")
foo

Why are we proving leftpad?

Because it's funny.

And because leftpad is a great demo for different proof techniques. The idea is simple, the implementation is simple, but the specification (what you actually, formally want it to do) is surprisingly tricky. Specifically, you need to prove things for it to be leftpad:

  1. The length of the output is max(n, len(str))
  2. The prefix of the output is padding characters and nothing but padding characters
  3. The suffix of the output is the original string.

A proof of leftpad is going to be small enough to be (mostly) grokkable by Formal Methods outsiders, while being complex enough to differentiate the ways we prove code correct.

I want to contribute!

We'd love to have you! Please read the contribution guidelines, and then submit your favorite proofs!

Plug

If you want to learn more about formal methods, I shout at clouds on my website and on twitter.

More Repositories

1

awesome-cold-showers

For when people get too hyped up about things
6,601
star
2

learntla

A TLA+ guide
CSS
278
star
3

autohotkey-scripts

Some of my AutoHotKey scripts
AutoHotkey
196
star
4

learntla-v2

Learn TLA+ for free! No prior experience necessary!
TLA
144
star
5

tlacli

A script for running TLA+/TLC from the command line
Python
77
star
6

alloydocs

Proposed documentation for alloytools.org
Python
64
star
7

hacker-test-history

Let's explain all the hacker test questions!
43
star
8

aws-lambda-send-to-slack

A quick lambda script that forwards sns messages to slack
JavaScript
34
star
9

tla.vim

Vim plugin for TLA+ and PlusCal
Vim Script
29
star
10

tla-graphing-demo

A demo of analyzing a TLA+ state graph
Python
27
star
11

tla-snippets

A collection of useful TLA+ operators
TLA
21
star
12

gpt-tricks

A collection of useful uses of GPT (and other LLMs), organized as examples
16
star
13

safehouse

Ostensibly a "scale-invariant" "headless" "developer-targeted" thingimajig, actually a mental health tool.
Python
15
star
14

dotfiles

Vim Script
8
star
15

sphinx-github-action-test

A quick repo for testing compiling a sphinx doc and syncing it with S3
3
star
16

tutor

Helps you with math! If you get a problem wrong, tells you why and how you got it wrong.
Python
2
star
17

tla-pygments

A terrible pygments plugin from a terrible human
Python
2
star
18

bad-ideas

Simple problems, terrible solutions.
Shell
1
star
19

wordalyzer

Python
1
star
20

knacks

Python
1
star
21

xmlaatot

XML as a Tool of Thought
1
star
22

rsl

A turing tarpit with multiple registers!
Python
1
star
23

smlcalc

A programmable calculator written in sml/nj.
Standard ML
1
star
24

vim-pivot

Lets you swap text elements around a pivot, eg (a,b) -> (b,a)
Vim Script
1
star
25

minotaur

A Python implementation of Robert Abbott's amazing Theseus and the Minotaur game.
Python
1
star