Skip to content

Instantly share code, notes, and snippets.

@julianpeeters
julianpeeters / avl.scala
Created February 9, 2024 01:52
AVL trees, intrinsically and extrinsically typed. Scala examples of exercises from CM 500 Logical Frameworks (see https://github.com/julianpeeters/hello-twelf/tree/main)
// AVL trees, intrinsically and extrinsically typed
/* "extrinsically typed" AVL trees
*
* avlTree : tree -> type.
* avlTree/leaf : avlTree leaf.
* avlTree/node-1: avlTree (node L R)
* <- height L (s H)
* <- height R H.
* avlTree/node0: avlTree (node L R)
@julianpeeters
julianpeeters / sorts.elf
Created February 8, 2024 17:41
Patterns of terms/judgments/theorems in a Twelf file
%Rob Simmons (he) (F2'23)
%9:12 AM
%Patterns of terms/judgments/theorems in a Twelf file
nat: type. % pattern is "a0 : k0"
z: nat. % pattern is "c0 : tau"
s: nat -> nat. % pattern is "c0 : tau"
two : nat = s (s z). % pattern is "c0 : tau = t"
plus : nat -> nat -> nat -> type. % pattern is "a1 : k1"
@julianpeeters
julianpeeters / bbt.scala
Last active February 9, 2024 00:02
Basic proofs of balanced binary trees. Scala examples of exercises from CM 500 Logical Frameworks (see https://github.com/julianpeeters/hello-twelf/tree/main)
sealed trait Nat
case object Zero extends Nat
case class Successor[N <: Nat](nat: N) extends Nat
/* Max, implemented in Twelf, then as a Scala def, and finally as a Scala match types
*
* max : nat -> nat -> nat -> type.
* %mode max +N1 +N2 -N3.
* max/z1 : max z N N.
* max/z2 : max N z N.
@julianpeeters
julianpeeters / even_or_odd.scala
Last active February 7, 2024 20:17
Scala example of Exercise 3 from CM 500 Logical Frameworks (see https://github.com/julianpeeters/hello-twelf/tree/main)
sealed trait Nat
case object Zero extends Nat
case class Successor[A <: Nat](a: A) extends Nat
sealed trait Odd[+N]
case class OddS[N <: Nat](even: Even2[N]) extends Odd[Successor[N]]
sealed trait Even2[+N]
case object Even2Z extends Even2[Zero.type]
case class Even2S[N <: Nat](odd: Odd[N]) extends Even2[Successor[N]]
@julianpeeters
julianpeeters / nat_even2.scala
Last active February 6, 2024 04:05
Scala example of Exercise 1 from CM 500 Logical Frameworks (see https://github.com/julianpeeters/hello-twelf/tree/main)
sealed trait Nat
case object Zero extends Nat
case class Successor[N <: Nat](nat: N) extends Nat
sealed trait Even[+N]
case object EvenZ extends Even[Zero.type]
case class EvenS[N <: Nat](even: Even[N]) extends Even[Successor[Successor[N]]]
sealed trait Odd[N]
case class OddS[N <: Nat](even: Even2[N]) extends Odd[Successor[N]]
@julianpeeters
julianpeeters / StoreRepresentable.scala
Created May 3, 2023 03:51
Example of the inability of Store to form a Representable (yet is itself a sum of representables)
// Unable to implement `tabulate` without a value to provide as the index
implicit def representableForStore[S]: Representable.Aux[[A] =>> Store[S, A], S] =
new Representable[[A] =>> Store[S, A]]:
override type Representation = S
override val F: Functor[[A] =>> Store[S, A]] = Functor[[A] =>> Store[S, A]]
override def tabulate[A](f: S => A): Store[S, A] = ???
override def index[A](f: Store[S, A]): S => A = (s: S) => f.peek(s)
trait Coercible[A, B] {
def coerce(a: A): B
}
def coerce[A, B](a: A)(implicit coerceAB: Coercible[A, B]): B = {
coerceAB.coerce(a)
}
trait Newtype[+T] {
val value: T
import cats.effect.{Concurrent, Sync}
import cats.effect.concurrent.{Deferred, Ref}
import cats.effect.implicits._
import cats.implicits._
import scala.collection.mutable.Queue
trait FlushableQueue[F[_], A] {
def enqueue(a: A): F[Unit]
def dequeueAll: F[List[A]]
}
import cats.implicits._
import cats.data._
import cats.effect._
import cats.effect.concurrent._
abstract class Alg[F[_]]{
def up: F[Unit]
def down: F[Unit]
def get: F[Int]
}
category
arrow
object
initial object
A B C