Skip to content

Commit

Permalink
WIP: Correct type of $.test
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq committed Feb 23, 2017
1 parent 40a8cd5 commit b10964e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
23 changes: 17 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,16 @@
return BinaryType(name, functionUrl(name), test, _1, _2);
}

// applyParameterizedType :: (Type... -> Type) -> Type
function applyParameterizedType(type) {
return typeof type === 'function' ?
type.apply(null, Z.map(K(Unknown), range(0, type.length))) :
type;
}

// applyParameterizedTypes :: Array Type -> Array Type
function applyParameterizedTypes(types) {
return Z.map(function(x) {
return typeof x === 'function' ?
x.apply(null, Z.map(K(Unknown), range(0, x.length))) :
x;
}, types);
return Z.map(applyParameterizedType, types);
}

//. ### Types
Expand Down Expand Up @@ -762,6 +765,14 @@
typeEq('sanctuary-def/Type')
);

var VariadicType = NullaryType(
'sanctuary-def/VariadicType',
'',
function(x) {
return test([], Type, x) || test([], AnyFunction, x);
}
);

// TypeClass :: Type
var TypeClass = NullaryType(
'sanctuary-type-classes/TypeClass',
Expand Down Expand Up @@ -2433,7 +2444,7 @@
ValidNumber: ValidNumber,
env: env,
create: create,
test: def('test', {}, [Array_(Type), Type, Any, Boolean_], test),
test: def('test', {}, [Array_(VariadicType), Type, Any, Boolean_], test),
NullaryType: CheckedNullaryType,
UnaryType: CheckedUnaryType,
BinaryType: CheckedBinaryType,
Expand Down
14 changes: 13 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2862,7 +2862,7 @@ describe('test', function() {
it('is a ternary function', function() {
eq(typeof $.test, 'function');
eq($.test.length, 3);
eq($.test.toString(), 'test :: Array Type -> Type -> Any -> Boolean');
eq($.test.toString(), 'test :: Array VariadicType -> Type -> Any -> Boolean');
});

it('supports nullary types', function() {
Expand Down Expand Up @@ -2904,6 +2904,18 @@ describe('test', function() {
eq($.test($.env, $Pair(a, b), Pair('foo', 42)), true);
});

it('supports environments with unary types', function() {
eq($.test($.env.concat([$.Array]), $.Array(a), null), false);
eq($.test($.env.concat([$.Array]), $.Array(a), [1]), true);
eq($.test($.env.concat([$.Array]), $.Array(a), [[]]), true);
});

it('supports environments with binary types', function() {
eq($.test($.env.concat([$Pair]), $Pair(a, b), null), false);
eq($.test($.env.concat([$Pair]), $Pair(a, b), Pair(1, '2')), true);
eq($.test($.env.concat([$Pair]), $Pair(a, b), Pair(Pair(1, '2'), Pair(1, '2'))), true);
});

});

describe('NullaryType', function() {
Expand Down

0 comments on commit b10964e

Please sign in to comment.