Skip to content

Instantly share code, notes, and snippets.

View edofic's full-sized avatar

Andraž Bajt edofic

View GitHub Profile
@edofic
edofic / type_errors.hs
Created December 5, 2015 18:53
test for type errors in haskell
{- compile with -fdefer-type-errors -}
import Control.Exception
import Data.List
import System.IO.Unsafe
assertTypeError :: a -> ()
assertTypeError a = unsafePerformIO $ do
res <- try $ evaluate a
case res of Left e@(ErrorCall msg) -> if "expected type" `isInfixOf` msg
@edofic
edofic / reverse.hs
Created November 28, 2015 22:10
String reversal in Haskell using the ST monad
import Control.Monad
import Control.Monad.ST
import Data.Array.MArray
import Data.Array.ST
swap :: STArray s Int a -> Int -> Int -> ST s ()
swap ar i j = do
ei <- readArray ar i
ej <- readArray ar j
writeArray ar i ej
@edofic
edofic / my_negation.idr
Last active September 7, 2015 08:06
Toy example of proof/program separation in Idris
%default total
data T : Type where
A : T
B : T
my_neg : T -> T
my_neg A = B
my_neg B = A
@edofic
edofic / fixed.hs
Created July 16, 2015 07:34
fixed precision arithmetic in haskell
import Data.Ratio ((%))
import GHC.Real
-- 10 decimal places
newtype Fixed = Fixed Integer deriving (Eq, Ord, Enum)
instance Show Fixed where
show (Fixed a) = show whole ++ "." ++ trailing where
(whole, part) = a `divMod` (10 ^ 10)
digits = show part
@edofic
edofic / halcyon_fail_log
Created March 17, 2015 14:38
Halcyon fail log
-----> Self-updating bashmenot... done, ac791c3
-----> Self-updating Halcyon... done, a0894ef
-----> Examining cache contents
cabal-install-1.22.0.0.tar.gz
ghc-7.10.0.20150316-x86_64-unknown-linux-deb7.tar.xz
halcyon-build-snowflake-0.1.1.1.tar.gz
halcyon-cabal-1.20.0.3-hackage-2015-03-17.tar.gz
halcyon-ghc-7.8.4.tar.gz
halcyon-ghc-7.10.1-rc3.tar.gz
halcyon-install-b188d5d-snowflake-0.1.1.1.tar.gz
@edofic
edofic / turing-sim..hs
Created January 5, 2015 09:15
purely functional turing machine simulator
module Main where
data Stream a = Stream a (Stream a) deriving (Eq, Show, Functor)
data Zipper a = Zipper { zl :: (Stream a)
, ze :: a
, zr :: (Stream a)
} deriving (Eq, Show, Functor)
srepeat :: a -> Stream a
srepeat a = go where go = Stream a go
@edofic
edofic / ring.hs
Created January 4, 2015 13:43
./ring n m # create n threads and pass unit around the ring m times
module Main where
import Control.Concurrent
import Control.Monad
import System.Environment (getArgs)
main = do
[n,m] <- getArgs
let slots = [1..read n]
vars <- forM slots $ const newEmptyMVar
@edofic
edofic / scotty.hs
Last active August 29, 2015 14:09
a scotty server serving a file from disk and proxying a request (to itself)
module Main where
import Control.Monad (when, replicateM)
import Control.Monad.IO.Class (liftIO)
import qualified Data.ByteString as BS
import qualified Blaze.ByteString.Builder as B
import qualified Network.HTTP.Client as C
import Web.Scotty
chunkSize = 32 * 1024
module TypedMap
( TypedMap
, empty
, insert
, lookup
) where
import Data.Dynamic
import Data.Maybe (fromJust)
import qualified Data.Map as M
@edofic
edofic / forker.go
Last active August 29, 2015 14:06
experiment to see how fork/exec impacts memory usage
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
)