Skip to content

Instantly share code, notes, and snippets.

@tfausak
tfausak / Main.hs
Created November 22, 2020 15:04
2020 State of Haskell Survey
{-# language OverloadedStrings #-}
module Main ( main ) where
import qualified Control.Monad as Monad
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Encode.Pretty as Aeson
import qualified Data.Aeson.Types as Aeson
import qualified Data.Bifunctor as Bifunctor
import qualified Data.ByteString.Lazy as LazyByteString
import qualified Data.Csv as Csv
import qualified Data.HashMap.Strict as HashMap
@tfausak
tfausak / language-extension-examples.hs
Created August 1, 2020 23:36
Minimal examples of Haskell language extensions.
a = a :: Eq b => () -- AllowAmbiguousTypes
a = proc b -> id -< b -- Arrows
a = let !b = () in b -- BangPatterns
a = 0b0 -- BinaryLiterals
a = id do 0 -- BlockArguments
foreign import capi "" a :: ()
class A b where c :: Eq b => b -- ConstrainedClassMethods
type A = Eq -- ConstraintKinds
# -- CPP
import Data.Proxy; a = Proxy :: Proxy True -- DataKinds
{-
this is a little experiment to parse a module with ghc
and extract exports identifiers, top-level declarations, and (documentation) comments
the idea is to use this as a basis for a haddock-like tool
that doesn't require type checking a module in order to run
-}
module Main ( main ) where
import qualified Control.Monad
import qualified DynFlags
/.stack-work/
/output/
/results.csv
/stack.yaml.lock
@tfausak
tfausak / Main.hs
Created February 28, 2020 15:49
Memory usage for various methods of storing Haskell byte strings in PostgreSQL.
-- I am working on a tool that downloads the Hackage index, stores it, and
-- processes it. I am storing the index in PostgreSQL. I want to keep the
-- maximum residency down. I don't care too much about total runtime.
--
-- This file contains a bunch of different operations in various formats. The
-- idea is to get a baseline measurement for each operation (like downloading
-- the index or reading it from a file) and each format (streaming, lazy, or
-- strict). Those measurements can be used to create a minimum memory
-- requirement for the actual tool.
--
@tfausak
tfausak / invertible-syntax-descriptions.markdown
Last active September 8, 2025 19:51
Survey of invertible syntax description libraries for Haskell.

Invertible syntax descriptions

An "invertible syntax description" is something that can be used to both parse and generate a syntax. For example, instead of defining separate toJson and fromJson functions for a given data type, an invertible syntax description could be used to provide both. There are many Haskell libraries that provide this functionality or something close to it. As far as I can tell most of them are at least inspired by Invertible syntax descriptions by Tillmann Rendel and Klaus Ostermann.

Personally I am interested in using these for HTTP routing. I frequently want to be able to define a route such as /episodes/:id.json, dispatch based on that route, and generate links to that route. Doing so manually is tedious and error prone.

-- Like Ratio but not broken.
module Quotient
( Quotient
, quotient
, unsafeQuotient
, (%)
, numerator
, denominator
, fromRatio
, toRatio
60,974,182,000 bytes allocated in the heap
263,226,596,160 bytes copied during GC
652,502,280 bytes maximum residency (815 sample(s))
3,177,208 bytes maximum slop
622 MB total memory in use (0 MB lost due to fragmentation)
Tot time (elapsed) Avg pause Max pause
Gen 0 57804 colls, 0 par 2.359s 2.372s 0.0000s 0.0006s
Gen 1 815 colls, 0 par 245.031s 246.321s 0.3022s 0.6341s
@tfausak
tfausak / haskell-weekly-in-2018.hs
Created February 3, 2019 16:33
Haskell Weekly in 2018
#!/usr/bin/env stack
-- stack --resolver lts-13.0 script
{-# OPTIONS_GHC -Weverything -Wno-implicit-prelude -Wno-unsafe #-}
module Main ( main ) where
import qualified Data.Aeson
import qualified Data.Aeson.Types
import qualified Data.List
import qualified Data.Ord
@tfausak
tfausak / buoy.hs
Last active May 2, 2024 19:34
keywords: Haskell applicative lift liftA liftA2 liftAN ap hoist raise boost
-- This whole thing is shamelessly stolen from:
-- <https://doisinkidney.com/snippets/nary-uncurry.html>.
-- Without flexible instances, GHC complains about the `HasApply` instances:
--
-- > Illegal instance declaration for `HasApply a Z`. All instance types must
-- > be of the form `(T a1 ... an)` where `a1 ... an` are *distinct type
-- > variables*, and each type variable appears at most once in the instance
-- > head.
--