Skip to content

Instantly share code, notes, and snippets.

{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ExistentialQuantification #-}
@andrewthad
andrewthad / twiddle.hs
Created December 21, 2016 15:55
Bit twiddling for fast decimal conversion
{-# LANGUAGE MagicHash #-}
import Data.Bits
import GHC.Prim
import GHC.Word
import Text.Printf
import Data.Char (ord)
hasBetween# :: Word# -> Word# -> Word# -> Word#
hasBetween# x m n =
{-# OPTIONS_GHC -O2 -fforce-recomp -ddump-simpl -dsuppress-all -dsuppress-coercions #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MultiWayIf #-}
import Data.Bits
import qualified Data.Vector.Unboxed as UV
main :: IO ()
main = do
v <- readLn
@andrewthad
andrewthad / sorted_vector_lookup.hs
Created April 13, 2017 14:48
Indexing into a sorted vector
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TemplateHaskell #-}
import Data.Bits
import Data.Vector (Vector)
import qualified Data.Vector.Unboxed as UV
import qualified Data.Vector as V
import qualified Data.List as L
import Test.QuickCheck.All (quickCheckAll)
@andrewthad
andrewthad / age_riddle.hs
Created April 21, 2017 17:19
Age Riddle
-- This is a riddle about people's ages that I made up. Below, we solve it
-- using the monadic interface to lists. Here it is:
--
-- * Jordan is either 13 years old or 16 years old
-- * Shen is between 3 and 5 years older than Jordan (inclusive bounds)
-- * Claudia is between 6 and 9 years younger than Jordan's age plus Shen's age.
-- * I am as old as the three aforementioned people combined.
--
-- Given the provided information, I could have many possible ages. The monad
-- instance for list models this kind of nondeterminism well. Run the example
@andrewthad
andrewthad / implicit_param_invalid_substitution.hs
Created June 13, 2017 14:05
Confusing behavior of Implicit Params
{-# LANGUAGE ImplicitParams #-}
example1 :: (?a :: Int) => [Int]
example1 = let ?a = 4 in [foo,bar + bar,(?a + 3) + (?a + 3)]
where
foo = bar + bar
bar = ?a + 3
example2 :: (?a :: Int) => [Int]
example2 = let ?a = 4 in [foo,bar + bar,(?a + 3) + (?a + 3)]
@andrewthad
andrewthad / prim_bench.hs
Last active March 30, 2018 12:39
Benchmark performance impact of removing start index from primitive vector
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE RankNTypes #-}
-- If you are building with GHC 8.2.1, it is recommonded that
-- this module be built with:
-- ghc -O2 -fllvm -pgmlo opt-3.9 -pgmlc llc-3.9 prim_bench.hs
-- For other GHC versions, choose the appropriate versions of
@andrewthad
andrewthad / stable.hs
Last active September 7, 2017 16:09
StableName with value in ST
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module Example
( Stable
, stabilize
, destabilize
) where
import GHC.Prim
import GHC.ST
@andrewthad
andrewthad / parallel_incrementing_array.hs
Created October 4, 2017 19:25
Parallel array initialization
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# OPTIONS_GHC -O2 -Wall -threaded -fforce-recomp #-}
import Criterion.Main
import Control.Monad (when)
import Control.Monad.ST.Unsafe (unsafeDupableInterleaveST,unsafeInterleaveST)
import GHC.ST (ST(..))
import GHC.Prim (spark#,seq#)
@andrewthad
andrewthad / parallel_product.hs
Last active October 5, 2017 17:48
Parallel Product
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MagicHash #-}
{-# OPTIONS_GHC -O2 -Wall -threaded -fforce-recomp #-}
import Criterion.Main
import Control.Monad (when)
import Control.Parallel.Strategies (runEval,rpar,rseq)
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar (newEmptyMVar,takeMVar,putMVar)