Skip to content

Commit

Permalink
Merge pull request #53 from plaid/dc-tuples
Browse files Browse the repository at this point in the history
permit the use of arrays as tuples
  • Loading branch information
davidchambers committed Apr 10, 2016
2 parents ee916c0 + c5cca38 commit 97b7e60
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,27 +1009,30 @@
});

// _determineActualTypes :: (Boolean, [Object], [Any]) -> [Type]
var _determineActualTypes = function recur(loose, $seen, values) {
var _determineActualTypes = function recur(loose, seen, values) {
if (isEmpty(values)) return [Unknown];
// typeses :: [[Type]]
var typeses = map(values, function(value) {
var seen$;
if (typeof value === 'object' && value != null ||
typeof value === 'function') {
// Abort if a circular reference is encountered; add the current
// object to the list of seen objects otherwise.
if ($seen.indexOf(value) >= 0) return [];
$seen.push(value);
if (seen.indexOf(value) >= 0) return [];
seen$ = seen.concat([value]);
} else {
seen$ = seen;
}
return chain(env, function(t) {
return (
t.name === 'sanctuary-def/Nullable' || !test(t, value).valid ?
[] :
t.type === 'UNARY' ?
map(recur(loose, $seen, t._1(value)), UnaryType.from(t)) :
map(recur(loose, seen$, t._1(value)), UnaryType.from(t)) :
t.type === 'BINARY' ?
BinaryType.xprod(t,
recur(loose, $seen, t._1(value)),
recur(loose, $seen, t._2(value))) :
recur(loose, seen$, t._1(value)),
recur(loose, seen$, t._2(value))) :
// else
[t]
);
Expand Down
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,39 @@ describe('def', function() {
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
});

it('permits the use of arrays as tuples', function() {
// Pair :: Type
var Pair = $.BinaryType(
'my-package/Pair',
R.both(R.is(Array), R.propEq('length', 2)),
R.compose(R.of, R.nth(0)),
R.compose(R.of, R.nth(1))
);

var env = $.env.concat([Either, Pair]);
var def = $.create(true, env);

// id :: a -> a
var id = def('id', {}, [a, a], R.identity);

eq(id(['abc', 123]), ['abc', 123]);
eq(id([Left('abc'), 123]), [Left('abc'), 123]);
eq(id(['abc', Right(123)]), ['abc', Right(123)]);
eq(id([Left('abc'), Right(123)]), [Left('abc'), Right(123)]);

throws(function() { id([Left('abc'), 123, 456]); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
'id :: a -> a\n' +
' ^\n' +
' 1\n' +
'\n' +
'1) [Left("abc"), 123, 456] :: Array ???\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
});

it('supports values of "foreign" types', function() {
// id :: a -> a
var id = def('id', {}, [a, a], R.identity);
Expand Down

0 comments on commit 97b7e60

Please sign in to comment.