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

Reducer dev #226

Merged
merged 22 commits into from
Apr 12, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ open Expect
let expectEvalToBe = (expr: string, answer: string) =>
Reducer.evaluate(expr)->ExpressionValue.toStringResult->expect->toBe(answer)

let testEval = (expr, answer) =>
test(expr, () => expectEvalToBe(expr, answer))

describe("builtin", () => {
// All MathJs operators and functions are available for string, number and boolean
// .e.g + - / * > >= < <= == /= not and or
// See https://mathjs.org/docs/expressions/syntax.html
// See https://mathjs.org/docs/reference/functions.html
test("-1", () => expectEvalToBe("-1", "Ok(-1)"))
test("1-1", () => expectEvalToBe("1-1", "Ok(0)"))
test("2>1", () => expectEvalToBe("2>1", "Ok(true)"))
test("concat('a','b')", () => expectEvalToBe("concat('a','b')", "Ok('ab')"))
testEval("-1", "Ok(-1)")
testEval("1-1", "Ok(0)")
testEval("2>1", "Ok(true)")
testEval("concat('a','b')", "Ok('ab')")
})

describe("builtin exception", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,60 @@ open Expect
let expectParseToBe = (expr, answer) =>
Parse.parse(expr)->Result.flatMap(Parse.castNodeType)->Parse.toStringResult->expect->toBe(answer)

let testParse = (expr, answer) => test(expr, () => expectParseToBe(expr, answer))

let testDescriptionParse = (desc, expr, answer) => test(desc, () => expectParseToBe(expr, answer))

module MySkip = {
let testParse = (expr, answer) => Skip.test(expr, () => expectParseToBe(expr, answer))

let testDescriptionParse = (desc, expr, answer) => Skip.test(desc, () => expectParseToBe(expr, answer))
}

describe("MathJs parse", () => {
describe("literals operators paranthesis", () => {
test("1", () => expectParseToBe("1", "1"))
test("'hello'", () => expectParseToBe("'hello'", "'hello'"))
test("true", () => expectParseToBe("true", "true"))
test("1+2", () => expectParseToBe("1+2", "add(1, 2)"))
test("add(1,2)", () => expectParseToBe("add(1,2)", "add(1, 2)"))
test("(1)", () => expectParseToBe("(1)", "(1)"))
test("(1+2)", () => expectParseToBe("(1+2)", "(add(1, 2))"))
testParse("1", "1")
testParse("'hello'", "'hello'")
testParse("true", "true")
testParse("1+2", "add(1, 2)")
testParse("add(1,2)", "add(1, 2)")
testParse("(1)", "(1)")
testParse("(1+2)", "(add(1, 2))")
})

describe("multi-line", () => {
testParse("1; 2", "{1; 2}")
})

describe("variables", () => {
Skip.test("define", () => expectParseToBe("x = 1", "???"))
Skip.test("use", () => expectParseToBe("x", "???"))
testParse("x = 1", "x = 1")
testParse("x", "x")
testParse("x = 1; x", "{x = 1; x}")
})

describe("functions", () => {
Skip.test("define", () => expectParseToBe("identity(x) = x", "???"))
Skip.test("use", () => expectParseToBe("identity(x)", "???"))
MySkip.testParse("identity(x) = x", "???")
MySkip.testParse("identity(x)", "???")
})

describe("arrays", () => {
test("empty", () => expectParseToBe("[]", "[]"))
test("define", () => expectParseToBe("[0, 1, 2]", "[0, 1, 2]"))
test("define with strings", () => expectParseToBe("['hello', 'world']", "['hello', 'world']"))
Skip.test("range", () => expectParseToBe("range(0, 4)", "range(0, 4)"))
test("index", () => expectParseToBe("([0,1,2])[1]", "([0, 1, 2])[1]"))
testDescriptionParse("empty", "[]", "[]")
testDescriptionParse("define", "[0, 1, 2]", "[0, 1, 2]")
testDescriptionParse("define with strings", "['hello', 'world']", "['hello', 'world']")
MySkip.testParse("range(0, 4)", "range(0, 4)")
testDescriptionParse("index", "([0,1,2])[1]", "([0, 1, 2])[1]")
})

describe("records", () => {
test("define", () => expectParseToBe("{a: 1, b: 2}", "{a: 1, b: 2}"))
test("use", () => expectParseToBe("record.property", "record['property']"))
testDescriptionParse("define", "{a: 1, b: 2}", "{a: 1, b: 2}")
testDescriptionParse("use", "record.property", "record['property']")
})

describe("comments", () => {
Skip.test("define", () => expectParseToBe("# This is a comment", "???"))
MySkip.testDescriptionParse("define", "# This is a comment", "???")
})

describe("if statement", () => {
Skip.test("define", () => expectParseToBe("if (true) { 1 } else { 0 }", "???"))
describe("if statement", () => { // TODO Tertiary operator instead
MySkip.testDescriptionParse("define", "if (true) { 1 } else { 0 }", "???")
})
})
98 changes: 61 additions & 37 deletions packages/squiggle-lang/__tests__/Reducer/Reducer_test.res
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
open Jest
open Reducer_TestHelpers

let testParseToBe = (expr, answer) => test(expr, () => expectParseToBe(expr, answer))

let testDescriptionParseToBe = (desc, expr, answer) => test(desc, () => expectParseToBe(expr, answer))

let testEvalToBe = (expr, answer) => test(expr, () => expectEvalToBe(expr, answer))

let testDescriptionEvalToBe = (desc, expr, answer) => test(desc, () => expectEvalToBe(expr, answer))

describe("reducer using mathjs parse", () => {
// Test the MathJs parser compatibility
// Those tests toString that there is a semantic mapping from MathJs to Expression
Expand All @@ -10,33 +18,39 @@ describe("reducer using mathjs parse", () => {
// Those tests toString that we are converting mathjs parse tree to what we need

describe("expressions", () => {
test("1", () => expectParseToBe("1", "Ok(1)"))
test("(1)", () => expectParseToBe("(1)", "Ok(1)"))
test("1+2", () => expectParseToBe("1+2", "Ok((:add 1 2))"))
test("(1+2)", () => expectParseToBe("1+2", "Ok((:add 1 2))"))
test("add(1,2)", () => expectParseToBe("1+2", "Ok((:add 1 2))"))
test("1+2*3", () => expectParseToBe("1+2*3", "Ok((:add 1 (:multiply 2 3)))"))
testParseToBe("1", "Ok(1)")
testParseToBe("(1)", "Ok(1)")
testParseToBe("1+2", "Ok((:add 1 2))")
testParseToBe("1+2", "Ok((:add 1 2))")
testParseToBe("1+2", "Ok((:add 1 2))")
testParseToBe("1+2*3", "Ok((:add 1 (:multiply 2 3)))")
})
describe("arrays", () => {
//Note. () is a empty list in Lisp
// The only builtin structure in Lisp is list. There are no arrays
// [1,2,3] becomes (1 2 3)
test("empty", () => expectParseToBe("[]", "Ok(())"))
test("[1, 2, 3]", () => expectParseToBe("[1, 2, 3]", "Ok((1 2 3))"))
test("['hello', 'world']", () => expectParseToBe("['hello', 'world']", "Ok(('hello' 'world'))"))
test("index", () => expectParseToBe("([0,1,2])[1]", "Ok((:$atIndex (0 1 2) (1)))"))
testDescriptionParseToBe("empty", "[]", "Ok(())")
testParseToBe("[1, 2, 3]", "Ok((1 2 3))")
testParseToBe("['hello', 'world']", "Ok(('hello' 'world'))")
testDescriptionParseToBe("index", "([0,1,2])[1]", "Ok((:$atIndex (0 1 2) (1)))")
})
describe("records", () => {
test("define", () =>
expectParseToBe("{a: 1, b: 2}", "Ok((:$constructRecord (('a' 1) ('b' 2))))")
)
test("use", () =>
expectParseToBe(
"{a: 1, b: 2}.a",
"Ok((:$atIndex (:$constructRecord (('a' 1) ('b' 2))) ('a')))",
)
testDescriptionParseToBe("define", "{a: 1, b: 2}", "Ok((:$constructRecord (('a' 1) ('b' 2))))")
testDescriptionParseToBe(
"use",
"{a: 1, b: 2}.a",
"Ok((:$atIndex (:$constructRecord (('a' 1) ('b' 2))) ('a')))",
)
})
describe("multi-line", () => {
testParseToBe("1; 2", "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) 1) 2))")
testParseToBe("1+1; 2+1", "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:add 1 1)) (:add 2 1)))")
})
describe("assignment", () => {
testParseToBe("x=1; x", "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :x 1)) :x))")
testParseToBe("x=1+1; x+1", "Ok((:$$bindExpression (:$$bindStatement (:$$bindings) (:$let :x (:add 1 1))) (:add :x 1)))")
})

})

describe("eval", () => {
Expand All @@ -45,37 +59,47 @@ describe("eval", () => {
// See https://mathjs.org/docs/expressions/syntax.html
// See https://mathjs.org/docs/reference/functions.html
describe("expressions", () => {
test("1", () => expectEvalToBe("1", "Ok(1)"))
test("1+2", () => expectEvalToBe("1+2", "Ok(3)"))
test("(1+2)*3", () => expectEvalToBe("(1+2)*3", "Ok(9)"))
test("2>1", () => expectEvalToBe("2>1", "Ok(true)"))
test("concat('a ', 'b')", () => expectEvalToBe("concat('a ', 'b')", "Ok('a b')"))
test("log(10)", () => expectEvalToBe("log(10)", "Ok(2.302585092994046)"))
test("cos(10)", () => expectEvalToBe("cos(10)", "Ok(-0.8390715290764524)"))
testEvalToBe("1", "Ok(1)")
testEvalToBe("1+2", "Ok(3)")
testEvalToBe("(1+2)*3", "Ok(9)")
testEvalToBe("2>1", "Ok(true)")
testEvalToBe("concat('a ', 'b')", "Ok('a b')")
testEvalToBe("log(10)", "Ok(2.302585092994046)")
testEvalToBe("cos(10)", "Ok(-0.8390715290764524)")
// TODO more built ins
})
describe("arrays", () => {
test("empty array", () => expectEvalToBe("[]", "Ok([])"))
test("[1, 2, 3]", () => expectEvalToBe("[1, 2, 3]", "Ok([1, 2, 3])"))
test("['hello', 'world']", () => expectEvalToBe("['hello', 'world']", "Ok(['hello', 'world'])"))
test("index", () => expectEvalToBe("([0,1,2])[1]", "Ok(1)"))
test("index not found", () =>
expectEvalToBe("([0,1,2])[10]", "Error(Array index not found: 10)")
)
testEvalToBe("[1, 2, 3]", "Ok([1, 2, 3])")
testEvalToBe("['hello', 'world']", "Ok(['hello', 'world'])")
testEvalToBe("([0,1,2])[1]", "Ok(1)")
testDescriptionEvalToBe("index not found", "([0,1,2])[10]", "Error(Array index not found: 10)")
})
describe("records", () => {
test("define", () => expectEvalToBe("{a: 1, b: 2}", "Ok({a: 1, b: 2})"))
test("index", () => expectEvalToBe("{a: 1}.a", "Ok(1)"))
test("index not found", () => expectEvalToBe("{a: 1}.b", "Error(Record property not found: b)"))
})

describe("multi-line", () => {
testEvalToBe("1; 2", "Error(Assignment expected)")
testEvalToBe("1+1; 2+1", "Error(Assignment expected)")
})
describe("assignment", () => {
testEvalToBe("x=1; x", "Ok(1)")
testEvalToBe("x=1+1; x+1", "Ok(3)")
testEvalToBe("x=1; y=x+1; y+1", "Ok(3)")
testEvalToBe("1; x=1", "Error(Assignment expected)")
testEvalToBe("1; 1", "Error(Assignment expected)")
testEvalToBe("x=1; x=1", "Error(Expression expected)")
})
})

describe("test exceptions", () => {
test("javascript exception", () =>
expectEvalToBe("jsraise('div by 0')", "Error(JS Exception: Error: 'div by 0')")
)

test("rescript exception", () =>
expectEvalToBe("resraise()", "Error(TODO: unhandled rescript exception)")
testDescriptionEvalToBe(
"javascript exception",
"javascriptraise('div by 0')",
"Error(JS Exception: Error: 'div by 0')",
)
testDescriptionEvalToBe("rescript exception", "rescriptraise()", "Error(TODO: unhandled rescript exception)")
})
3 changes: 3 additions & 0 deletions packages/squiggle-lang/src/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function tag<a, b>(x: a, y: b): tagged<a, b> {
export type squiggleExpression =
| tagged<"symbol", string>
| tagged<"string", string>
| tagged<"call", string>
| tagged<"array", squiggleExpression[]>
| tagged<"boolean", boolean>
| tagged<"distribution", Distribution>
Expand Down Expand Up @@ -117,6 +118,8 @@ function createTsExport(
);
case "EvBool":
return tag("boolean", x.value);
case "EvCall":
return tag("call", x.value);
case "EvDistribution":
return tag("distribution", new Distribution(x.value, sampEnv));
case "EvNumber":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ exception TestRescriptException
let callInternal = (call: functionCall): result<'b, errorValue> => {
let callMathJs = (call: functionCall): result<'b, errorValue> =>
switch call {
| ("jsraise", [msg]) => Js.Exn.raiseError(toString(msg)) // For Tests
| ("resraise", _) => raise(TestRescriptException) // For Tests
| ("javascriptraise", [msg]) => Js.Exn.raiseError(toString(msg)) // For Tests
| ("rescriptraise", _) => raise(TestRescriptException) // For Tests
| call => call->toStringFunctionCall->MathJs.Eval.eval
}

Expand Down Expand Up @@ -58,7 +58,7 @@ let callInternal = (call: functionCall): result<'b, errorValue> => {
}

/*
Lisp engine uses Result monad while reducing expressions
Reducer uses Result monad while reducing expressions
*/
let dispatch = (call: functionCall): result<expressionValue, errorValue> =>
try {
Expand Down
10 changes: 10 additions & 0 deletions packages/squiggle-lang/src/rescript/Reducer/Reducer_ErrorValue.res
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
@genType
type errorValue =
| REArrayIndexNotFound(string, int)
| REAssignmentExpected
| REExpressionExpected
| REFunctionExpected(string)
| REJavaScriptExn(option<string>, option<string>) // Javascript Exception
| REMacroNotFound(string)
| RERecordPropertyNotFound(string, string)
| RESymbolNotFound(string)
| RESyntaxError(string)
| RETodo(string) // To do

type t = errorValue
Expand All @@ -12,6 +17,8 @@ type t = errorValue
let errorToString = err =>
switch err {
| REArrayIndexNotFound(msg, index) => `${msg}: ${Js.String.make(index)}`
| REAssignmentExpected => "Assignment expected"
| REExpressionExpected => "Expression expected"
| REFunctionExpected(msg) => `Function expected: ${msg}`
| REJavaScriptExn(omsg, oname) => {
let answer = "JS Exception:"
Expand All @@ -25,6 +32,9 @@ let errorToString = err =>
}
answer
}
| REMacroNotFound(macro) => `Macro not found: ${macro}`
| RERecordPropertyNotFound(msg, index) => `${msg}: ${index}`
| RESymbolNotFound(symbolName) => `${symbolName} is not defined`
| RESyntaxError(desc) => `Syntax Error: ${desc}`
| RETodo(msg) => `TODO: ${msg}`
}
Loading