View Source Guesswork.Answer.Computation (Guesswork v0.6.0)
Represents some arbitrary computation that can be bound to a variable or used to verify correctness.
Summary
Functions
Takes computation and attempt to apply the inputs to the value. Succeeds if all inputs are concrete.
Checks the environment for the inputs, and then runs the stored functions. If the inputs are not present or not concrete values, an error is returned.
Types
@type error() :: {:error, {:unbound | :bound_to_var, Guesswork.Ast.Variable.t()}}
@type func() :: (... -> Guesswork.Ast.Term.entity())
@type t() :: %Guesswork.Answer.Computation{ func: func(), inputs: [Guesswork.Ast.Term.entity() | Guesswork.Ast.Variable.t()] }
Functions
@spec attempt_apply(t()) :: {:ok, Guesswork.Ast.Term.entity()} | :error
Takes computation and attempt to apply the inputs to the value. Succeeds if all inputs are concrete.
@spec new([Guesswork.Ast.Variable.t() | Guesswork.Ast.Term.t()], func()) :: t()
@spec run(t(), Guesswork.Ast.Statement.env()) :: {:ok, Guesswork.Ast.Term.entity()} | error()
Checks the environment for the inputs, and then runs the stored functions. If the inputs are not present or not concrete values, an error is returned.
Examples
iex> Guesswork.Answer.Computation.new([], fn -> 4 end)
...> |> Guesswork.Answer.Computation.run(%{})
{:ok, 4}
iex> Guesswork.Answer.Computation.new([1, 2, 3], fn a, b, c -> a + b + c end)
...> |> Guesswork.Answer.Computation.run(%{})
{:ok, 6}
iex> var1 = Guesswork.Ast.Variable.new("x")
...> var2 = Guesswork.Ast.Variable.new("y")
...> var3 = Guesswork.Ast.Variable.new("z")
...>
...> Guesswork.Answer.Computation.new([var1, var2, var3], fn a, b, c -> a + b + c end)
...> |> Guesswork.Answer.Computation.run(%{var1 => 1, var2 => 2, var3 => 3})
{:ok, 6}
iex> Guesswork.Answer.Computation.new([var1, var2, var3], fn a, b, c -> a + b + c end)
...> |> Guesswork.Answer.Computation.run(%{var1 => 1, var3 => 3})
{:error, {:unbound, var2}}
iex> var4 = Guesswork.Ast.Variable.new("a")
...>
...> Guesswork.Answer.Computation.new([var1, var2, var3], fn a, b, c -> a + b + c end)
...> |> Guesswork.Answer.Computation.run(%{var1 => 1, var2 => var4, var3 => 3})
{:error, {:bound_to_var, var2}}