squiggle/packages/squiggle-lang/src/rescript/ReducerInterface/ReducerInterface_ExternalLibrary.res

61 lines
2.2 KiB
Plaintext
Raw Normal View History

2022-03-29 09:09:59 +00:00
module ExpressionValue = ReducerInterface_ExpressionValue
2022-03-24 12:41:31 +00:00
2022-03-29 09:09:59 +00:00
type expressionValue = ExpressionValue.expressionValue
2022-03-24 12:41:31 +00:00
2022-03-29 09:09:59 +00:00
module Sample = {
// In real life real libraries should be somewhere else
2022-03-24 12:41:31 +00:00
/*
For an example of mapping polymorphic custom functions. To be deleted after real integration
2022-03-29 09:09:59 +00:00
*/
let customAdd = (a: float, b: float): float => {a +. b}
2022-03-24 12:41:31 +00:00
}
/*
Map external calls of Reducer
*/
2022-03-31 20:44:52 +00:00
let env: GenericDist_GenericOperation.env = {
sampleCount: 100,
xyPointLength: 100,
}
2022-03-29 09:09:59 +00:00
let dispatch = (call: ExpressionValue.functionCall, chain): result<expressionValue, 'e> =>
switch call {
| ("add", [EvNumber(a), EvNumber(b)]) => Sample.customAdd(a, b)->EvNumber->Ok
2022-03-31 20:44:52 +00:00
| ("add", [EvDist(a), EvDist(b)]) => {
let x = GenericDist_GenericOperation.Output.toDistR(
GenericDist_GenericOperation.run(~env, FromDist(ToDistCombination(Algebraic, #Add, #Dist(b)), a))
)
switch x {
| Ok(thing) => Ok(EvDist(thing))
| Error(err) => Error(Reducer_ErrorValue.RETodo("")) // TODO:
}
}
| ("add", [EvNumber(a), EvDist(b)]) => {
let x = GenericDist_GenericOperation.Output.toDistR(
GenericDist_GenericOperation.run(~env, FromDist(ToDistCombination(Algebraic, #Add, #Dist(b)), a))
)
switch x {
| Ok(thing) => Ok(EvDist(thing))
| Error(err) => Error(Reducer_ErrorValue.RETodo("")) // TODO:
}
}
2022-03-29 09:09:59 +00:00
| call => chain(call)
2022-03-24 12:41:31 +00:00
2022-03-29 09:09:59 +00:00
/*
2022-03-24 12:41:31 +00:00
If your dispatch is too big you can divide it into smaller dispatches and pass the call so that it gets called finally.
The final chain(call) invokes the builtin default functions of the interpreter.
Via chain(call), all MathJs operators and functions are available for string, number , boolean, array and record
.e.g + - / * > >= < <= == /= not and or sin cos log ln concat, etc.
// See https://mathjs.org/docs/expressions/syntax.html
// See https://mathjs.org/docs/reference/functions.html
Remember from the users point of view, there are no different modules:
// "doSth( constructorType1 )"
// "doSth( constructorType2 )"
doSth gets dispatched to the correct module because of the type signature. You get function and operator abstraction for free. You don't need to combine different implementations into one type. That would be duplicating the repsonsibility of the dispatcher.
*/
2022-03-29 09:09:59 +00:00
}