Skip to content

Instantly share code, notes, and snippets.

View nh2's full-sized avatar

Niklas Hambüchen nh2

View GitHub Profile
@nh2
nh2 / realToFrac-rewrite-rules.txt
Created February 9, 2014 14:43
Discussion on #haskell about realToFrac performance
Does realToFrac compile to a no-op for (CFloat <-> Float / CDouble <-> Double)?
(14:27:30) kosmikus: nh2: it'll be a noop in many situations. CDouble normally is a newtype wrapper around Double. There are GHC Rule pragmas that'll turn realToFrac between Double and CDouble into a realToFrac between Double and Double by applying the constructor. Applying the constructor is a no-op, and there's a Double/Double instance having id as implementation.
(14:28:17) kosmikus: nh2: that being said, it's known that while simple applications of newtype constructors are newtypes, that doesn't translate to all contexts (such as mapping the constructor over a datastructure, for example)
(14:28:55) FreeFull: kosmikus: realToFrac isn't part of a typeclass though, it is defined as realToFrac = fromRational . toRational
(14:30:37) kosmikus: FreeFull: hm, you're right, I've been looking at the wrong place. let's check again.
(14:32:38) nh2: kosmikus, FreeFull: I really hope that realToFrac has rewrite rules for Double/CDouble and
@nh2
nh2 / matmult.hs
Created February 9, 2014 21:13
Example of how Haskell's forM_ [1..N] is slow and custom loops are fast (lack of fusion)
{-# LANGUAGE ScopedTypeVariables, BangPatterns #-}
-- Example of how forM_ [1..N] is slow
module Main (main) where
import Prelude hiding (read)
import Control.Monad
import Data.Vector ((!), Vector)
import qualified Data.Vector as V
@nh2
nh2 / time-natural.hs
Created March 1, 2014 01:33
Natural time in Haskell: `2 hours + 4 seconds`
{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving #-}
newtype TimeUnit = TimeUnit Integer -- how many microseconds
deriving (Eq, Show, Num)
instance Num (TimeUnit -> TimeUnit) where
fromInteger n = \(TimeUnit scale) -> TimeUnit (n * scale)
-- a + b = ... -- task for you
@nh2
nh2 / generate-test-git.sh
Last active August 29, 2015 13:57
Generates a git repository with dual history and some branches on top (for fixing a Jenkins performance bug)
#!/bin/bash
# Creates a git repository called "testgit-generated"
# with two separate histories branching from the initial
# commit, each being 500 commits long.
# On top of each, we create 200 branches with one extra
# commit per branch.
# Should take around 30 seconds.
set -e
@nh2
nh2 / k-smallest-benchmark.hs
Created April 6, 2014 15:02
Benchmark for finding the k-smallest element in a list in Haskell
import Criterion.Main
import Data.List (sort)
import Data.Vector (Vector, fromList, unsafeThaw)
import qualified Data.Vector.Mutable as VM
import Data.Vector.Algorithms.Heap (select)
import Control.Monad.ST (runST)
import Data.Maybe (fromJust)
kSmallest1 :: Int -> [Int] -> Int
@nh2
nh2 / reverse-deps-sorted.hs
Created April 10, 2014 01:49
Sorts and prints Haskell packages by number of reverse dependencies
import Data.List (sort)
import qualified Data.Map as Map
import Distribution.PackDeps
import Control.Monad
main :: IO ()
main = do
newest <- loadNewest
let reverses = getReverses newest
@nh2
nh2 / list-loop.hs
Last active August 29, 2015 14:00
Benchmark: forM_ [1..n] against manual loop
{-# LANGUAGE BangPatterns, ScopedTypeVariables #-}
module Main (main) where
import Criterion.Main
import Control.Monad
import Data.Vector ((!), Vector)
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
@nh2
nh2 / reinterpret-cast.c
Created April 30, 2014 00:02
reinterpret_cast in C
#include <stdio.h>
int main(int argc, char const *argv[])
{
float f = 1.23;
int i = * (int *) &f;
printf("%d\n", i);
float f2 = * (float *) &f;
@nh2
nh2 / generic-show.hs
Last active August 29, 2015 14:01
Haskell sorcery with Generics and Typeable for showing the structure of any value
import GHC.Generics
import Data.Typeable
deriving instance Show (f p) => Show (M1 i c f p)
deriving instance (Show (f p), Show (g p)) => Show ((f :*:g) p)
deriving instance (Show (f p), Show (g p)) => Show ((f :+:g) p)
deriving instance (Show c) => Show (K1 i c p)
deriving instance Show (V1 p)
deriving instance Show (U1 p)
deriving instance Show D
@nh2
nh2 / Haskell-Conditional-Compilation.txt
Created May 14, 2014 14:27
MIN_VERSION_base vs. __GLASGOW_HASKELL__ for conditional compilation
nh2: does `#if MIN_VERSION_base(4,7,0)` fail when using ghci / do I need to build with cabal? I'm trying to get my packages 7.8-ready
thoughtpolice: that macro is only defined by Cabal, IIRC. so yes, you do need cabal
nh2: thoughtpolice: is what they describe in https://github.com/hesselink/flock/pull/1 a valid workaround, using __GLASGOW_HASKELL__?
carter: nh2: yup, only in cbal
carter: yes
thoughtpolice: nh2: sure, __GLASGOW_HASKELL__ should be fine, since the accompanying version of base is fixed anyway
thoughtpolice: nh2: so you probably want something like '#if __GLASGOW_HASKELL__ >= 708'
thoughtpolice: nh2: but note __GLASGOW_HASKELL__ does not account for the minor version, so you can't distinguish base 4.7.0.0 from 4.7.1.0 for example
thoughtpolice: (although i think historically we've never bumped an update like that - only very minor bumps per release)
carter: thoughtpolice: edward just pointed out to me that GHC doesn't expose GHC minor version cpp