Skip to content

Instantly share code, notes, and snippets.

View charles-cooper's full-sized avatar

Charles Cooper charles-cooper

View GitHub Profile
@charles-cooper
charles-cooper / Example.hs
Last active December 10, 2015 22:17
RFC: dsl for writing C
import Language.C.DSL
import Language.Format
main = prettyPrint $ do
include "stdio.h"
includeLocal "foo.h"
intptr_t <- typedef (ptr int) "intptr_t"
x <- int "x"
y <- double "y"
z <- ptr intptr_t "z"
w <- carray int "w" 21
nan = 0/0
silenceNaN d = if isNaN d then 0 else d
cumSumWithMissing :: [Double] -> [Double]
cumSumWithMissing ds = let
ret = scanl1 (+) $ silenceNaN <$> ds
go x y = if isNaN x then x else y
in zipWith go ds ret
foo :: [Double] -> [Double]
@charles-cooper
charles-cooper / encodings.hs
Last active December 31, 2015 20:16
example of church encoding
-- see https://docs.google.com/presentation/d/1f-avqUksUCO8vvwn1KQNu-LIVwqNc1EbQz1l_stTD8o/edit#slide=id.gf6104008a_0_810
{-# LANGUAGE RankNTypes #-}
type PersonLambdas = forall a. (String -> Int -> a) -> (String -> Int -> Double -> a) -> a
child :: String -> Int -> PersonLambdas
child s i = \childL adultL -> childL s i
adult :: String -> Int -> Double -> PersonLambdas
adult s i d = \_ adultL -> adultL s i d
@charles-cooper
charles-cooper / ta_lib.nix
Created January 5, 2016 15:19
ta-lib nix file
{ stdenv, fetchurl, gettext, attr }:
stdenv.mkDerivation rec {
version = "0.4.0";
name = "ta-lib-${version}";
src = fetchurl {
url = "http://prdownloads.sourceforge.net/ta-lib/${name}-src.tar.gz";
sha256 = "0lf69nna0aahwpgd9m9yjzbv2fbfn081djfznssa84f0n7y1xx4z";
};
nativeBuildInputs = [ gettext ];
buildInputs = [ attr ];
@charles-cooper
charles-cooper / phantom_types.hs
Last active January 18, 2016 03:39
phantom types
{- Fun with phantom types. Type safe doubles. #-}
-- this lets us derive the Num implementation
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
-- P stands for Phantom, or Parametrized type
newtype P a b = Wrap { unwrap :: b } deriving Num
deriving instance Floating b => Floating (P a b)
deriving instance Fractional b => Fractional (P a b)
$ cat turing.hs
-- this is a finite memory tape with dereference and assign
import Control.Monad.State
import qualified Data.Map as Map
import Data.Word
type Ptr = Word64
-- a C-like array (char *) which maps int64 to int8
data Val a = Val Int deriving Show
instance Functor Val where
fmap _ (Val x) = Val x
coerce :: Val a -> Val b
coerce = fmap undefined
data Void
x :: Val Int
x = Val 1
@charles-cooper
charles-cooper / vectors.hs
Created January 31, 2016 17:03
vectorised numeric instances in haskell
-- These are straightforward. Just map or zipWith all operations.
-- The only interesting one is fromInteger, typically when people
-- do an operation on a vector and a scalar, they mean to run it
-- over the vector. E.g. [1,2,3] * 1.
instance Num a => Num [a] where
(+) = zipWith (+)
(*) = zipWith (*)
abs = map abs
signum = map signum
@charles-cooper
charles-cooper / dataKinds.hs
Created January 31, 2016 18:45
fun with data kinds
{-# LANGUAGE TypeFamilies, DataKinds, PolyKinds, TypeOperators #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -fwarn-unticked-promoted-constructors #-}
import Prelude hiding (lookup)
import qualified Data.Map as Map
type family Equal a b :: Bool where
Equal a a = 'True
@charles-cooper
charles-cooper / Lists.hs
Created March 26, 2016 15:56
fun with lists
{-# LANGUAGE DeriveFunctor #-}
module Main where
import Data.Monoid
import Control.Comonad
import Data.Fix
import Data.Foldable as Foldable
newtype Cell a b = Cell { runCell :: Either () ((,) a b) } deriving (Functor)