MrDoops

MrDoops

I’ve been building a tool that compiles elixir expressions into a “workflow” a directed acyclic graph of steps, conditions, and other nodes. Some examples in a livebook here. The advantage of doing this is it allows for composition of functionality at runtime - with varying levels of potential user defined logic. Should be handy for data pipelines, rule based expert systems or other runtime dependent workflows.

A big part of expert systems is to build rules at runtime, but to do this I need to be able to inject runtime values into a macro (at least as implemented - if there’s a better way to extract atomic conditionals in a function I’m all ears).

So the idea is supporting ^ as an escape value like Ecto and Explorer do, but I barely understand Macros and have been struggling to figure out how to access the runtime values in this situation.

test "escapes runtime values with '^'" do
    some_values = [:potato, :ham, :tomato]

    escaped_rule = Dagger.rule(
      name: "escaped_rule",
      condition: fn val when val in ^some_values -> true end,
      reaction: "food"
    )

    assert match?(%Rule{}, escaped_rule)
    assert Rule.check(escaped_rule, :potato)
end

I want the left hand side of this rule to build a function such as:

fn val when val in [:potato, :ham, :tomato] -> true end

I noticed Jose implemented something similar in Explorer recently: explorer/lib/explorer/query.ex at v0.5.5 · elixir-explorer/explorer · GitHub but I don’t quite understand how it works yet.

I’ve tried a few variations such as:

check =
  Macro.prewalk(lhs, fn
    {:^, _meta, [{bind, _, _} = expr]} = ast ->
      Macro.Env.has_var?(context, {bind, nil}) |> IO.inspect(label: "has var?") # true
      runtime_value = Macro.Env.vars(context) |> IO.inspect() # [some_values: nil]

      if runtime_value do
        var = Macro.unique_var(bind, __MODULE__)
        quote do: unquote(var)
      else
        ast
      end

    ast ->
      ast
  end)

Can anyone more familiar with macros and/or escaping runtime values help me understand how this works or how you’d approach this problem? Any feedback is appreciated.

Thanks.

Showing Posts 1 to 1

zachallaun

zachallaun

You can’t access the runtime value at compile time, because, well, it doesn’t have a value yet. :slight_smile: what you can do is strip the ^ and inject the variable into the quoted expression such that the runtime value will be used when things finally execute.

The ^ isn’t special in any way, it’s just a conventional signal to the macro that what follows should be left untouched and not further transformed.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews