Skip to content

Commit ee916c0

Browse files
committed
Merge pull request #52 from plaid/dc-nonexhaustive-env
remove the requirement that all types be in the environment
2 parents 62bf5f3 + 9deda6f commit ee916c0

3 files changed

Lines changed: 8 additions & 97 deletions

File tree

‎README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ counterpart).
389389

390390
`$.env` is a list of [types](#types):
391391

392-
- [`$.Any`](#any)
393392
- [`$.Array`](#array)
394393
- [`$.Boolean`](#boolean)
395394
- [`$.Date`](#date)

‎index.js‎

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@
4343
// always :: a -> (-> a)
4444
var always = function(x) { return function() { return x; }; };
4545

46-
// any :: ([a], (a -> Boolean)) -> Boolean
47-
var any = function(xs, pred) {
48-
for (var idx = 0; idx < xs.length; idx += 1) {
49-
if (pred(xs[idx])) return true;
50-
}
51-
return false;
52-
};
53-
5446
// chain :: ([a], (a -> [b])) -> [b]
5547
var chain = function(xs, f) {
5648
var result = [];
@@ -60,15 +52,6 @@
6052
return result;
6153
};
6254

63-
// eqProps :: String -> Object -> Object -> Boolean
64-
var eqProps = function(key) {
65-
return function(o1) {
66-
return function(o2) {
67-
return o1[key] === o2[key];
68-
};
69-
};
70-
};
71-
7255
// filter :: ([a], (a -> Boolean)) -> [a]
7356
var filter = function(xs, pred) {
7457
var result = [];
@@ -254,14 +237,6 @@
254237
};
255238
};
256239

257-
// Any :: Type
258-
var Any = {
259-
'@@type': 'sanctuary-def/Type',
260-
type: 'ANY',
261-
test: K(true),
262-
toString: always('Any')
263-
};
264-
265240
// Unknown :: Type
266241
var Unknown = {
267242
'@@type': 'sanctuary-def/Type',
@@ -474,7 +449,6 @@
474449

475450
// $.env :: [Type]
476451
$.env = [
477-
($.Any = Any),
478452
($.Array = type1('Array', id)),
479453
($.Boolean = type0('Boolean')),
480454
($.Date = type0('Date')),
@@ -489,6 +463,12 @@
489463
($.Undefined = type0('Undefined'))
490464
];
491465

466+
// Any :: Type
467+
$.Any = NullaryType(
468+
'sanctuary-def/Any',
469+
K(true)
470+
);
471+
492472
// ValidDate :: Type
493473
$.ValidDate = NullaryType(
494474
'sanctuary-def/ValidDate',
@@ -638,20 +618,6 @@
638618
}
639619
};
640620

641-
// rejectAny :: Type -> [Type]
642-
var rejectAny = function recur(t) {
643-
switch (t.type) {
644-
case 'ANY':
645-
return [];
646-
case 'UNARY':
647-
return map(recur(t.$1), UnaryType.from(t));
648-
case 'BINARY':
649-
return BinaryType.xprod(t, recur(t.$1), recur(t.$2));
650-
default:
651-
return [t];
652-
}
653-
};
654-
655621
// unexpectedType :: Any -> TypeError
656622
var unexpectedType = /* istanbul ignore next */ function(x) {
657623
return new TypeError(
@@ -786,15 +752,6 @@
786752
);
787753
};
788754

789-
var typeNotInEnvironment = function(env, name, type) {
790-
return new TypeError(trimTrailingSpaces(unlines([
791-
'Definition of ' + LEFT_SINGLE_QUOTATION_MARK + name + RIGHT_SINGLE_QUOTATION_MARK +
792-
' references ' + type.name + ' which is not in the environment:',
793-
'',
794-
map(map(chain(env, rejectAny), showType), prefix(' - ')).join('\n')
795-
])));
796-
};
797-
798755
var invalidArgument = function(name, types, value, index) {
799756
return new TypeError(
800757
LEFT_SINGLE_QUOTATION_MARK + name + RIGHT_SINGLE_QUOTATION_MARK +
@@ -1051,30 +1008,9 @@
10511008
x;
10521009
});
10531010

1054-
// assertExpectedTypesInEnvironment :: String -> [Type] -> Undefined
1055-
var assertExpectedTypesInEnvironment = function(name) {
1056-
return function recur(expTypes) {
1057-
for (var idx = 0; idx < expTypes.length; idx += 1) {
1058-
var expType = expTypes[idx];
1059-
if (expType.type !== 'VARIABLE') {
1060-
if (!any(env, eqProps('name')(expType))) {
1061-
throw typeNotInEnvironment(env, name, expType);
1062-
}
1063-
if (expType.type === 'UNARY') {
1064-
recur([expType.$1]);
1065-
} else if (expType.type === 'BINARY') {
1066-
recur([expType.$1, expType.$2]);
1067-
}
1068-
}
1069-
}
1070-
};
1071-
};
1072-
10731011
// _determineActualTypes :: (Boolean, [Object], [Any]) -> [Type]
10741012
var _determineActualTypes = function recur(loose, $seen, values) {
10751013
if (isEmpty(values)) return [Unknown];
1076-
// consistentTypes :: [Type]
1077-
var consistentTypes = chain(env, rejectAny);
10781014
// typeses :: [[Type]]
10791015
var typeses = map(values, function(value) {
10801016
if (typeof value === 'object' && value != null ||
@@ -1084,7 +1020,7 @@
10841020
if ($seen.indexOf(value) >= 0) return [];
10851021
$seen.push(value);
10861022
}
1087-
return chain(consistentTypes, function(t) {
1023+
return chain(env, function(t) {
10881024
return (
10891025
t.name === 'sanctuary-def/Nullable' || !test(t, value).valid ?
10901026
[] :
@@ -1105,7 +1041,7 @@
11051041
// If none of the values is a member of a type in the environment,
11061042
// and all the values have the same type identifier, the values are
11071043
// members of a "foreign" type.
1108-
if (isEmpty(filterTypesByValues(consistentTypes, values)) &&
1044+
if (isEmpty(filterTypesByValues(env, values)) &&
11091045
all(values.slice(1), $$typeEq($$type(values[0])))) {
11101046
// Create a nullary type for the foreign type.
11111047
return [type0($$type(values[0]))];
@@ -1377,8 +1313,6 @@
13771313
);
13781314
}
13791315

1380-
if (checkTypes) assertExpectedTypesInEnvironment(name)(expTypes);
1381-
13821316
return curry(name,
13831317
constraints,
13841318
expTypes,

‎test/index.js‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -788,28 +788,6 @@ describe('def', function() {
788788
var env = $.env.concat([Integer, $Pair, AnonMaybe]);
789789
var def = $.create(true, env);
790790

791-
var T = $.Array($Pair($.String, Maybe($.Number)));
792-
793-
throws(function() { def('id', {}, [T, T], R.identity); },
794-
errorEq(TypeError,
795-
'Definition of ‘id’ references my-package/Maybe which is not in the environment:\n' +
796-
'\n' +
797-
' - Array ???\n' +
798-
' - Boolean\n' +
799-
' - Date\n' +
800-
' - Error\n' +
801-
' - Function\n' +
802-
' - Null\n' +
803-
' - Number\n' +
804-
' - Object\n' +
805-
' - RegExp\n' +
806-
' - StrMap ???\n' +
807-
' - String\n' +
808-
' - Undefined\n' +
809-
' - Integer\n' +
810-
' - Pair ??? ???\n' +
811-
' - AnonMaybe ???\n'));
812-
813791
// even :: Integer -> Boolean
814792
var even = def('even', {}, [Integer, $.Boolean], function(x) {
815793
return x % 2 === 0;

0 commit comments

Comments
 (0)