|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Witness Parameters and Arguments" |
| 4 | +--- |
| 5 | + |
| 6 | +Witness parameters is a new syntax to define implicit parameters. Unlike traditional implicit parameters, witness parameters come with specific syntax for applications, which mirrors the parameter syntax. |
| 7 | + |
| 8 | +A witness parameter list starts with a dot ‘.’ and is followed by a normal parameter list. Analogously, a witness argument list also starts with a ‘.’ and is followed by a normal argument list. Example: |
| 9 | +```scala |
| 10 | +def maximum[T](xs: List[T]) |
| 11 | + .(cmp: Ord[T]): T = |
| 12 | + xs.reduceLeft((x, y) => if (x < y) y else x) |
| 13 | + |
| 14 | +def decending[T].(asc: Ord[T]): Ord[T] = new Ord[T] { |
| 15 | + def compareTo(this x: Int)(y: Int) = asc.compareTo(y)(x) |
| 16 | +} |
| 17 | + |
| 18 | +def minimum[T](xs: List[T]).(cmp: Ord[T]) = |
| 19 | + maximum(xs).(descending) |
| 20 | +``` |
| 21 | +The example shows three methods that each have a witness parameter list for `Ord[T]`. |
| 22 | +The `minimum` method's right hand side contains witness arguments `.(descending)`. |
| 23 | +As is the case for implicit arguments, witness arguments can be left out. For instance, |
| 24 | +given `xs: List[Int]`, the following calls are all possible (and they all normalize to the last one:) |
| 25 | +```scala |
| 26 | +maximum(xs) |
| 27 | +maximum(xs).(descending) |
| 28 | +maximum(xs).(descending.(IntOrd)) |
| 29 | +``` |
| 30 | +Unlike for implicit parameters, witness arguments must be passed using the `.(...)` syntax. So the expression `maximum(xs)(descending)` would give a type error. |
| 31 | + |
| 32 | +Witness parameters translate straightforwardly to implicit parameters. Here are the previous three method definitions again, this time formulated using implicit parameters. |
| 33 | +```scala |
| 34 | +def maximum[T](xs: List[T]) |
| 35 | + (implicit cmp: Ord[T]): T = |
| 36 | + xs.reduceLeft((x, y) => if (x < y) y else x) |
| 37 | + |
| 38 | +def descending[T](implicit asc: Ord[T]): Ord[T] = new Ord[T] { |
| 39 | + def compareTo(this x: T)(y: T) = asc.compareTo(y)(x) |
| 40 | +} |
| 41 | + |
| 42 | +def minimum[T](xs: List[T])(implicit cmp: Ord[T]) = |
| 43 | + maximum(xs)(descending) |
| 44 | +``` |
| 45 | + |
| 46 | +## Anonymous Witness Parameters |
| 47 | + |
| 48 | +The `<name> :` part of a witness parameter can be left out. For instance, the `minimum` and `maximum` method definitions could be abbreviated to |
| 49 | +```scala |
| 50 | +def maximum[T](xs: List[T]).(_: Ord[T]): T = |
| 51 | + xs.reduceLeft((x, y) => if (x < y) y else x) |
| 52 | + |
| 53 | +def minimum[T](xs: List[T]).(_: Ord[T]) = |
| 54 | + maximum(xs).(descending) |
| 55 | +``` |
| 56 | + |
| 57 | +## Summoning a Witness |
| 58 | + |
| 59 | +The `implicitly` method, defined in `Predef` computes an implicit value for a given type. Keeping with the "witness" terminology, it seems apt to inroduce the name `summon` for this operation. So `summon[T]` summons a witness for `T`, in the same way as `implicitly[T]`. |
| 60 | +The definition of `summon` is straightforward: |
| 61 | +```scala |
| 62 | +def summon[T].(x: T) = x |
| 63 | +``` |
| 64 | + |
| 65 | +## Implicit Closures and Function Types |
| 66 | + |
| 67 | +A period ‘.’ in front of a parameter list also marks implicit closures and implicit function types. Examples for types: |
| 68 | +```scala |
| 69 | +.Context => T |
| 70 | +.A => .B => T |
| 71 | +.(A, B) => T |
| 72 | +.(x: A, y: B) => T |
| 73 | +``` |
| 74 | +Examples for closures: |
| 75 | +```scala |
| 76 | +.ctx => ctx.value |
| 77 | +.(ctx: Context) => ctx.value |
| 78 | +.(a: A, b: B) => t |
| 79 | +``` |
| 80 | + |
| 81 | +## Syntax |
| 82 | + |
| 83 | +Here is the new syntax for witness definitions, parameters and arguments, seen as a delta from the [standard context free syntax of Scala 3](http://dotty.epfl.ch/docs/internals/syntax.html). |
| 84 | +``` |
| 85 | +TmplDef ::= ... |
| 86 | + | ‘witness’ WitnessDef |
| 87 | +WitnessDef ::= [id] WitnessClauses [‘for’ [ConstrApps]] TemplateBody |
| 88 | + | [id] WitnessClauses [‘for’ Type] ‘=’ Expr |
| 89 | + | id WitnessClauses ‘for’ Type |
| 90 | +WitnessClauses ::= [DefTypeParamClause] [‘with’ DefParams] |
| 91 | +
|
| 92 | +ClsParamClause ::= ... |
| 93 | + | ‘.’ ‘(’ ClsParams ‘)’ |
| 94 | +DefParamClause ::= ... |
| 95 | + | ‘.’ ‘(’ DefParams ‘)’ |
| 96 | +Type ::= ... |
| 97 | + | ‘.’ FunArgTypes ‘=>’ Type |
| 98 | +Expr ::= ... |
| 99 | + | ‘.’ FunParams ‘=>’ Expr |
| 100 | +
|
| 101 | +SimpleExpr1 ::= ... |
| 102 | + | SimpleExpr1 ‘.’ ParArgumentExprs |
| 103 | +``` |
| 104 | + |
| 105 | +## More Examples |
| 106 | + |
| 107 | +Semigroups and monoids: |
| 108 | +```scala |
| 109 | +trait SemiGroup[T] { |
| 110 | + def combine(this x: T)(y: T): T |
| 111 | +} |
| 112 | +trait Monoid[T] extends SemiGroup[T] { |
| 113 | + def unit: T |
| 114 | +} |
| 115 | + |
| 116 | +witness for Monoid[String] { |
| 117 | + def combine(this x: String)(y: String): String = x.concat(y) |
| 118 | + def unit: String = "" |
| 119 | +} |
| 120 | + |
| 121 | +def sum[T: Monoid](xs: List[T]): T = |
| 122 | + xs.foldLeft(summon[Monoid[T]].unit)(_.combine(_)) |
| 123 | +``` |
| 124 | +Functors and monads: |
| 125 | +```scala |
| 126 | +trait Functor[F[_]] { |
| 127 | + def map[A, B](this x: F[A])(f: A => B): F[B] |
| 128 | +} |
| 129 | + |
| 130 | +trait Monad[F[_]] extends Functor[F] { |
| 131 | + def flatMap[A, B](this x: F[A])(f: A => F[B]): F[B] |
| 132 | + def map[A, B](this x: F[A])(f: A => B) = x.flatMap(f `andThen` pure) |
| 133 | + |
| 134 | + def pure[A](x: A): F[A] |
| 135 | +} |
| 136 | + |
| 137 | +witness ListMonad for Monad[List] { |
| 138 | + def flatMap[A, B](this xs: List[A])(f: A => List[B]): List[B] = |
| 139 | + xs.flatMap(f) |
| 140 | + def pure[A](x: A): List[A] = |
| 141 | + List(x) |
| 142 | +} |
| 143 | + |
| 144 | +witness ReaderMonad[Ctx] for Monad[[X] => Ctx => X] { |
| 145 | + def flatMap[A, B](this r: Ctx => A)(f: A => Ctx => B): Ctx => B = |
| 146 | + ctx => f(r(ctx))(ctx) |
| 147 | + def pure[A](x: A): Ctx => A = |
| 148 | + ctx => x |
| 149 | +} |
| 150 | +``` |
| 151 | +Implementing postconditions via `ensuring`: |
| 152 | +```scala |
| 153 | +object PostConditions { |
| 154 | + opaque type WrappedResult[T] = T |
| 155 | + |
| 156 | + private witness WrappedResult { |
| 157 | + def apply[T](x: T): WrappedResult[T] = x |
| 158 | + def unwrap[T](this x: WrappedResult[T]): T = x |
| 159 | + } |
| 160 | + |
| 161 | + def result[T].(wrapped: WrappedResult[T]): T = wrapped.unwrap |
| 162 | + |
| 163 | + witness { |
| 164 | + def ensuring[T](this x: T)(condition: .WrappedResult[T] => Boolean): T = { |
| 165 | + assert(condition.(WrappedResult(x))) |
| 166 | + x |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +object Test { |
| 172 | + import PostConditions._ |
| 173 | + val s = List(1, 2, 3).sum.ensuring(result == 6) |
| 174 | +} |
| 175 | +``` |
0 commit comments