Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell-like as-patterns in Swift?

Does Swift have anything similar to Haskell's as-patterns used in pattern matching? I'm trying to get rid of the second switch statement in the following piece of code by using a nested-pattern:

indirect enum Type: CustomStringConvertible {
  case Int
  case Fun(Type, Type)

  var description: String {
    switch self {
      case .Int: return "int"
      case .Fun(let p, let r):
        switch p {
          case .Fun(_): return "(\(p)) -> \(r)"
          case _: return "\(p) -> \(r)"
        }
    }
  }
}

Type.Int                             // "int"
Type.Fun(.Int, .Int)                 // "int -> int"
Type.Fun(Type.Fun(.Int, .Int), .Int) // "(int -> int) -> int"

The Haskell equivalent, using as-patterns, would be this:

data Type =
    Int
  | Fun Type Type

desc :: Type -> String
desc t =
  case t of
    Int -> "int"
    Fun (p @ (Fun _ _)) r -> "(" ++ desc p ++ ") -> " ++ desc r
    Fun p r -> desc p ++ " -> " ++ desc r
like image 275
Ionuț G. Stan Avatar asked Nov 07 '22 22:11

Ionuț G. Stan


1 Answers

Not the same as the Haskell as-pattern, but you can get rid of the second switch statement with a nested pattern like this:

var description: String {
    switch self {
    case .Int: return "int"
    case .Fun(.Fun(let p, let q), let r): return "(\(Type.Fun(p, q))) -> \(r)"
    case .Fun(let p, let r): return "\(p) -> \(r)"
    }
}

or by rearranging the cases:

var description: String {
    switch self {
    case .Int: return "int"
    case .Fun(.Int, let r): return "int -> \(r)"
    case .Fun(let p, let r): return "(\(p)) -> \(r)"
    }
}
like image 81
Martin R Avatar answered Nov 15 '22 08:11

Martin R