Skip to content

Instantly share code, notes, and snippets.

@louispan
louispan / minimal_ghc_ghcjs_nix.sh
Last active Jun 24, 2021
Install Cabal, GHC, or GHCJS with one Nix command
View minimal_ghc_ghcjs_nix.sh
# 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 Jun 18, 2018
Concurrently interpret polymorphic variant of commands
View ConcurrentVariantInterpreter.hs
{-# 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 Jun 18, 2018
Interpret polymorphic variant of commands using ContT and State
View VariantInterpreter.hs
{-# 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 Apr 15, 2018
Example of a reader & state of Many items
View Many ReaderT and StateT.hs
{-# 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 Mar 7, 2018
Combine reading and producing monads
View ReaderProducer.hs
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 Feb 19, 2018
Interpreting without continuation monad
View InterpreterWithout.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilyDependencies #-}
module Main where
@louispan
louispan / TypeclassInterpreter.hs
Last active Feb 19, 2018
Interpreting using typeclasses
View TypeclassInterpreter.hs
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilyDependencies #-}
module Main where
@louispan
louispan / ContMonadInterpreter.hs
Last active Aug 16, 2019
Continuation monad interpreter
View ContMonadInterpreter.hs
{-# 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
View partialTypeFun.hs
{-# 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
Haskell - Advanced Overlap
View advancedOverlap.hs
{-# 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