Skip to content

Instantly share code, notes, and snippets.

@dcastro
dcastro / singleton-types.md
Last active Mar 8, 2021
singleton-types.md
View singleton-types.md

This tutorial assumes you're already familiar with Haskell's kind system and extensions like DataKinds. If not, please read my blog post first </shameless-plug>.

Say you want to model a TCP connection, and it can be in either an "open" or "closed" status.

data Connection = MkConnection ...
@dcastro
dcastro / log.log
Created Feb 12, 2021
nix + custom build type
View log.log
$ nix-build ci.nix -A all-components --no-out-link
building '/nix/store/bflfb6jrxykrp53fkd5f0gd0d45z776s-git-ls-files.drv'...
trace: To make this a fixed-output derivation but not materialized, set `stack-sha256` to the output of /nix/store/w4f2lcmndg68m64cdrhm1vkhz2s216i7-calculateSha
trace: To materialize the output entirely, pass a writable path as the `materialized` argument and pass that path to /nix/store/y1w6r7c4wrvhllzmz4k3h6g30b4hph74-generateMaterialized
building '/nix/store/36nhcnn93ik0zdsmbcbrpqbq0ajl5lsc-morley-stack-to-nix-pkgs.drv'...
substituteStream(): WARNING: pattern '/nix/store/fn96af9026mkyk28pj3ivwd9drzd247m-morley' doesn't match anything in file '/nix/store/zyzl3v6lww8a81qhmv052075a4qbbj7n-morley-stack-to-nix-pkgs/default.nix'
substituteStream(): WARNING: pattern '/nix/store/fn96af9026mkyk28pj3ivwd9drzd247m-morley' doesn't match anything in file '/nix/store/zyzl3v6lww8a81qhmv052075a4qbbj7n-morley-stack-to-nix-pkgs/pkgs.nix'
these derivations will be built:
@dcastro
dcastro / a.log
Created Feb 9, 2021
Doctest + mixins
View a.log
/home/dc/.ghcup/bin/ghc --make -fbuilding-cabal-package -O -static -dynamic-too -dynosuf dyn_o -dynhisuf dyn_hi -outputdir /home/dc/dev/other/doctest-mixins/dist-newstyle/build/x86_64-linux/ghc-8.8.3/doctest-mixins-0.1.0.0/build -odir /home/dc/dev/other/doctest-mixins/dist-newstyle/build/x86_64-linux/ghc-8.8.3/doctest-mixins-0.1.0.0/build -hidir /home/dc/dev/other/doctest-mixins/dist-newstyle/build/x86_64-linux/ghc-8.8.3/doctest-mixins-0.1.0.0/build -stubdir /home/dc/dev/other/doctest-mixins/dist-newstyle/build/x86_64-linux/ghc-8.8.3/doctest-mixins-0.1.0.0/build -i -i/home/dc/dev/other/doctest-mixins/dist-newstyle/build/x86_64-linux/ghc-8.8.3/doctest-mixins-0.1.0.0/build -isrc -i/home/dc/dev/other/doctest-mixins/dist-newstyle/build/x86_64-linux/ghc-8.8.3/doctest-mixins-0.1.0.0/build/autogen -i/home/dc/dev/other/doctest-mixins/dist-newstyle/build/x86_64-linux/ghc-8.8.3/doctest-mixins-0.1.0.0/build/global-autogen -I/home/dc/dev/other/doctest-mixins/dist-newstyle/build/x86_64-linux/ghc-8.8.3/doctest-mixins-0.1.0
@dcastro
dcastro / rightComb.hs
Last active Jan 18, 2021
TyFam inductive case
View rightComb.hs
import Data.Vinyl (Rec(..))
data Tag = TagInt | TagPair Tag Tag
data Exp (t :: Tag) where
EInt :: Int -> Exp 'TagInt
EPair :: Exp t1 -> Exp t2 -> Exp ('TagPair t1 t2)
type family RightComb (tags :: [Tag]) :: Tag where
RightComb '[ x, y ] = 'TagPair x y
@dcastro
dcastro / universal_existential.md
Created Dec 17, 2020
Universally vs existentially quantified type variables
View universal_existential.md

A type variable can be either universally ("for all x, then ...") or existentially quantified ("there exists some x, ...").

(If you're a maths persons, this corresponds to ∀x. x and ∃x. x, respectively)

In haskell, universally quantified type vars are represented with a forall ON THE LEFT of a type signature:

head :: forall a. [a] -> Maybe a
@dcastro
dcastro / Problem.hs
Last active Sep 29, 2020
Capturing type variables in TemplateHaskell
View Problem.hs
import Language.Haskell.TH.Lib
import Language.Haskell.TH.Syntax
module Problem where
data PP a where
PP :: forall a. Typeable a => PP a
instance Lift (PP a) where
lift PP = [|PP|]
@dcastro
dcastro / approve.log
Created Sep 25, 2020
Log - Optimized Stablecoin FA1.2
View approve.log
Calling approve
[Debug] [Morley.Client.TezosClient.Impl.getAlias#254] Getting an alias for tz1TfLF61NboKZYbU7hxX3C1jXw45im6d8Kt
[Debug] [Morley.Client.TezosClient.Impl.callTezosClient#447] Running: tezos-client -A carthage.testnet.tezos.serokell.team -P 8732 --mode mockup list known contracts
[Debug] [Morley.Client.TezosClient.Impl.callTezosClient#453] stdout:
nettest.Stablecoin.Stablecoin FA1.2: KT1VuoysRj6tWFXAjrbQd67YMnTMi3SpNTdC
nettest.Stablecoin.natConsumer: KT1GnUjbtLqQgeupcDCReYmzTa86mkvwgoRa
nettest.Stablecoin.nettest.transferlist_internal: KT1QQAymdeefTj3RwTkvjvGWTDWJPenoiG7k
nettest.Stablecoin.nettest.Stablecoin: KT1Q4DccWWaM15nc9iaMDcwLNj3m85k8euVF
helper: KT1DW3Fq5m7q1bn8QgjhSX9iEwcBAK82bva6
nettest.Stablecoin.chainIdContract: KT1KUhbZCatB5X6svDpXJ4gAX5a9X1LyU2yv
@dcastro
dcastro / approve.log
Last active Sep 25, 2020
Log - Stablecoin FA1.2
View approve.log
Calling approve
[Debug] [Morley.Client.TezosClient.Impl.getAlias#254] Getting an alias for tz1TfLF61NboKZYbU7hxX3C1jXw45im6d8Kt
[Debug] [Morley.Client.TezosClient.Impl.callTezosClient#447] Running: tezos-client -A carthage.testnet.tezos.serokell.team -P 8732 --mode mockup list known contracts
[Debug] [Morley.Client.TezosClient.Impl.callTezosClient#453] stdout:
nettest.Stablecoin.Stablecoin FA1.2: KT1GrQC4nGZckcfeCtLu2sKphsNsoxUoLNtK
nettest.Stablecoin.natConsumer: KT1GdZ9EoAY2o5jHN2x4rACwTDAwcCGZV7bn
helper: KT1DW3Fq5m7q1bn8QgjhSX9iEwcBAK82bva6
nettest.Stablecoin.chainIdContract: KT1KUhbZCatB5X6svDpXJ4gAX5a9X1LyU2yv
nettest.Stablecoin.permitCounterConsumer: KT1AihogGMjD3R8y5TK31Z6iJKXwCThK4i5D
nettest.Stablecoin.nettest.Stablecoin: KT1HBxv2cjKjY1nWALMbKKMdsymrdTf8hxtG
@dcastro
dcastro / amm.scala
Last active Mar 3, 2020
ammonite log
View amm.scala
Loading...
Welcome to the Ammonite Repl 1.3.2
(Scala 2.12.7 Java 1.8.0_212)
If you like Ammonite, please support our development at www.patreon.com/lihaoyi
@ import $ivy.`org.scala-lang.modules::scala-parser-combinators:1.1.2`
import $ivy.$
@ {
sealed trait Expr
case class Term(term: String) extends Expr
@dcastro
dcastro / io.md
Last active Feb 21, 2020
IO vs Future and Referential transparency
View io.md

The problem

These programs are safe to refactor:

object p1 {
  val x = 1 + 123
  val y = 1 + 123
}