Skip to content

Instantly share code, notes, and snippets.

@cbarrett
Last active January 17, 2017 04:56
Show Gist options
  • Save cbarrett/4746a64830fd4ac117b8a28a4c018417 to your computer and use it in GitHub Desktop.
Save cbarrett/4746a64830fd4ac117b8a28a4c018417 to your computer and use it in GitHub Desktop.
Notes from my stream on 2017/01/16
// Dhall, a terminating configuration language
// Syntax: https://github.com/Gabriel439/Haskell-Dhall-Library/blob/master/src/Dhall/Core.hs
// This gist's history:
// 1. Contents at stream end
// 2. Clean up free vs. bound variables
enum Expr {
case constant(Int)
case variable(Bound)
indirect case `let`(Free, equalTo: Expr, in: Expr)
}
final class Free {
func ref() -> Bound {
return Bound(to: self)
}
// Debugging
var name: String
init(_ name: String) {
self.name = name
}
}
struct Bound {
let to: Free
// Debugging
var name: String {
return to.name
}
}
extension Expr {
var debugDescription: String {
switch self {
case let .constant(c):
return "\(c)"
case let .variable(v):
return v.name
case let .`let`(v, e, b):
return "let \(v.name) = \(e.debugDescription) in \(b.debugDescription)"
}
}
}
// let x = 5 in let y = x in y
// evaluates to: 5
func makeY(_ x: Free) -> Expr {
let y = Free("y")
return Expr.`let`(y, equalTo: .variable(x.ref()), in: .variable(y.ref()))
}
let x = Free("x")
let e = Expr.`let`(x, equalTo: .constant(5), in: makeY(x))
e.debugDescription
//switch e.normal {
//case let .variable(v):
// v.value
//default:
// break
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment