Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar

Arnau Abella monadplus

View GitHub Profile
@monadplus
monadplus / rewrite-pragma.md
Last active January 13, 2020 22:19
GHC: rewrite rules
{-# RULES "map/map" forall f g xs. map f (map g xs) = map (f.g) xs #-}
  • Use the debug flag -ddump-simpl-stats to see what rules fired.
  • Use -ddump-rule-firings to show individual rule firing.
  • Use -ddump-rule-rewrite to show the code before and after rewrite.
@monadplus
monadplus / algebraic_effects.md
Last active January 13, 2020 21:43
Algebraic Effects

Is having an algebraic effect important?

Given a monadic operation

op :: forall a. (M a, ..., M a) -> M a

then it is algebraic if it satisfies the following law:

@monadplus
monadplus / non-algebraic-op.hs
Last active January 14, 2020 09:19
Non-algebraic op
{-# LANGUAGE FlexibleContexts #-}
-- Credit for this code to: https://github.com/pva701
module Task0
( f
, f1
, f2
)where
import Control.Monad.Except (throwError, runExceptT, catchError, MonadError, runExcept)
@monadplus
monadplus / logging_why.md
Last active January 14, 2020 09:52
Why logging is an effect we want to track

Why logging is an effect we want to track

P. Oscar Boykin>
I love functional programming. I’m still very skeptical that logging is an effect we want to track. 
Logging is for observability. Removing all logging shouldn’t change the semantics of the program. 
By using an effect we change the types of many logged functions.
@monadplus
monadplus / ghc_architecture.md
Last active September 30, 2024 09:00
GHC Architecture: overview

GHC Architecture

GHC itself is written in Haskell, but the runtime system for Haskell, essential to run programs, is written in C and C--.

Front end

GHC's frontend - incorporating the lexer, parser and typechecker—is designed to preserve as much information about the source language as possible until after type inference is complete, toward the goal of providing clear error messages to users.

@monadplus
monadplus / handle_pattern.md
Last active January 15, 2020 20:12
The Handle Pattern (All credit to Jasper Van der Jeugt)

The Handle Pattern

This pattern is design for qualified import this is why all types are called the same (Database.Handle, Socket.Handle, Database.Config, Secket.Config, ...)

A database Handle

What you need:

  • A type called Handle
@monadplus
monadplus / cachix.md
Created January 17, 2020 09:10
Cachix: how to

Cachix

User

Add the cache:

$ cachix use monadplus

Money in Haskell

Floats (dont)

>>> 0.10 + 0.70 -- Fractional a
0.7999999999999999
>>> (0.1 + 0.7) * 2 -- Fractional a
1.5999999999999999
>>> 0.10 + 0.60 :: Float
@monadplus
monadplus / guaranteed.hs
Last active January 23, 2020 15:29
guarantee: Map k v -> Maybe (k -> v)
-- | Generic transformation for map to a total function 'key' -> 'value.
--
-- It fails if the given 'Data.Map.Strict' does not contains all the options
-- for the given 'key'.
--
-- The 'key' is expected to be a unary coproduct with an Ord instance.
class GGuaranteed gEnum where
gguarantee :: Map (gEnum key) value
-> Either (gEnum key) (gEnum key -> value)