Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar

Arnau Abella monadplus

View GitHub Profile
@monadplus
monadplus / haskellFilterSource.nix
Last active November 14, 2019 20:58
haskellFilterSource
{ mkDerivation, base, stdenv, pkgs }:
mkDerivation {
pname = "parconc-playground";
version = "0.1.0.0";
src = pkgs.lib.cleanSourceWith {
src = ./.;
filter = path: type:
let baseName = baseNameOf path; in
!( type == "directory"
&& builtins.elem baseName ([".git" ".cabal-sandbox" "dist"] ++ paths))
@monadplus
monadplus / dynamic.hs
Created November 14, 2019 22:11
Data.Dynamic
-- [Dynamic Types](https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Dynamic.html)
-- Typeable provides type information at runtime and allowws for dynamic
-- casting via `cast :: (Typeable a, Typeable b) => a -> Maybe b`.
data Dynamic where
Dynamic :: Typeable t => t -> Dynamic
elimDynamic
:: (forall a. Typeable a => a -> r)
@monadplus
monadplus / partial_vs_total.md
Created November 16, 2019 07:54
Partial vs Total languages

All these languages are examples of partial languages, i.e. the result of computing the value of an expression e of type T is one of the following:

  • the program terminates with a value in the type T.
  • the program e does not terminate.
  • the program raises an exception (which has been caused by an incomplete definition).

Agda and other languages based on type theory are total languages in the sense that a program e of type T will always terminate with a value in T.

@monadplus
monadplus / type-level-equality-test.hs
Created November 21, 2019 19:38
Type level equality test
type family Delete (t :: Tree) (n :: Nat) :: Tree where
Delete 'Empty _ = 'Empty
Delete ('Node l c r) n = Delete' (Compare n c) ('Node l c r) n
type family Delete' (ord :: Ordering) (t :: Tree) (n :: Nat) :: Tree where
Delete' 'LT ('Node l c r) n = 'Node (Delete l n) c r
Delete' 'GT ('Node l c r) n = 'Node l c (Delete r n)
Delete' 'EQ ('Node l c 'Empty) n = l
Delete' 'EQ ('Node l c r) n = Constr l (Smallest r)
@monadplus
monadplus / sieve.hs
Created November 21, 2019 21:43
Sieve of Eratosthenes
-- Source: https://wiki.haskell.org/Prime_numbers
--primesTo m = sieve [2..m]
--where
--sieve (x:xs) = x : sieve (xs \\ [x,x+x..m])
--sieve [] = []
type family Sieve (n :: Nat) :: [Nat] where
Sieve n = Sieve' (Drop N2 (UpToN n))
type family Sieve' (xs :: [Nat]) :: [Nat] where
@monadplus
monadplus / async.md
Created November 28, 2019 21:43
Takeaway from Asynchronous Exceptions in Haskell

Asynchronous Exceptions: Discussion

  • All non-IO haskell code is automatically safe by construction. This is the one factor that makes asynchronous exceptions feasible.
  • When working with resources: bracket and so have built in support for asynchronous-exceptions.
  • When working with MVar: use modifyMVar and so.

A couple of thecniques can simplify matters:

  • Large chunks of heavily stateful code can be wrapped in a mask, which drops into polling mode for asynchronous exceptions. This is much easier to work with. The problem then boils down to finding the interruptible operations and ensuring that exceptions raised by those will not cause problems. The GHC I/O library uses this technique: every Handle operation runs entirely inside mask.
@monadplus
monadplus / tailRecM.hs
Created December 26, 2019 10:37
tailRecM
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE InstanceSigs #-}
--{-# LANGUAGE ScopedTypeVariables #-}
module TailRecM where
-- This only makes sense in Purescript (strict haskell dialect)
-- But I am lacking a purescript runtime interpreter
import Control.Monad.Writer.Lazy
import Control.Monad.Trans.State.Lazy
@monadplus
monadplus / concurrent_data_structures.md
Created January 4, 2020 22:30
Shared Concurrent Data Structures

Shared Concurrent Data Structures

Brief summarize of shared state options, with a focus on the performance implications of the different choices.

Typically, the best approach when you want some shared state is to take an existing pure data structure, such as a list or a Map, and store it in a mutable container.

There are a couple of subtle performance issues to be aware of, though.

The first is the effect of lazy evaluation when writing a new value int othe container, which we've already covered.

@monadplus
monadplus / numbers.md
Last active January 8, 2024 21:39
Numbers in Haskell

Numbers in Haskell

Primitives types: Int, Integer, Float, and Double.

Derived: Complex, Rational, Scientific

Class structure

Class Operations Description Values
@monadplus
monadplus / inline-pragma.md
Last active April 13, 2020 11:50
GHC: Inline Pragma
## INLINEABLE vs INLINE
INLINEABLE is prefered. (Source: <https://wiki.haskell.org/Inlining_and_Specialisation>