Skip to content

Instantly share code, notes, and snippets.

@dduan
Created December 18, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dduan/117f9426c8032a997a0877312abf47fc to your computer and use it in GitHub Desktop.
Save dduan/117f9426c8032a997a0877312abf47fc to your computer and use it in GitHub Desktop.
Representing Algebraic Data Type?
struct Decl {
struct Option {
enum Segment {
case one(String, [String])
indirect case map(Segment, Segment)
}
typealias Field = (name: String?, type: Segment)
let name: String
let fields: [Field]
}
let name: String
let variables: [String]
let options: [Option]
}
enum Type {
enum Primitive {
case int
case bool
}
case primitive(Primitive)
case declared(Decl)
case variable(String)
indirect case map(Type, Type)
}
typealias Environment = [String: Type]
var global: Environment = ["Int": .primitive(.int), "Bool": .primitive(.bool)]
Decl(
name: "Suit",
variables: [],
options: [
Decl.Option(name: "spade", fields: []),
Decl.Option(name: "heart", fields: []),
Decl.Option(name: "diamond", fields: []),
Decl.Option(name: "club", fields: [])])
Decl(
name: "Optional",
variables: ["T"],
options: [
Decl.Option(name: "none", fields: []),
Decl.Option(name: "some", fields: [
Decl.Option.Field(name: nil, type: .one("T", []))])])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment