Hello,

I have created an RFC for proper algebraic data types and structural pattern 
matching a la Swift or Rust. It's available here: 
<https://github.com/nim-lang/RFCs/issues/525>

I go into much more detail on the RFC itself, but here is an example of code it 
would allow you to write:
    
    
    # An implementation of the simple lambda calculus
    # (typechecker and Type union omitted)
    
    type Ident = string
    
    type Expr = ref union
      Constant: Term
      Variable: tuple[id: Ident]
      # Annotation: tuple[expr: Expr, kind: Type]
      Abstraction: tuple[param: Ident, fn: Expr]
      Application: tuple[fn, arg: Expr]
      Conditional: tuple[cond, `if`, `else`: Expr]
    
    type Term = union
      Unittype Ident = string
    
    type Expr = ref union
      Constant: Term
      Variable: tuple[id: Ident]
      # Annotation: tuple[expr: Expr, kind: Type]
      Abstraction: tuple[param: Ident, fn: Expr]
      Application: tuple[fn, arg: Expr]
      Conditional: tuple[cond, `if`, `else`: Expr]
    
    type Term = union
      Unit
      Boolean: bool
      Natural: uint
      Integer: int
      Float: float
      String: tuple[len: uint, cap: uint, data: seq[uint]]
      Enum: tuple[val: uint, data: seq[Type]]
      Struct: Table[Ident, Term]
      Function: Expr
    
    var globalSymbolTable = initTable[Ident, Term]()
    func execute(ast: Expr): Term =
      case ast
      of Constant(term):
        return term
      of Variable(id):
        return globalSymbolTable[id]
      # other structures of applications + abstractions are invalid
      of Application(Abstraction(param, fn), arg):
        globalSymbolTable[param] = execute(arg)
        return execute(fn)
      of Conditional(cond, `if`: ifClause, `else`: elseClause):
        if execute(cond) == Boolean(true):
          return execute(ifClause)
        else:
          return execute(elseClause)
      else:
        echo "Failed to execute AST! Improper structure"
        quit()
      Integer: int
      Float: float
      String: tuple[len: uint, cap: uint, data: seq[uint]]
      Enum: tuple[val: uint, data: seq[Type]]
      Struct: Table[Ident, Term]
      Function: Expr
    
    var globalSymbolTable = initTable[Ident, Term]()
    func execute(ast: Expr): Term =
      case ast
      of Constant(term):
        return term
      of Variable(id):
        return globalSymbolTable[id]
      # other structures of applications + abstractions are invalid
      of Application(Abstraction(param, fn), arg):
        globalSymbolTable[param] = execute(arg)
        return execute(fn)
      of Conditional(cond, `if`: ifClause, `else`: elseClause):
        if execute(cond) == Boolean(true):
          return execute(ifClause)
        else:
          return execute(elseClause)
      else:
        echo "Failed to execute AST! Improper structure"
        quit()
    
    
    Run

Please let me know what you think of this proposal on the Github issue or here 
in the forum.

Reply via email to