- Markdown style
[](https://youtu.be/nTQUwghvy5Q)
- HTML style
<a href="http://www.youtube.com/watch?feature=player_embedded&v=nTQUwghvy5Q" target="_blank">
type term = | |
| Lam of (term -> term) | |
| Pi of term * (term -> term) | |
| Appl of term * term | |
| Ann of term * term | |
| FreeVar of int | |
| Star | |
| Box | |
let unfurl lvl f = f (FreeVar lvl) |
Worth a read for some more context.
Create the file in the root of the project (where your Package.swift
file lives as well), and use the following contents:
/// Package.xcconfig
func apply<A, B>(fn: (A) -> B, a: A) -> B { | |
fn(a) | |
} | |
func bluebird<A, B, C>(fn1: (A) -> B, fn2: (C) -> A, c: C) -> B { | |
fn1(fn2(c)) | |
} | |
func blackbird<A, B, C, D>(fn1: (C) -> D, fn2: (A, B) -> C, a: A, b: B) -> D { |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE TupleSections #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE RankNTypes #-} | |
import Control.Applicative | |
import Control.Comonad | |
import Data.Bifunctor |
#Every Single Option Under The Sun
// Playground - noun: a place where people can play | |
public func id<A>(x : A) -> A { | |
return x | |
} | |
public func error<A>(x : String) -> A { | |
assert(false, x) | |
} |
public func unsafeCoerce<A, B>(_ x : A) -> B { | |
return unsafeBitCast(x, to: B.self) | |
} | |
func Y<A>(_ f : @escaping (A) -> A) -> A { | |
return { (x : @escaping (A) -> A) in | |
return f((x(unsafeCoerce(x)))) | |
}({ (x : A) -> A in | |
let fn : (A) -> A = unsafeCoerce(x) | |
return f(fn(x)) |
Copy and paste the swift code below into a playground to experiment.
This is a very close emulation of Functor and Monad typeclasses in swift. As of Swift 1.2 and Xcode 6.3, this is no longer very fragile.
Unfortunately, the compiler cannot verify the types when passing a function to (>>=)
. We have to wrap the function in a closure and call it with an explicit argument to compile.
optionalDoubles >>= squareRoot // doesn't compile
optionalDoubles >>= { squareRoot($0) } // compiles