Skip to content

Instantly share code, notes, and snippets.

View siraben's full-sized avatar

Ben Siraphob siraben

View GitHub Profile
@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 / 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!)
@siraben
siraben / monoidal-functors.hs
Created June 2, 2020 13:02
Monoidal functors and applicatives
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE FlexibleInstances #-}
-- Tuesday, June 02, 2020
-- Monoidial functors and Applicatives
import Prelude hiding (Applicative(..), (**))
-- Lax monoidial functors
-- Laws:
-- Identity: unit ** x ~= x ** unit ~= unit
-- Associativity: u ** (v ** w) ~= (u ** v) ** w
@siraben
siraben / lc.rs
Last active August 20, 2020 17:26
Lambda calculus interpreter in Rust
use std::rc::Rc;
// Environment-passing lambda calculus interpreter in Rust
#[derive(Debug, Clone, PartialEq)]
enum LC {
Var(String),
App(Box<LC>, Box<LC>),
Abs(String, Box<LC>),
Lit(u32),
Incr(Box<LC>),
}