Created
December 18, 2017 19:27
-
-
Save dduan/117f9426c8032a997a0877312abf47fc to your computer and use it in GitHub Desktop.
Representing Algebraic Data Type?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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