Skip to content

Instantly share code, notes, and snippets.

@chrisdone
chrisdone / ghc-ffi-types.hs
Created November 9, 2019 16:03
GHC ffi types
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_powm_word" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Word# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_powm" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Int# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_powm1" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Word# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_powm_sec" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Int# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_invert_word" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Word# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_invert" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Int# #))
(CCall (CCallSpec (StaticTarget NoSourceText "__int_encodeDouble" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Double# #))
(CCall (CCallSpec
@chrisdone
chrisdone / ghc-ffi-types.txt
Created November 9, 2019 16:02
GHC FFI types
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_powm_word" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Word# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_powm" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Int# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_powm1" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Word# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_powm_sec" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Int# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_invert_word" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Word# #))
(CCall (CCallSpec (StaticTarget NoSourceText "integer_gmp_invert" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Int# #))
(CCall (CCallSpec (StaticTarget NoSourceText "__int_encodeDouble" (Just integer-gmp) True) CCallConv PlayRisky),(# State# RealWorld, Double# #))
(CCall (CCallSpec
[2 of 2] Compiling Main ( hask-tok3.hs, hask-tok3.o )
==================== Tidy Core ====================
Result size of Tidy Core
= {terms: 2,614, types: 3,717, coercions: 1,413, joins: 31/63}
-- RHS size: {terms: 16, types: 9, coercions: 0, joins: 0/0}
Main.$WPoint [InlPrag=INLINE[2]] :: Int -> Int -> Int -> Point
[GblId[DataConWrapper],
Arity=3,
@chrisdone
chrisdone / 0_Named.hs
Last active November 9, 2019 10:31
Simplified Ghost of Departed proofs
-- | Our main engine for naming a value, then we can prove properties about a named value.
{-# LANGUAGE ExistentialQuantification #-} -- Used for SomeNamed.
{-# LANGUAGE PatternSynonyms #-} -- Used for the Name pattern.
{-# LANGUAGE ViewPatterns #-} -- Used for the Name pattern.
{-# LANGUAGE RankNTypes #-} -- Used for withName.
module Named ( Named, pattern Name, forgetName, withName, someNamed, SomeNamed(..) ) where
-- | Give a generated type-level name to any value.
@chrisdone
chrisdone / 0README.md
Last active November 8, 2020 09:49
total-eval.hs

Total (non-error-throwing) lambda-calculus evaluators.

@chrisdone
chrisdone / README.md
Last active November 9, 2019 12:35
Lexing efficiently with Zepto on War & Peace

This is a small experiment to see whether one can:

  1. Lex a file efficiently, retaining line/column and indentation information.
  2. Consuming no or little memory (aside from the input size itself), and
  3. Still have the flexibility to perform zero-cost operations like folds (counting tokens), doing nothing (a simple pass), or printing. SAX-style.

This proves that one could, e.g., run in ST and write to a mutable Storable vector. Allowing the caller to process the set of tokens later. But the cost/calculation of figuring out line/col/indentation of each token has already been figured out.

The input file is war-and-peace.txt which is 6MB. Simply reading the file takes 27ms. Counting all words (non-space) in the file takes 36ms. So let's say about 9ms, in the absense of more rigorous gauge-based benchmarking. There are 1,132,619 "words" in the file.

@chrisdone
chrisdone / gadt-monad.hs
Last active November 11, 2019 19:48
GADT monad
{-# LANGUAGE GADTs, LambdaCase #-}
import Control.Monad
-- Model the problem with a simple GADT:
data Foo a where
GetInput :: Foo String
WriteOutput :: String -> Foo ()
Pure :: a -> Foo a
{-# OPTIONS_GHC -fno-warn-type-defaults #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ExtendedDefaultRules #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
@chrisdone
chrisdone / 0README.md
Created October 22, 2019 14:31
Spinning up Duta mail server on DigitalOcean with docker-machine and docker-compose

Change the mx.chrisdone.com mentions to your own domain.

Spin up a Droplet on DigitalOcean with docker-machine.

$ time docker-machine create \
  --driver digitalocean \
  --digitalocean-access-token $(cat ~/.do-token) \
  --digitalocean-monitoring \
  --digitalocean-region "lon1" \

--digitalocean-size "s-1vcpu-1gb" \

@chrisdone
chrisdone / money-currency.hs
Last active October 22, 2019 16:33
money currency in haskell
-- | A currency-less integral monetary value which cannot be further
-- subdivided. E.g. cent, penny, satoshi.
--
-- For lack of a better name:
-- <https://money.stackexchange.com/questions/85562/generic-name-for-the-smallest-unit-of-currency>
newtype IntegralMoney = IntegralMoney Int
deriving (Eq, Ord, Integral, Num, Enum, Real, Show)
instance PersistFieldSql IntegralMoney where
sqlType _ = SqlString
instance PersistField IntegralMoney where