Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar

Arnau Abella monadplus

View GitHub Profile
@monadplus
monadplus / reflect_haskell.hs
Created October 1, 2019 14:27
Reflect library in Haskell (there will be probably better ways to do this)
{-# LANGUAGE Rank2Types, FlexibleContexts, UndecidableInstances #-}
import Data.Reflection
import Data.Proxy
import Text.Printf
import Prelude
data Coin = Euro | USD
newtype Amount coin = Amount { _amount :: Int }
@monadplus
monadplus / haskell.md
Last active October 31, 2019 18:24
Haskell notes
g :: b -> c
f :: r -> a -> b
(g .) . f :: r -> a -> c

[x+1..] -- [x+1, x+2, ..]

The thread that runs main in a Haskell program is a bound thread (slow). The best way around this problem is just to create a new thread from the main and work in that instead. [Parallelism and Concurrency in Haskell, chapter 15, page 248].

@monadplus
monadplus / st.md
Created October 15, 2019 19:18
ST Trick with existentials types/rigid skolem

ST Trick

newtype ST s a = ST { unsafeRunST :: a } -- phantom type s

instance Functor (ST s) where
  fmap f (ST a) = seq a (ST $ f a)

instance Applicative (ST s) where
 pure = ST
@monadplus
monadplus / lazy.md
Last active October 16, 2019 17:56
Laziness vs Strictness

Laziness vs Strictness vs Non-strictness

Non-Strictness means reducing the program/expressions outside-in and sub-expressions may be discarded and bottoms may not be evaluated.

Stricness means evaluating the program from inside-out.

Laziness means evaluating an expression when it is needed. When the evaluation engine sees the expression for the first time, creates a thunk to compute the expression and a pointer to the thunk. When the expression is needed, its value is computed and the thunk is replaced by its result.

@monadplus
monadplus / associated_type_families.hs
Last active October 17, 2019 05:59
Associated type families
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-- Source: "Thinking with Types" - S.Maguire
@monadplus
monadplus / space_leak.md
Last active October 17, 2019 18:40
Space leak: when laziness bites us (Source: https://chrispenner.ca/posts/wc)

This code is hella slow and consumes ludicrous amounts of memory. But why ? Laziness shouldn't be a problem since we are using foldl'.

import Data.List
import Data.Char

simpleFold :: FilePath -> IO (Int, Int, Int)
simpleFold fp = do
    countFile <$> readFile fp

{-# UNPACK #-} Unpacking strict fields

This is one of the most powerful techniques you can use to optimise data structures.

When a constructor field is marked strict, and it is a single-constructor type, then it is possible to ask GHC to unpack the contents of the field directly in its parent. For example, given this:

data T = T {-# UNPACK #-} !(Int,Float)
@monadplus
monadplus / gpg-example.sh
Created November 6, 2019 10:22
GPG: example
#!/bin/bash
# Let's encrypt a file with recipient's public key
gpg --recipient monadplus --encrypt gpg-example.sh # Binary file
gpg --recipient monadplus --armor --encrypt gpg-example.sh # ASCII file
# Let's decrypt
gpg --decrypt gpg-example.sh.gpg # This will prompt the passphrase of the private key
####################
@monadplus
monadplus / command.md
Last active November 12, 2019 21:49
Pandoc: from markdown to html slides
# Markdown -> html (using slidy)
$ pandoc -t slidy -s -o slides.html slides.md

# Markdown -> html (using slidy on a url) 
$ pandoc -f markdown -t slidy -s -o slides.html https://github.com/Gabriel439/slides/raw/master/nix-internals/slides.md
@monadplus
monadplus / nix_commands.md
Last active November 13, 2019 08:16
Nix: useful commands

show-derivation:

$ nix show-derivation /nix/store/kg2g9q5chm6dyn2hh8pk8zxwkgb4ph7q-myname.drv

{
  "/nix/store/kg2g9q5chm6dyn2hh8pk8zxwkgb4ph7q-myname.drv": {
    "outputs": {
      "out": {
 "path": "/nix/store/bgzq8iyk93c2cxpcsqapfrwij5kwfnpc-myname"