Skip to content

Commit

Permalink
Fix types of primitive higher order functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tkers committed Jun 25, 2019
1 parent b297df5 commit a029337
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
18 changes: 18 additions & 0 deletions packages/delisp-core/__tests__/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,24 @@ describe("Type inference", () => {
});
});

describe("Primitive functions", () => {
it("map with multiple values", () => {
expect(typeOf("(map (lambda (x) (values x x)) [1 2 3 4])")).toEqual(
"[number]"
);
});
it("filter with multiple values", () => {
expect(
typeOf("(filter (lambda (x) (values true 5)) [-2 -1 0 1 2])")
).toEqual("[number]");
});
it("fold with multiple values", () => {
expect(
typeOf(`(fold (lambda (a b) (values 50 (+ a b))) [1 2 3 4] 0)`)
).toEqual("number");
});
});

describe("Type annotations", () => {
it("user-specified variables can specialize inferred types", () => {
expect(typeOf("(the [number] [])")).toBe("[number]");
Expand Down
14 changes: 9 additions & 5 deletions packages/delisp-core/src/compiler/inline-primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,17 @@ defineInlinePrimitive("*", "(-> number number _ number)", args => {
};
});

defineInlinePrimitive("map", "(-> (-> a e b) [a] e [b])", ([fn, vec]) => {
return methodCall(vec, "map", [primitiveCall("bindPrimaryValue", fn)]);
});
defineInlinePrimitive(
"map",
"(-> (-> a e (values b | _)) [a] e [b])",
([fn, vec]) => {
return methodCall(vec, "map", [primitiveCall("bindPrimaryValue", fn)]);
}
);

defineInlinePrimitive(
"filter",
"(-> (-> a _ boolean) [a] _ [a])",
"(-> (-> a _ (values boolean | _)) [a] _ [a])",
([predicate, vec]) => {
return methodCall(vec, "filter", [
primitiveCall("bindPrimaryValue", predicate)
Expand All @@ -190,7 +194,7 @@ defineInlinePrimitive(

defineInlinePrimitive(
"fold",
"(-> (-> b a _ b) [a] b _ b)",
"(-> (-> b a _ (values b | _)) [a] b _ b)",
([fn, vec, init]) => {
return methodCall(vec, "reduce", [
primitiveCall("bindPrimaryValue", fn),
Expand Down

0 comments on commit a029337

Please sign in to comment.