Skip to content

Instantly share code, notes, and snippets.

View siraben's full-sized avatar

Ben Siraphob siraben

View GitHub Profile
@siraben
siraben / list_monad.v
Created June 25, 2019 16:16
Formalized Coq proof that lists form a semigroup, monoid, functor and monad.
Require Import List.
Import ListNotations.
(** A semigroup consists of an total associative operation. **)
Inductive semigroup (A : Type) (Op : A -> A -> A) : Prop :=
| semigroup_intro :
(forall (a b c : A), Op a (Op b c) = Op (Op a b) c) -> semigroup A Op.
(** Proof that lists with append form a semigroup. **)
Theorem list_semigroup :
@siraben
siraben / monad-composition.hs
Created October 9, 2019 22:13
Composition of monads in Haskell, categorically.
import Control.Monad
-- Replace T and S with any types that have monad instances.
type T a = [a]
type S a = Maybe a
-- But you must provide the l function (with the following type) to
-- make the rest of the code work.
l :: T (S a) -> S (T a)
l x = etaS (do {Just v <- x; return v})
@siraben
siraben / monad-transformers.hs
Last active October 16, 2019 21:17
Monad transformers à la Richard Bird's "Thinking Functionally with Haskell" (2nd edition)
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
data Term
= Con Integer
| Div Term Term
deriving (Show, Eq)
data Exc e a
@siraben
siraben / ALaCarte.hs
Created October 29, 2019 21:04
Code from the paper "Data types à la carte" (Swierstra 2008)
{-# LANGUAGE FlexibleInstances, OverlappingInstances #-}
{-# LANGUAGE UndecidableInstances, MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DeriveFunctor #-}
module ALaCarte where
import qualified Prelude (getChar, getLine, putChar, readFile, writeFile)
import Prelude hiding (getChar, getLine, putChar, readFile, writeFile)
@siraben
siraben / karplus.hs
Last active November 1, 2019 23:29
Karplus-Strong in Haskell
{-# LANGUAGE FlexibleContexts #-}
-- Karplus-Strong using infinite lists
import Control.Monad
import Data.Array.IO
import Data.List
import Foreign.C.Types
import System.Environment
import System.Exit
import System.IO
@siraben
siraben / classy.hs
Last active November 20, 2019 22:24
Annotating Ben Lynn's mini Haskell compiler
------------------------------------------------------------------------
-- A mini Haskell compiler with typeclasses.
-- Originally written by Ben Lynn, modified by Ben Siraphob
------------------------------------------------------------------------
{-
Ideas for improvements (listed rough order of difficulty for each
section)
C runtime
=========
@siraben
siraben / a-normalization.hs
Last active December 2, 2020 16:01
A-Normalization algorithm in Haskell
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE DeriveFunctor, GeneralizedNewtypeDeriving #-}
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
-- A-Normalization, based on Matt Might's blog post:
-- http://matt.might.net/articles/a-normalization/
import Control.Monad.Cont
import Control.Monad.State
import Data.Function
@siraben
siraben / day3p1.hs
Last active December 5, 2019 01:03
Avent of Code 2019 Day 3
{-# LANGUAGE OverloadedStrings #-}
import Data.Function
import Data.List hiding (delete)
import Data.Set
import qualified Data.Text as T
data Direction = U | R | D | L deriving (Show, Read)
type Wire = [Move]
type Pos = (Int, Int)
data Move = Move Direction Int deriving (Show, Read)
@siraben
siraben / aop.hs
Created April 11, 2020 02:22
Translating code from "Algebra of Programming"
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE OverloadedLists #-}
-- Friday, April 10, 2020 at 17:25
-- Algebra of Programming in Haskell, an attempt.
-- Unlike other approaches, we will not use polytyping, which requires
-- elaborate use of generics, type families, functional dependencies,
-- and more. Thus, we will be able to reason equationally about our
@siraben
siraben / ski.lisp
Last active April 20, 2020 07:00
SKI Machine in the Ethereum Virtual Machine
;; Virtual machine that runs SKI combinators
(subroutine $)
(subroutine arg)
(subroutine num)
(subroutine lazy)
(subroutine $arg)
(subroutine run)
(subroutine init)
(subroutine sp!)