Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

permit the use of arrays as tuples #53

Merged
merged 1 commit into from
Apr 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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