Skip to content

Instantly share code, notes, and snippets.

### Keybase proof
I hereby claim:
* I am beala on github.
* I am beala (https://keybase.io/beala) on keybase.
* I have a public key whose fingerprint is 0BCE 27F8 FBED 34C0 B936 7625 1D7D CA76 C811 1C0C
To claim this, I am signing this object:
@beala
beala / .emacs
Last active August 29, 2015 14:05
emacs config for haskell-mode with cabal repl
(add-to-list 'load-path "~/src/haskell-mode")
(load "haskell-mode-autoloads.el")
(add-hook 'haskell-mode-hook 'interactive-haskell-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
(custom-set-variables
'(haskell-process-suggest-remove-import-lines t)
'(haskell-process-auto-import-loaded-modules t)
@beala
beala / gist:a252fcb54970b7968cd4
Last active August 29, 2015 14:07
Type of arbitrarily nested list
λ: newtype Fix f = Nest {unFix :: f (Fix f)}
λ: :t Nest []
Nest [] :: Fix []
λ: :t Nest [Nest []]
Nest [Nest []] :: Fix []
@beala
beala / dep_graph.hs
Last active August 29, 2015 14:13
Evaluate a dependency DAG concurrently communicating state through an MVar.
import Control.Concurrent
import qualified Data.Graph.Inductive.Graph as Gr
import Data.Graph.Inductive.PatriciaTree
import qualified Data.Map.Strict as M
import Control.Monad.Trans.Either
import Control.Monad.Trans (liftIO)
import System.IO
data State = NotStarted | Started | Finished deriving (Show)
type NodeStateMap = M.Map Gr.Node State
@beala
beala / reader.hs
Last active August 29, 2015 14:13
Explanation of the reader applicative/monad.
import Control.Applicative
-- Declare a struct with two readers `a` and `b`.
data Env = E {a :: Int, b :: Int}
-- Applicative (r -> _) lets us, eg, take a function that operates on Ints,
-- and instead have it operate on an Env that contains Ints. Since the new
-- function reads from the environment, we call it the Reader applicative
-- (or monad if >>= is defined).
@beala
beala / trace.scala
Created January 26, 2015 03:56
Trace any stateful computation in 10 lines.
/* Lift a stateful computation State[S,A] to State[(S, Seq[(S,A)]), A]
* where the (S, Seq[(S,A)]) is a history of the computation's state at
* each step: (currentState, Seq[(stateBeforeStep, resultOfStep)])
*/
def trace[S,A](iter: State[S,A]): State[(S, Seq[(S,A)]),A] = for {
preStateAndHistory <- get[(S, Seq[(S,A)])]
preState = preStateAndHistory._1
history = preStateAndHistory._2
(postState, result) = runState(for {
result <- iter
@beala
beala / fizzbuzz.hs
Last active August 29, 2015 14:15
Free monad for declarative fizz buzz
{-# LANGUAGE DeriveFunctor #-}
import Control.Monad.Free
import Data.List
data FizzBuzz f a = FizzBuzz (Factor f) String a deriving Functor
newtype Factor a = Factor a
-- A fizz buzz action that produces a String if the Integral is a factor.
fizzbuzz :: (Show a, Integral a) => a -> String -> Free (FizzBuzz a) ()
@beala
beala / sbv_fun.hs
Last active August 29, 2015 14:17
Fun with sbv.
-- Fun with sbv, a theorem prover to haskell.
-- In this file I:
-- - Implement a bit twiddling reverse function.
-- - Use SMT to prove the twiddling function correct.
-- - Implement and prove correct some simple bit conversion functions.
-- - Use the reverse function and sbv's SAT solver to find bytes that are palindromes.
import Data.SBV
-- Functions for reversing 8 bits.
@beala
beala / rot13.hs
Created March 29, 2015 22:58
rot13 (verified!)
{-# LANGUAGE DeriveDataTypeable #-}
import Data.SBV
import Control.Monad.IO.Class
import Data.Foldable
import Data.Generics
-------------------------------------------------------------------------------
-- Rot13 over an array of 8 bit words.
--
@beala
beala / NonEmptyList.scala
Last active August 29, 2015 14:21
NonEmptyList
object Main {
// NonEmpty because the constructor forces you to provide at least one
// instance of A.
case class NonEmptyList[A](a: A, as: List[A]) {
def map[B](f: A => B): NonEmptyList[B] = {
NonEmptyList[B](f(a), as.map(f))
}
def flatMap[B](f: A => NonEmptyList[B]): NonEmptyList[B] = flatten(map(f))
}