Skip to content

Instantly share code, notes, and snippets.

@JakobBruenker
JakobBruenker / Filter.hs
Created July 20, 2023 01:42
Generic Filter
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
module Filter where
import Data.Profunctor
import Data.Profunctor.Product
import Prelude hiding (filter)
import Control.Applicative
@JakobBruenker
JakobBruenker / roger.js
Last active July 1, 2023 15:13
Roger's fixed-point theorem in Javascript
/*
Constructive proof of Roger's fixed-point theorem in Javascript
===============================================================
N.B. You can load this file in node.js using `.load roger.js` to use the
functions defined here in a REPL.
Definition (Program)
--------------------
Below, a "program" will always be a Javascript function of one argument.
@JakobBruenker
JakobBruenker / visual.md
Last active January 4, 2024 03:13
The paths of homotopy type theory visualized

Path Visualization

As example, we're using the proof that there's a path between homotopy1 and homotopy2 from Exercise 4.

Instead of looking at a circle, let's look at a general path $p$ between some points $x, y : A$, just because it makes it less confusing to visualize. We'll come back to using the circle later on.

A path is a continuous function of type $[0, 1] \rightarrow A$. If we say $x$ is blue and $y$ is red, we can visualize the path $p$ as a colored image of the interval:

image

@JakobBruenker
JakobBruenker / GenericShow.hs
Last active March 19, 2022 21:17
Utility to show things without Show instance (but Data instance)
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE MonadComprehensions #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Data
import Data.Foldable
import Data.Functor
import Data.Maybe
@JakobBruenker
JakobBruenker / Example.hs
Last active July 31, 2021 23:15
Generating RIO-style Has-classes/lenses via template haskell
data WindowSize = MkWindowSize { windowWidth :: !Natural
, windowHeight :: !Natural
}
makeRioClassy ''WindowSize
-- This will generate
-- class (HasWindowWidth env, HasWindowHeight env)
-- => HasWindowSize env where windowSizeL :: Lens' env WindowSize
-- class HasWindowWidth env where ... (method signatures omitted for brevity from here on out)
-- class HasWindowHeight env
-- (as well as associated `id` instances, omitted for brevity for this and the other types)
-- Inspired by https://github.com/goldfirere/video-resources/blob/main/2021-07-06-zipWith/ZipWith.hs
{-# LANGUAGE StandaloneKindSignatures, DataKinds, TypeOperators,
TypeFamilies, UndecidableInstances, GADTs, ScopedTypeVariables,
TypeApplications, FlexibleInstances, FlexibleContexts,
ConstraintKinds, FunctionalDependencies, AllowAmbiguousTypes #-}
module Lift where
import Data.Kind
@JakobBruenker
JakobBruenker / Main.hs
Created December 20, 2020 14:48
AOC 2020 day 20
{-# LANGUAGE ScopedTypeVariables
, ViewPatterns
, LambdaCase
, BlockArguments
, StandaloneKindSignatures
, GADTs
, TypeOperators
, ImportQualifiedPost
, UndecidableInstances
, DerivingVia
{-# LANGUAGE NumDecimals, BlockArguments #-}
import System.Environment
import Data.List
f 0 = (length .) . filter
f 1 = count
f 2 = count'
f 3 = count''
@JakobBruenker
JakobBruenker / Dependent.hs
Last active November 25, 2020 22:19
Comparison of basic length-indexed vectors in Haskell and Java
{-# LANGUAGE GADTs, TypeFamilies, DataKinds, TypeOperators, StandaloneDeriving #-}
module Dependent where
data Nat = Z | S Nat
data Vec (n :: Nat) a where
Nil :: Vec Z a
Cons :: a -> Vec n a -> Vec (S n) a
{-# LANGUAGE TypeFamilies, UndecidableInstances, KindSignatures, DataKinds, TypeOperators, GADTs, RankNTypes, PolyKinds #-}
import Data.Kind
import Data.Type.Equality
data Nat = Z | S Nat
data SNat :: Nat -> Type where
SZ :: SNat Z
SS :: SNat n -> SNat (S n)