Skip to content

Instantly share code, notes, and snippets.


Niklas Hambüchen nh2

View GitHub Profile
nh2 / realToFrac-rewrite-rules.txt
Created Feb 9, 2014
Discussion on #haskell about realToFrac performance
View realToFrac-rewrite-rules.txt
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 / matmult.hs
Created Feb 9, 2014
Example of how Haskell's forM_ [1..N] is slow and custom loops are fast (lack of fusion)
View matmult.hs
{-# 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 / time-natural.hs
Created Mar 1, 2014
Natural time in Haskell: `2 hours + 4 seconds`
View time-natural.hs
{-# 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 /
Last active Aug 29, 2015
Generates a git repository with dual history and some branches on top (for fixing a Jenkins performance bug)
# 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 / k-smallest-benchmark.hs
Created Apr 6, 2014
Benchmark for finding the k-smallest element in a list in Haskell
View k-smallest-benchmark.hs
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 / reverse-deps-sorted.hs
Created Apr 10, 2014
Sorts and prints Haskell packages by number of reverse dependencies
View reverse-deps-sorted.hs
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 / list-loop.hs
Last active Aug 29, 2015
Benchmark: forM_ [1..n] against manual loop
View list-loop.hs
{-# 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 / reinterpret-cast.c
Created Apr 30, 2014
reinterpret_cast in C
View reinterpret-cast.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 / generic-show.hs
Last active Aug 29, 2015
Haskell sorcery with Generics and Typeable for showing the structure of any value
View generic-show.hs
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 / Haskell-Conditional-Compilation.txt
Created May 14, 2014
MIN_VERSION_base vs. __GLASGOW_HASKELL__ for conditional compilation
View Haskell-Conditional-Compilation.txt
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 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 from 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