Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar

Arnau Abella monadplus

View GitHub Profile
@monadplus
monadplus / nixpkgs.md
Created March 4, 2020 08:47
Where is <nixpkgs> ?

Nix looks for expressions at NIX_PATH.

NIX_PATH is very similar to PATH but used by nix expressions (an expression is search on the NIX_PATH from left to right)

$ nix-instantiate --eval -E '<nixpkgs>'
/nix/var/nix/profiles/per-user/root/channels/nixos
$ echo $NIX_PATH
@monadplus
monadplus / nix-locate.md
Created March 4, 2020 08:58
Nix: locate a file in any package (for example, .so)
@monadplus
monadplus / listing.tex
Created March 9, 2020 16:10
Code listing with package listings
\usepackage{xcolor}
\definecolor{codegreen}{rgb}{0,0.5,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\usepackage{listings}
\lstset{
backgroundcolor=\color{backcolour},
@monadplus
monadplus / running-haskell-scripts.md
Last active February 10, 2023 19:46
Running a haskell script without GHC using nix

Updated version can be found on my website.

Running a haskell script without GHC

Given the following haskell script generate-random-samples.hs that requires mwc-random ...

{-# LANGUAGE ScopedTypeVariables #-}

import System.Random.MWC
@monadplus
monadplus / Unboxed.hs
Created March 11, 2020 12:58
Unboxed Types in GHC (Example)
{-# LANGUAGE MagicHash #-}
module Main where
-- More on https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#unboxed-types-and-primitive-operations
import GHC.Exts
showUnboxedInt :: Int# -> String
showUnboxedInt n = (show $ I# n) ++ "#"
@monadplus
monadplus / laziness.hs
Last active May 1, 2021 14:23
Laziness in Haskell
import Data.IntMap.Lazy as Map
{-
Notice that the smallest is not evaluated under 'go' function,
otherwise the computation would be ⊥
-}
attachMin :: Ord a => [a] -> [(a, a)]
attachMin l =
let (l', smallest) = go smallest l
in l'
@monadplus
monadplus / relation_type_classes.md
Created March 16, 2020 10:12
Expressing relations between types using type classes

All credit to O. Charles and his wonderful blog "24 of GHC extensions"

Expressing relations between types using type classes

Suppose that we want to model a generic mutable variable container using type classes.

This first approach works fine for IORef and MVar...

class IOStore store where
@monadplus
monadplus / overhead_ffi.md
Created March 19, 2020 22:26
GHC FFI overhead
"unsafe" c ffi (doesn't mean what you think) takes 10-15 ns when i measured it on my computer mroe than 5 years ago
[23:03] carter: safe which does some scheduler stuff took an extra 100-200 ns
[23:03] carter: so for c stuff that takes < 1-10 microseconds, unsafe is fine and safe
[23:04] carter: for stuff that runs longer, use safe c calls
[23:05] carter: (reason is: safe calls dont pause / block gc)
[23:07] carter: https://bitbucket.org/carter/who-ya-gonna-call-talk-may-2013-ny-haskell/src/master/ might have some code
@monadplus
monadplus / Nats.hs
Created April 9, 2020 11:04
from Int to Nat
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
import GHC.TypeLits
import Data.Proxy
import Data.Maybe
import System.IO
main :: IO ()
main = do
@monadplus
monadplus / nix-shell-unstable.sh
Last active April 27, 2020 11:39
nix-shell from unstable
nix-shell --packages 'let unstable = import <nixos-unstable> {}; in unstable.haskellPackages.ghcWithHoogle (pkgs: with pkgs; [ fused-effects ])'