Skip to content

Instantly share code, notes, and snippets.

View yangzhixuan's full-sized avatar

Zhixuan Yang yangzhixuan

  • Imperial College London
  • Web
View GitHub Profile
@yangzhixuan
yangzhixuan / CList.agda
Created April 12, 2024 19:19
Refined Church-encoded lists predicatively
open import Agda.Builtin.Sigma
open import Agda.Builtin.Equality
postulate
funext : ∀ {a b} {A : Set a} {B : A → Set b} → {f g : (x : A) → B x}
→ ((x : A) → f x ≡ g x) → f ≡ g
uip : ∀ {a} {A : Set a} {x y : A} → {p q : x ≡ y} → p ≡ q
@yangzhixuan
yangzhixuan / codeinmath.lhs
Created April 7, 2024 22:16
Using lhs2tex code in mathmode
\documentclass{article}
\usepackage{amsmath}
%include polycode.fmt
\newenvironment{autohscode}%
{\relax\ifmmode\expandafter\pmboxed\else\expandafter\plainhscode\fi}%
{\relax\ifmmode\expandafter\endpmboxed\else\expandafter\endplainhscode\fi}
@yangzhixuan
yangzhixuan / MixAlg.hs
Created January 4, 2024 00:36
Mixing two arbitrary functor algebras (using codensity lifting along the fibration of functor algebras)
{-# LANGUAGE ImpredicativeTypes #-}
{-# LANGUAGE GADTs #-}
data Free f a where
V :: a -> Free f a
O :: f (Free f a) -> Free f a
fold :: Functor f => (a -> c) -> (f c -> c) -> Free f a -> c
fold k alg (V a) = k a
fold k alg (O o) = alg (fmap (fold k alg) o)
@yangzhixuan
yangzhixuan / Clocked.hs
Last active June 10, 2023 19:05
Clocked guarded recursion in Haskell
{-# LANGUAGE DataKinds, ImpredicativeTypes #-}
-- Clocked guarded corecursion in the style of Atkey and McBride
-- https://bentnib.org/productive.pdf
-------- Library code -----------
data Clock
newtype Later (k :: Clock) (a :: *) = L a
@yangzhixuan
yangzhixuan / TArray.hs
Created April 21, 2023 11:17
Treap-based array
import Control.Monad (foldM)
import Prelude hiding (sum)
import Control.Monad.Random (MonadRandom(getRandom))
type Pos = Int
data TArray = E | N TArray -- Left subtree
Int -- Data payload (not position!)
Int -- Priority
Int -- Size of the tree
@yangzhixuan
yangzhixuan / agda.sty
Created February 24, 2023 20:05
A customised version of agda.sty with some new features: centred code blocks, italic fonts, etc
% ----------------------------------------------------------------------
% Some useful commands when doing highlighting of Agda code in LaTeX.
% ----------------------------------------------------------------------
\ProvidesPackage{agda}
\RequirePackage{ifxetex, ifluatex, xifthen, xcolor, polytable, etoolbox,
calc, environ, xparse, xkeyval, amsmath}
% https://tex.stackexchange.com/questions/47576/combining-ifxetex-and-ifluatex-with-the-logical-or-operation
@yangzhixuan
yangzhixuan / ndet.rkt
Created June 4, 2022 08:16
Monadic reflection/reification in Racket using delimited control
#lang racket
(require "reflection.rkt")
; unit : x -> [x]
(define unit (lambda (x) (cons x '())))
; bind : [x] -> (x -> [y]) -> [y]
(define bind (lambda (m f) (apply append (map f m))))
@yangzhixuan
yangzhixuan / AdjointFolds.hs
Last active March 20, 2022 18:28
A Haskell implementation of adjoint folds and some interface for category theory.
{-# LANGUAGE KindSignatures, GADTs, TypeOperators, TypeFamilies, UndecidableInstances #-}
{-# LANGUAGE FlexibleContexts, MultiParamTypeClasses, FlexibleInstances #-}
{-# LANGUAGE ConstraintKinds, DeriveFunctor #-}
import Prelude hiding (Functor)
import qualified Prelude (Functor)
-- This file explores doing category theory in Haskell by implementing
-- Ralf Hinze et al.'s adjoint folds [1]:
--
@yangzhixuan
yangzhixuan / FastApp.hs
Last active May 7, 2022 07:08
Efficient Free Applicatives from Okasaki's Functional Data Structures
{-# LANGUAGE GADTs, RankNTypes, TypeOperators, ScopedTypeVariables, KindSignatures #-}
module FastApp where
import Prelude hiding (head, tail)
import Control.Applicative
-- Chris Okasaki invented many cool and efficient functional data structures
-- in his book Purely Functional Data Structures.
-- Among them, one is catenable lists supporting amortised O(1)-time |(++)|,
@yangzhixuan
yangzhixuan / CSList.hs
Last active November 12, 2020 09:05
Church-Scott representation of lists, which supports O(1) time `(++)` and `tail`
{-# LANGUAGE RankNTypes #-}
-- Church-Scott encoding combines the Church encoding and Scott encoding of datatypes
-- See: http://www.cs.ru.nl/~herman/PUBS/ChurchScottDataTypes.pdf
-- Although the encoding in the paper seems not to support (++) efficiently, it can be fixed as follows.
import Prelude hiding ((++), head, tail)
newtype CSList a = CSList { run :: forall x . x -> CSList a -> (a -> CSList a -> x -> x) -> x}