Skip to content

Instantly share code, notes, and snippets.

View siraben's full-sized avatar

Ben Siraphob siraben

View GitHub Profile
@siraben
siraben / vandy_covid.ipynb
Last active February 22, 2022 16:25
Web scraping Vanderbilt's COVID-19 dashboard
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@siraben
siraben / no_surject_12.v
Last active December 17, 2021 23:20
Constructive proof that there are no surjections from {1} to {1,2}
(* There are no surjections from {1} to {1,2} *)
Definition surjective {A B} (f : A -> B) := forall (b : B), exists (a : A), b = f a.
Definition One := unit.
Definition Two := bool.
Theorem no_surject_one_two : ~ exists (f : One -> Two), surjective f.
Proof.
@siraben
siraben / countable_choice_wikipedia.v
Created August 2, 2021 17:10
"countable choice" according to wikipedia
(* P is a family of nat-indexed sets of nat such that:
H: for all n, there is an m such that m ∈ P n
-----------
Then we show that there is a choice function f
*)
Theorem countable_choice_wikipedia
(P : nat -> (nat -> Prop)) (H : forall n, { m | P n m }) : { f | forall n, (exists m, P n m /\ f n = m)}.
Proof.
set (f := fun (n : nat) => let (m, Hm) := H n in m).
exists f.
@siraben
siraben / add_mod_assoc.v
Last active July 21, 2021 05:59
Proof that addition under modulo n is associative
From Coq.Arith Require Import PeanoNat.
Import Nat.
Require Import Lia.
Definition add_mod n i j :=
match i + j ?= n with
| Lt => i + j
| _ => i + j - n
end.
@siraben
siraben / futamura.v
Last active July 17, 2021 08:57
Futamura Projections in Coq
Require Import ssreflect ssrfun.
Module Futamura.
(* Program from a -> b written in L *)
Inductive Program {a b language : Type} :=
| mkProgram : (a -> b) -> Program.
Definition runProgram {A B L} (p : @Program A B L) := match p with mkProgram f => f end.
Notation "[ S | a >> b ]" := (@Program a b S) (at level 2) : type_scope.
@siraben
siraben / Verif_xor_swap.v
Last active July 16, 2021 05:16
Formal verification of XOR swap in C
Require Import VST.floyd.proofauto.
Require Import VST.floyd.library.
Require Import xor_swap.
Instance CompSpecs : compspecs. make_compspecs prog. Defined.
Definition Vprog : varspecs. mk_varspecs prog. Defined.
(* Specification of xor swap. In words:
Let a and b be unsigned int pointers to values n and m
respectively, with respective writable shares sh1 and sh2, and
(* Coq code for https://siraben.github.io/2021/06/27/classical-math-coq.html *)
Definition left_inverse {A B} (f : A -> B) g := forall a, g (f a) = a.
Definition right_inverse {A B} (f : A -> B) g := forall b, f (g b) = b.
Module NaiveInjective.
Definition injective {A B} (f : A -> B) :=
forall a' a'', a' <> a'' -> f a' <> f a''.
@siraben
siraben / asm.v
Last active July 7, 2021 08:23
Verifying factorial for a simple assembly language
Require Import Arith.PeanoNat.
Require Import List.
Import Nat.
Import ListNotations.
(** * Reflexive transitive closure of a relation *)
Definition relation (X : Type) := X -> X -> Prop.
Inductive multi {X : Type} (R : relation X) : relation X :=
| multi_refl : forall (x : X), multi R x x
| multi_step : forall (x y z : X),
@siraben
siraben / Verif_factorial.v
Last active June 14, 2021 07:17
Verifying factorial in C with Coq
Require Import VST.floyd.proofauto.
Require Import VST.floyd.library.
Require Import factorial.
Instance CompSpecs : compspecs. make_compspecs prog. Defined.
Definition Vprog : varspecs. mk_varspecs prog. Defined.
(* First we right down a specification of factorial in Coq that
operates on integers. We need to prove that it terminates as well. *)
Function factorial (n: Z) { measure Z.to_nat n } :=
if Z_gt_dec n 0 then n * factorial (n-1) else 1.
@siraben
siraben / church-rosser.v
Last active May 19, 2021 06:55
Mechanized proof of the Church-Rosser theorem
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
Require Import Arith.
Import Nat.
Require Import Lia.
(** * Reflexive transitive closure of a relation *)
Definition relation (X : Type) := X -> X -> Prop.
Inductive multi {X : Type} (R : relation X) : relation X :=
| multi_refl : forall (x : X), multi R x x
| multi_step : forall (x y z : X),