Skip to content

Instantly share code, notes, and snippets.

View ghulette's full-sized avatar

Geoff Hulette ghulette

  • AWS S3-ARG
  • San Francisco, CA
View GitHub Profile
@ghulette
ghulette / ackermann.v
Last active March 17, 2022 23:31
Definition of the Ackermann-Peter function in Coq, as an inductive relation and also a fixpoint
(* Let's look at the Ackermann-Peter function, the canonical example of a
total computable function that is not primitive recursive. As such it should
be possible to write it as a computation in Gallina but the normal Fixpoint
termination check won't work.
The function can be defined case-wise:
A(0, n) = n+1
A(m+1, 0) = A(m, 1)
A(m+1, n+1) = A(m,A(m+1,n))
@ghulette
ghulette / conflict.rs
Last active August 8, 2021 16:03
How to fix: this won't type check because the From impl conflicts with the core reflexive instance
#[derive(Debug)]
pub struct Point<T> {
pub x: T,
pub y: T,
}
impl<T> Point<T> {
pub fn new(x: T, y: T) -> Self {
Point { x, y }
}
@ghulette
ghulette / Fair.cfg
Created July 23, 2021 00:12
Illustrating fairness in TLA
SPECIFICATION Spec
INVARIANT TypeInv
PROPERTY EventuallyC
(*
$ ocamlopt tail.ml
$ ./a.out 100000
length (tail call) executed in 0.000256s
length (while loop) executed in 0.000366s
length (recursive) executed in 0.001092s
$ ./a.out 100000000
length (tail call) executed in 0.255737s
length (while loop) executed in 0.272046s
length (recursive) died
Require Import ZArith.
Require Import Coq.Lists.List.
Require Import Omega.
Import ListNotations.
Local Open Scope Z_scope.
Inductive is_sum : list Z -> Z -> Prop :=
| is_sum_nil : is_sum [] 0
| is_sum_cons : forall y n ns, is_sum ns y -> is_sum (n::ns) (n + y).
(* nats = 0 :: List.map (fun x -> x+1) nats *)
type 'a lazylist =
| Nil
| Cons of 'a * (unit -> 'a lazylist)
let rec take n l =
match (n,l) with
| 0,_ -> []
| _,Nil -> []
@ghulette
ghulette / formula.ml
Last active December 19, 2017 19:16
Using CPS
type formula =
| False
| True
| Atom of string
| Not of formula
| And of formula * formula
| Or of formula * formula
let is_lit = function
| Atom _ | Not (Atom _) -> true
@ghulette
ghulette / Connectives.v
Created November 8, 2017 05:46
P /\ Q <-> ~(P -> ~Q)
Require Import Coq.Logic.Classical.
Theorem conj_iff :
forall P Q, P /\ Q <-> ~(P -> ~Q).
Proof.
split.
unfold not.
intros (HP,HQ) H.
apply H.
apply HP.
@ghulette
ghulette / args.ml
Created May 1, 2017 17:54
Labeled and optional arguments in OCaml, by example
open Printf
(* Labeled arguments, the easy way *)
let _ =
(* Declare a function with labeled arguments like this: *)
let f ~x ~y = x + y in
(* Type: f : x:int -> y:int -> int *)
(* Call it like this: *)
printf "%d\n" (f ~x:5 ~y:6);
@ghulette
ghulette / backtrace.ml
Created April 25, 2017 22:16
How to get a backtrace out of ocaml
(*******************************************************************************
Getting a backtrace out of ocaml is not completely obvious. You
have to compile the code with -g and then run with OCAMLRUNPARAM=b,
like this:
$ ocamlc -g -o test test.ml
$ OCAMLRUNPARAM=b ./test
backtrace_status: true
Failure("nth")