Skip to content

Instantly share code, notes, and snippets.

@andrewthad
andrewthad / examples.hs
Created June 14, 2023 18:52
Examples of folds and strict folds
{-# language BangPatterns #-}
main :: IO ()
main = print (createMap myList)
myList :: [String]
myList = ["foo","bar","foo","bar"]
-- updateMap :: Map String Int -> String -> Map String Int
-- updateMap v k = Map.updateWith (+1) k m
@andrewthad
andrewthad / strictness_in_kinds.md
Created June 14, 2019 14:47
Strictness in Kinds

Strictness in Kinds

Let's tweak GHC's RuntimeRep stuff to try adding information about strictness. This information could be helpful for avoiding evaluating thunks that are guaranteed to already be evaluated. Presently, RuntimeRep is defined as:

data RuntimeRep

= LiftedRep

@andrewthad
andrewthad / case-on-bytes.md
Created August 5, 2020 14:15
Case on Bytes without compiler support

This is an idea for how to case on a byte sequence without any special support from the compiler. The basic idea is explored in https://github.com/layer-3-communications/fortios-syslog, but there code generation is used rather than TemplateHaskell. The idea is that, at compile time, generating a perfect hash function for all strings of the same length is a good way to pattern match on a sequence of bytes. Roughly, we have:

foo :: Bytes -> Bar
foo b = case B.length b of
  3 -> case hash3 b of

Car | b == "car" -> ...

@andrewthad
andrewthad / T5030.boxedrep.prof
Created May 24, 2020 00:01
T5030 SCC Profiling for BoxedRep
Sat May 23 19:57 2020 Time and Allocation Profiling Report (Final)
ghc +RTS -p -RTS -B/home/amartin/ghc-boxedrep-prof/lib/ghc-8.11.0.20200518 ./testsuite/tests/perf/compiler/T5030.hs
total time = 0.56 secs (558 ticks @ 1000 us, 1 processor)
total alloc = 711,938,744 bytes (excludes profiling overheads)
COST CENTRE MODULE SRC %time %alloc
simplCast-simplCoercion GHC.Core.Opt.Simplify compiler/GHC/Core/Opt/Simplify.hs:1293:57-77 28.5 30.5
@andrewthad
andrewthad / boxedrep.txt
Created May 18, 2020 11:15
T5030 built with ticky compiler before and after boxed rep
This file has been truncated, but you can view the full file.
'/home/amartin/ghc-boxedrep-ticky/lib/ghc-8.11.0.20200515/bin/ghc' '-B/home/amartin/ghc-boxedrep-ticky/lib/ghc-8.11.0.20200515' '-O2' './testsuite/tests/perf/compiler/T5030.hs' +RTS '-rboxedrep.ticky'
STACK USAGE:
ENTERS: 124184476 of which 124184476 (100.0%) direct to the entry code
[the rest indirected via Node's info ptr]
19582791 ( 15.8%) thunks
21067039 ( 17.0%) data values
0 ( 0.0%) normal indirections
0 ( 0.0%) permanent indirections
@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 / zookeeper_protocol_notes.md
Created July 7, 2019 12:37
Zookeeper Protocol Notes

Discerning the Zookeeper Protocol

To write a zookeeper client, it is neccessary to understand the protocol used to communicate with it. Unfortunately, this protocol is not documented. In this example, we use tcpflow to dump both the read and write channel of the TCP connection that zkCli.sh uses to connect with the zookeeper server.

Requests

@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 / T9020-ghc-882.dump.txt
Created February 24, 2020 19:01
Typechecker dump for T9020 in GHC 8.8.2 and in GHC-boxedrep
checkFamInstConsistency [Prelude]
Tc2 (src)
Tc3
tcExtendKindEnvList []
tcExtendKindEnvList []
tcDeriving False
tcDeriving 1 []
rnd
Adding instances:
Tc3b
@andrewthad
andrewthad / base64_benchmarks.txt
Created January 16, 2020 11:55
Base64 Benchmark Suite
benchmarked encode/base64-bytestring/25
time 161.7 ns (152.6 ns .. 168.1 ns)
0.980 R² (0.944 R² .. 0.998 R²)
mean 178.6 ns (169.1 ns .. 216.5 ns)
std dev 55.41 ns (14.38 ns .. 113.1 ns)
variance introduced by outliers: 95% (severely inflated)
benchmarked encode/base64-bytestring/100
time 232.1 ns (228.7 ns .. 234.8 ns)
0.999 R² (0.998 R² .. 0.999 R²)