Skip to content

Instantly share code, notes, and snippets.

View bkase's full-sized avatar

Brandon Kase bkase

View GitHub Profile
@bkase
bkase / algebraic-graphs.swift
Created January 5, 2018 05:58
Playing with Algebraic Graphs (port from Swift Sandbox)
// Paper located: https://github.com/snowleopard/alga-paper
infix operator <>: AdditionPrecedence
protocol Semigroup {
static func <>(lhs: Self, rhs: Self) -> Self
}
protocol Monoid: Semigroup {
static var empty: Self { get }
}
protocol CommutativeMonoid: Monoid { }
protocol BoundedSemilattice: CommutativeMonoid { }
@bkase
bkase / algorithm-m.swift
Created January 5, 2018 05:59
Type Inference Algorithm M (ported from Swift Sandbox)
// Write some awesome Swift code, or import libraries like "Foundation",
// "Dispatch", or "Glibc"
enum Result<E, T> {
case fail(E)
case success(T)
var succeeded: Bool {
switch self {
case .fail(_): return false
@bkase
bkase / algorithm-w.swift
Created January 5, 2018 06:00
Type Inference Algorithm W (ported from Swift Sandbox)
// Algorithm W
// From Principal Types for functional programs
// http://web.cs.wpi.edu/~cs4536/c12/milner-damas_principal_types.pdf
// by bkase
// STD library
infix operator <>: AdditionPrecedence
extension Dictionary {
static func <>(lhs: Dictionary, rhs: Dictionary) -> Dictionary {
var d: [Key: Value] = [:]
@bkase
bkase / hashconsing-sklansky.swift
Created January 5, 2018 06:02
Hashconsing and Sklansky (ported from Swift Sandbox)
// An exploration of http://okmij.org/ftp/tagless-final/sharing/sharing.pdf
// Specifically focusing on Hashconsing and Sklansky
typealias NodeId = Int
enum Node: Equatable, Hashable {
case nconst(Int)
case nvar(String)
case nadd(NodeId, NodeId)
var hashValue: Int {
@bkase
bkase / callback-based-effects.swift
Created January 5, 2018 06:06
Callback-based Effects (ported from Swift Sandbox)
/*
* A callback-base implementation of effectful computation that defers interpretation
* until after computation is expressed (so you can inject different interpreters
* onto the same computation).
*
* The downside is the results aren't expressed as values. They are callback calls.
* You probably don't actually want to use this, but it seems interesting to think about.
*
* Inspired by "Programming with Algebraic Effects and Handlers" by
* Andrej Bauer and Matija Pretnar 2012
@bkase
bkase / reified-monoid.swift
Created January 5, 2018 06:07
Reified Monoids (ported from Swift Sandbox)
// Thanks, Chris Eidhof for the idea
struct Monoid<T> {
let empty: T
let append: (T, T) -> T
func fold(arr: Array<T>) -> T {
return arr.reduce(self.empty, self.append)
}
}
@bkase
bkase / monoidal-cancellation.swift
Created January 5, 2018 06:08
Monoidal Cancellations (ported from Swift Sandbox)
// Write some awesome Swift code, or import libraries like "Foundation",
// "Dispatch", or "Glibc"
struct Monoid<T> {
let empty: T
let append: (T, T) -> T
func fold(_ arr: [T]) -> T {
return arr.reduce(empty, append)
}
@bkase
bkase / unary-indexed-collection.swift
Created January 5, 2018 06:10
Unary-indexed Collection (ported from Swift Sandbox)
// Write some awesome Swift code, or import libraries like "Foundation",
// "Dispatch", or "Glibc"
import Foundation
struct SampleCollection: LazyCollectionProtocol, RandomAccessCollection {
typealias Indices = DefaultRandomAccessIndices<SampleCollection>
let xs = ["a", "b", "c"]
@bkase
bkase / not-really-gadts-protocol.swift
Created January 12, 2018 09:32
"Gadts" with a base protocol
protocol Expr {
associatedtype A
func eval() -> A
}
struct Const<T>: Expr {
typealias A = T
let value: A
func eval() -> A {
return value
@bkase
bkase / Co.swift
Last active December 6, 2019 00:27
Co.swift seems to work. Read NaturalSubclass.swift for what doesn't work, Natural.swift for what seems to work, and Co_old.swift for where it breaks down
import Bow
// This version of Co seems to work even thoug it's a bit gross to deal with Any
// Think of this as an example of how to deal with rank2 type polymorphism -- you only
// deal with Any in the constructor, even `runCo` has a generic interface.
// newtype Co w a = Co (forall r. w (a -> r) -> r)
public final class ForCo {}
public final class CoPartial<W: Comonad>: Kind<ForCo, W> {}
public typealias CoOf<W: Comonad, A> = Kind<CoPartial<W>, A>