1

I'm writing a constraint satisfaction solver that will read an input file with multiple functions such as

and(not_equal(X0,X1),not_equal(absolute(subtract(X2,X3)),X4))

The solver will try 100s to 100,000,000s of combinations of X1, X2, X3, X4, X5 on the above function.

Currently the solver:

  1. Reads the next instruction of the function from a Vec.
  2. Pattern matches that instruction to an action.
  3. Executes the instruction and saves the result to possibly be used later in the function.

Instead, I would like pre-process the functions and convert them into static Rust code. So the above function would become:

fn func_1 (vars: &Vec<i32>) -> bool {
    let t1 = if vars[0] != vars[1] {1} else {0};
    let t2 = vars[2] - vars[3];
    t2.abs();
    let t3 = if t2 != vars[4] {1} else {0};
    if t1 == 1 && t3 == 1 {return true} else {return false}
}

I plan on having a pre-processor program:

  1. Read the input file.
  2. Auto-generate a module with all functions converted into Rust.
  3. Edit a module of the solver so that it uses the newly generated functions.
  4. Invoke the compiler.
  5. Run the newly compiled solver.

Is there a better way to do this?

Instead of having two programs can I write a single program that edits itself, invokes the compiler on itself, and schedules itself to be run after compilation is finished?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
HiDefender
  • 2,088
  • 2
  • 14
  • 31
  • Sounds like you're trying to write a compiler. :-) – erip Jun 18 '17 at 21:35
  • What benefit do you foresee in cramming two separate programs into a single source file? – Shepmaster Jun 18 '17 at 21:58
  • @Shepmaster mostly that file management would be easier if the programs were tightly coupled. I also wanted to make sure that I wasn't missing an easier solution. – HiDefender Jun 18 '17 at 22:10
  • Is there a reason why you're writing this instead of using an SMT solver like Z3? It's possible that what you're trying to do can't be expressed in SMT, but I'd look first before embarking on such a similar looking project. – Kyle Jones Jun 18 '17 at 22:17
  • @KyleJones For fun and experience mostly. This is actually the 2nd solver I've written. I want to fix several design mistakes I made early on in the first solver. – HiDefender Jun 18 '17 at 22:29

0 Answers0