Skip to content

Instantly share code, notes, and snippets.

@rntz
rntz / Reverse.agda
Last active August 29, 2018 22:41
Failing to prove that reversing a list reverses its order in Agda
open import Level renaming (zero to lzero; suc to lsuc)
open import Relation.Binary
open import Data.List
open import Data.List.Properties
open import Data.Unit hiding (_≤_)
open import Data.Product
open import Relation.Binary.PropositionalEquality
open import Data.Fin
open import Data.Nat as ℕ hiding (_≤_)
@rntz
rntz / makeAgdaDepGraph.sh
Created August 23, 2018 12:23
Shell & sed script to generate a graphviz file with agda dependencies
#!/bin/bash
set -e
echo "digraph {"
# Generate the node names.
find . -iname '*.agda' '!' -name '.*' |
sed -Ee 's,^\./,,;s,.agda$,,;s,/,.,g;s/.*/"&" [label="&"];/'
# Generate edges.
find . -iname '*.agda' '!' -name '.*' |
@rntz
rntz / bidirectional-structural.ml
Created July 26, 2018 14:44
The bidirectionally typed lambda calculus in OCaml using structural types
(* A bidirectionally-typed λ-calculus looks like:
types A,B ∷= base | A → B
terms M ∷= E | λx.M
exprs E ∷= x | E M | (M : A)
This is an exploration of how to encode this in OCaml using _structural_
typing (polymorphic variants & equirecursive types), rather than _nominal_
typing. *)
@rntz
rntz / four-lambda-calculi.ml
Last active July 25, 2018 12:56
Four variants of the lambda calculus, structurally typed in OCaml
type var = string
type tp = Base | Fn of tp * tp
(* Normal & neutral term formers. *)
type ('norm, 'neut) neutF
= [ `Var of var
| `App of 'neut * 'norm ]
type ('norm, 'neut) normF =
[ ('norm, 'neut) neutF
| `Lam of var * 'norm ]
@rntz
rntz / bidirectional-structural.ml
Created July 25, 2018 09:40
Trying to encode a bidirectional lambda-calculus in OCaml with structural types
(* A bidirectionally-typed λ-calculus looks like:
types A,B ∷= base | A → B
terms M ∷= E | λx.M
exprs E ∷= x | E M | (M : A)
This is an exploration of how to encode this in OCaml using _structural_
typing (polymorphic variants & equirecursive types), rather than _nominal_
typing. As you'll see, it's not entirely satisfying. *)
{-# LANGUAGE FlexibleInstances #-}
module MiniToneInference where
import Prelude hiding ((&&))
import Control.Applicative
import Control.Monad
import Data.Map.Strict (Map, (!))
import Data.Maybe (fromJust)
import Data.Monoid ((<>))
import Data.Set (isSubsetOf)
@rntz
rntz / tone-inference.rkt
Last active June 28, 2018 12:06
Inferring monotonicity with modal types for a simple lambda-calculus
#lang racket
(require racket/hash rackunit)
;;; ---------- TONES and TONE CONTEXTS ---------- ;;;
(define tone? (or/c 'id 'op 'iso 'path))
(define (tone<=? a b)
(match* (a b)
[('iso _) #t] [(_ 'path) #t]
[(x x) #t]
@rntz
rntz / Fulcrum.agda
Last active April 23, 2018 13:03
verified fulcrum in agda
module Fulcrum where
open import Data.Integer as ℤ
open import Data.List hiding (sum)
open import Data.List.Any; open Membership-≡
open import Data.Nat as ℕ using (ℕ; _∸_)
open import Data.Product
open import Data.Sum
open import Relation.Binary
open import Relation.Binary.PropositionalEquality
module Unique where
-- I deal with naturals instead of integers. with a bit more effort, I could
-- probably have parameterized this over any type with decidable equality.
open import Data.Nat
open import Data.Product
open import Data.List hiding (any)
open import Data.List.Any
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
@rntz
rntz / leftpad.agda
Created April 21, 2018 21:10
verified left-pad in agda
module HillelVerificationProblems where
open import Data.Nat
open import Data.Nat.Properties.Simple using (+-right-identity; +-comm)
open import Data.List
open import Data.List.All
open import Data.List.Properties
open import Relation.Binary.PropositionalEquality
-- NB. (a ∸ b) is saturating subtraction and (a ⊔ b) is max(a,b).