Skip to content

Instantly share code, notes, and snippets.

@louispan
louispan / minimal_ghc_ghcjs_nix.sh
Last active June 20, 2022 05:00
Install Cabal, GHC, or GHCJS with one Nix command
# install nix (5.5 mins)
curl https://nixos.org/nix/install | sh
# open a new shell to source nix
bash
# query available haskell compilers
nix-env -qaP -A nixpkgs.haskell.compiler [QUERY]
# install ghc (2.5 mins, cached binary)
@louispan
louispan / ConcurrentVariantInterpreter.hs
Last active June 18, 2018 13:05
Concurrently interpret polymorphic variant of commands
{-# LANGUAGE ApplicativeDo #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
@louispan
louispan / VariantInterpreter.hs
Last active June 18, 2018 13:06
Interpret polymorphic variant of commands using ContT and State
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
-- Example of interpreting using polymorphic variant
@louispan
louispan / Many ReaderT and StateT.hs
Last active April 15, 2018 08:05
Example of a reader & state of Many items
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeApplications #-}
module Main where
import Control.Lens
import Control.Monad.Reader
import Control.Monad.State
import Data.Diverse.Lens
import Data.Semigroup
@louispan
louispan / ReaderProducer.hs
Created March 7, 2018 20:31
Combine reading and producing monads
import Control.Monad.Morph
import Data.Functor.Identity
-- | This function combines two different monadic effects,
-- where one of the effects is a reader effect and the other is a
-- producing effect.
--
-- This version results in the reader monad's inner effect to be wrapped
-- around the producing effect.
-- This requires the Reader inner effect to be an MFunctor on Identity.
@louispan
louispan / InterpreterWithout.hs
Last active February 19, 2018 10:54
Interpreting without continuation monad
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilyDependencies #-}
module Main where
@louispan
louispan / TypeclassInterpreter.hs
Last active February 19, 2018 10:54
Interpreting using typeclasses
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilyDependencies #-}
module Main where
@louispan
louispan / ContMonadInterpreter.hs
Last active August 16, 2019 18:03
Continuation monad interpreter
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilyDependencies #-}
-- Example of interpreting using continuation monad instead of the free monad.
module Main where
{-# LANGUAGE UndecidableSuperClasses #-}
-- https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html?highlight=undecidablesuperclasses#ghc-flag--XUndecidableSuperClasses
-- from https://hackage.haskell.org/package/constraint-manip-0.1.0.0/docs/src/Control.ConstraintManip.html
-- To emulate a type function, but partially appliable
type MyC a b = (a ~ b)
-- Do
class (a ~ b) => MyC a b
instance (a ~ b) => MyC a b
@louispan
louispan / advancedOverlap.hs
Last active May 7, 2018 06:06
Haskell - Advanced Overlap
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
-- https://wiki.haskell.org/GHC/AdvancedOverlap
-- when using closed type family, data kinds, and proxy