31 Polymorphic Contracts
(require unstable/poly-c) |
(poly/c (x ...) c) |
At each application of a function, the poly/c contract constructs a new weak, eq?-based hash table for each x. Values flowing into the polymorphic function (i.e. values protected by some x in negative position with respect to poly/c) are stored in the hash table. Values flowing out of the polymorphic function (i.e. protected by some x in positive position with respect to poly/c) are checked for their presence in the hash table. If they are present, they are returned; otherwise, a contract violation is signalled.
Examples: | ||||
| ||||
> (check #t 'ok) | ||||
'ok | ||||
> (check #f 'ignored) | ||||
self-contract violation: expected a(n) X; got: 'invalid | ||||
contract on check from (function check), blaming | ||||
(function check) | ||||
contract: (poly/c (X) ...) | ||||
at: eval:2.0 | ||||
> (check #t 'surprise) | ||||
self-contract violation: expected a(n) X; got: 'invalid | ||||
contract on check from (function check), blaming | ||||
(function check) | ||||
contract: (poly/c (X) ...) | ||||
at: eval:2.0 |
(parametric/c (x ...) c) |
At each application of a function, the parametric/c contract constructs a new opaque wrapper for each x; values flowing into the polymorphic function (i.e. values protected by some x in negative position with respect to parametric/c) are wrapped in the corresponding opaque wrapper. Values flowing out of the polymorphic function (i.e. values protected by some x in positive position with respect to parametric/c) are checked for the appropriate wrapper. If they have it, they are unwrapped; if they do not, a contract violation is signalled.
Examples: | ||||
| ||||
> (check #t 'ok) | ||||
'ok | ||||
> (check #f 'ignored) | ||||
self-contract violation: expected a(n) X; got: 'invalid | ||||
contract on check from (function check), blaming | ||||
(function check) | ||||
contract: (parametric/c (X) ...) | ||||
at: eval:2.0 | ||||
> (check #t 'surprise) | ||||
'surprise |
If positive? is true, values in positive position get stored and values in negative position are checked. Otherwise, the reverse happens.
If positive? is true, values in positive position get wrapped and values in negative position get unwrapped. Otherwise, the reverse happens.