Skip to content

Instantly share code, notes, and snippets.

View zraffer's full-sized avatar
🚀
let it be

Bay Raktar zraffer

🚀
let it be
View GitHub Profile
@zraffer
zraffer / encodings.md
Created May 19, 2020 14:22 — forked from zmactep/encodings.md
Number encodings

Alternative to the Church, Scott and Parigot encodings of data on the Lambda Calculus.

When it comes to encoding data on the pure λ-calculus (without complex extensions such as ADTs), there are 3 widely used approaches.

Church Encoding

The Church Encoding, which represents data structures as their folds. Using Caramel’s syntax, the natural number 3 is, for example. represented as:

0 c0 = (f x -> x)
1 c1 = (f x -> (f x))
2 c2 = (f x -> (f (f x)))
@zraffer
zraffer / truffle-material.md
Created April 20, 2018 17:14 — forked from smarr/truffle-material.md
Truffle: Languages and Material
@zraffer
zraffer / FreeMonad.scala
Created August 22, 2016 11:17 — forked from jdegoes/FreeMonad.scala
A pedagogical free(er) monad in 23 lines of Scala
sealed trait Free[F[_], A] { self =>
final def map[B](ab: A => B): Free[F, B] = Free.flatMap(self, ab andThen (Free.point[F, B](_)))
final def flatMap[B](afb: A => Free[F, B]): Free[F, B] = Free.flatMap(self, afb)
final def interpret[G[_]: Monad](fg: F ~> G): G[A] = self match {
case Free.Point(a0) => a0().point[G]
case Free.Effect(fa) => fg(fa)
case fm : Free.FlatMap[F, A] =>
val ga0 = fm.fa.interpret[G](fg)
ga0.flatMap(a0 => fm.afb(a0).interpret[G](fg))
}
@zraffer
zraffer / Para2.idr
Created March 7, 2016 18:16 — forked from pbl64k/Para2.idr
Parigot/Church-Scott encoding of naturals in CoC (by way of Idris)
Fix : (Type -> Type) -> Type
Fix f = {x : Type} -> (f x -> x) -> x
fold : Functor f => {x : Type} -> (f x -> x) -> Fix f -> x
fold k t = t k
embed : Functor f => f (Fix f) -> Fix f
embed s k = k (map (fold k) s)
project : Functor f => Fix f -> f (Fix f)
@zraffer
zraffer / Para.idr
Created March 4, 2016 12:07 — forked from pbl64k/Para.idr
%default total
data Mu : (Type -> Type) -> Type where
In : f (Mu f) -> Mu f
out : Mu f -> f (Mu f)
out (In x) = x
data LstF : Type -> Type -> Type where
LF : ({c : Type} -> (Maybe (a, Unit -> c, b) -> c) -> c) -> LstF a b
@zraffer
zraffer / para.hs
Created March 3, 2016 13:57 — forked from pbl64k/para.hs
Possibly pointless? encoding of lists as their own paramorphisms.
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE Rank2Types #-}
import Data.Maybe
data List a = L (forall b. (Maybe (a, (b, List a)) -> b) -> b)
nil :: List a
nil = L (\f -> f Nothing)