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 / 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)
@andrewthad
andrewthad / dependent_haskell_quantifiers.markdown
Created October 16, 2017 13:35
Dependent Haskell Quantifiers
Quantifier Dependency Relevance Visibility Matchability
∀ (a :: τ ). ... dep. irrel. inv. (unification) unmatchable
∀ (a :: τ ) → ... dep. irrel. vis. unmatchable
Π (a :: τ ). ... dep. rel. inv. (unification) unmatchable
Π (a :: τ ) → ... dep. rel. vis. unmatchable
τ ⇒ ... non-dep. rel. inv. (solving) unmatchable
τ → ... non-dep. rel. vis. unmatchable
@andrewthad
andrewthad / infinite_list_tree.hs
Created October 26, 2017 12:32
Variant of infinite tree that partition by evenness and oddness
{-# language BangPatterns #-}
import Prelude hiding (lookup)
import Data.Primitive
main :: IO ()
main = do
print $ lookup [17,15,6,999]
data Tree a = Tree (Tree a) a (Tree a)
@andrewthad
andrewthad / Arrs.hs
Created October 26, 2017 15:34
Fast Extensible Records
{-# language PolyKinds #-}
{-# language TypeOperators #-}
{-# language LambdaCase #-}
{-# language BangPatterns #-}
{-# language KindSignatures #-}
{-# language DataKinds #-}
{-# language GADTs #-}
{-# language RankNTypes #-}
{-# language TypeFamilies #-}
{-# language TypeFamilyDependencies #-}