Skip to content

Instantly share code, notes, and snippets.

@dcoutts
dcoutts / ShutdownDemo.hs
Created April 6, 2020 17:45
--shutdown-ipc FD pattern
module Main where
import Control.Exception
import Control.Concurrent
import Control.Concurrent.Async
import System.Environment
import System.IO
import System.IO.Error
@dcoutts
dcoutts / bytearray.cmm
Created January 25, 2020 16:29
placeByteArray# CMM primop
#include "Cmm.h"
/* Place a ByteArray# object header so that its payload is at the given address.
The payload pointer we're given points to the first byte of the payload,
so we have to place the object header immediately before it. The caller
must ensure that the space before the payload is appropriately allocated
so that we can use it.
*/
stg_placeByteArrayzh ( W_ payload, W_ n )
@dcoutts
dcoutts / SequenceFiles.hs
Created August 3, 2018 08:08
Example code for incrementally writing & reading files consisting of a sequence of records in CBOR binary format.
{-# LANGUAGE ScopedTypeVariables #-}
module SequenceFiles (
writeBinaryFileSequence,
appendBinaryFileSequence,
hPutBinaryFileSequence,
readBinaryFileSequenceLazy,
withBinaryFileSequenceLazy,
withBinaryFileSequence,
) where
@dcoutts
dcoutts / DataRefinement1.lhs
Last active June 5, 2018 15:10
Data refinment, theory and example
> module DataRefinement1 where
> import Data.Word
> import qualified Data.ByteString as BS
> import Control.Applicative
> import Test.QuickCheck
> import Prelude hiding (abs, null, replicate)
Data refinement provides a roadmap for how to think about, specify and test
cleanAction :: CleanFlags -> [String] -> GlobalFlags -> IO ()
cleanAction cleanFlags extraArgs globalFlags = do
projectRootDir <- findProjectRoot
let DistDirLayout{distDirectory} = defaultDistDirLayout projectRootDir
notice verbosity "cleaning..."
-- Remove the whole shared dist dir
chattyTry "removing dist/" $ do
@dcoutts
dcoutts / TargetSelector.hs
Created August 31, 2017 08:59
TargetSelector with TargetPackageNamed
{-# LANGUAGE CPP, DeriveGeneric, DeriveFunctor, RecordWildCards #-}
-----------------------------------------------------------------------------
-- |
-- Module : Distribution.Client.TargetSelector
-- Copyright : (c) Duncan Coutts 2012, 2015, 2016
-- License : BSD-like
--
-- Maintainer : duncan@community.haskell.org
--
-- Handling for user-specified target selectors.
@dcoutts
dcoutts / Main.hs
Created February 7, 2017 20:18
ghc-pkg read/write GADT
data ReadMode -- just type level tags
data ReadWriteMode
-- | Like Bool but statically determined by the mode type.
data OpenMode mode where
OpenReadMode :: OpenMode ReadMode
OpenReadWriteMode :: OpenMode ReadWriteMode
-- | Like Maybe but the case is statically determined by the mode type.
data InReadWriteMode mode a where
@dcoutts
dcoutts / Lazy.hs
Created June 21, 2016 16:38
Two different approaches to incremental CBOR sequence files
-- | Pure lazy API on top of CBOR
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module CBOR.Lazy (
-- * Encoding
EncodedElems(..)
, encodeElems
-- * Decoding
, DecodeLazy(..)
, DecoderState(..)