Skip to content

Instantly share code, notes, and snippets.

View Kazark's full-sized avatar

Keith Pinson Kazark

  • Undisclosed
View GitHub Profile
@Kazark
Kazark / Bijection.idr
Last active May 4, 2020 21:13
Basic implementation of correct-by-construction bijections
module Bijection
%default total
%access public export
infixr 5 <=>
record (<=>) a p where
constructor Biject
ap : a -> p
@Kazark
Kazark / main.c
Created April 16, 2020 01:32
Usable Lisp-style symbols, without a global symbol table or interning or heavyweight strings
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct MaybeULong {
const unsigned long nothing;
const unsigned long x;
} MaybeULong;
@Kazark
Kazark / .bashrc
Created December 12, 2019 16:37
Bash Post Git PROMPT_COMMAND
export PROMPT_COMMAND="__posh_git_ps1 '${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w' '\[\033[34m\]\$\[\033[00m\] '"
@Kazark
Kazark / AndOr.idr
Last active July 12, 2019 13:51
Sigma+Nat suffice for ADTs
module AndOr
%default total
%access private
TData : Type -> Type -> Nat -> Type
TData a _ Z = a
TData a b (S Z) = (a, b)
TData _ b (S (S Z)) = b
TData _ _ _ = Void
@Kazark
Kazark / Guard.idr
Last active October 25, 2019 01:02
Who needs constructivism? Make types out of Boolean predicates! (Encoded constructively...)
module Guard
%default total
%access public export
data Guarded : (a -> Bool) -> a -> Type where
Guard : (pred : a -> Bool) -> (subject : a) -> (prf : pred subject = True)
-> Guarded pred subject
data GuardedIsBogus : Type where
@Kazark
Kazark / Id.agda
Created March 14, 2019 15:39
Identity functor and monad laws
module Id where
open import Relation.Binary.PropositionalEquality.Core
open import Function
data Id : Set → Set where
Itself : ∀{A} → A → Id A
fmap : ∀{A B} → (A → B) → Id A → Id B
fmap f (Itself x) = Itself (f x)
@Kazark
Kazark / ThisOrThese.agda
Created March 11, 2019 21:48
Like either but applicative instance accumulates lefts
module Data.ThisOrThese where
open import Data.List.Base
open import Data.List.NonEmpty
open import Relation.Binary.PropositionalEquality.Core
open import Function
-- Apparently this is called ValidatedNel in Cats.
data ThisOrThese : Set -> Set -> Set where
These : ∀ {A B} -> List⁺ A -> ThisOrThese A B
@Kazark
Kazark / Env.fsx
Created March 6, 2019 21:43
An F# toy example of Scala ZIO Environment
module EnvDemo
open System
open System.IO
[<Struct>]
type Nothing =
private
| Nothing
@Kazark
Kazark / Maybe.cs
Last active October 25, 2019 01:05
Maybe monad encoding in C#
class Nothing : Exception {}
public static class Maybe {
public static void Nothing() => throw new Nothing();
public static void Guard(bool cond) { if (!cond) Nothing(); }
public static void GuardNot(bool cond) { if (cond) Nothing(); }
}
public static class TryParse {
public static UInt32 UInt32(string x) {
@Kazark
Kazark / GADT.fsx
Last active February 14, 2019 20:12
Generalized Algebra Data Types for DSLs, with Free Monads, in F#
/// Example encoding of the shape of GADT one finds for request types
module Request =
/// The constructors must be kept private, so that we can refine the types of
/// the introduction rules.
type T<'a> =
private
| FileExists' of string
| ReadText' of string
| GetCwd'