Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

LINQ to Z3 - an esoteric LINQ binding based on Bart De Smet's idea

License

Notifications You must be signed in to change notification settings

endjin/Z3.LinqBinding

 
 

Repository files navigation

Z3.LinqBinding

LINQ to Z3 - an esoteric LINQ binding based on Bart De Smet's idea

Original Idea

In 2009 Bart De Smet created a LINQ to Z3 binding and describes it in three blog posts

This Version

Since 2009 the Z3 Theorem Prover, initial from Microsoft Research, has changed:

  • new API versions with a lot of breaking changes
  • in March 2015 the licence has moved to MIT License and the source code to GitHub: https://github.com/Z3Prover/z3

This version of Z3.LinqBinding supports .NET 6.0 and Microsoft.Z3 4.8.12.
Also the sample code of a Sudoku theorem extension has been added: Z3.LinqBinding.Sudoku

Documentation

Basic Usage

using (var ctx = new Z3Context())
{
    ctx.Log = Console.Out; // see internal logging

    var theorem = from t in ctx.NewTheorem(new { x = default(bool), y = default(bool) })
                  where t.x ^ t.y
                  select t;

    var result = theorem.Solve();
    Console.WriteLine(result);
}

Output:

(xor x y)
{ x = True, y = False }

Advanced Usage

You can use build in or custom symbol classes.
Currently only bool and integer theorems are supported.

using (var ctx = new Z3Context())
{
    ctx.Log = Console.Out; // see internal logging

    var theorem = from t in ctx.NewTheorem<Symbols<int, int>>()
                  where t.X1 < t.X2 + 1
                  where t.X1 > 2
                  where t.X1 != t.X2
                  select t;

    var result = theorem.Solve();
    Console.WriteLine(result);
}

Output:

(< X1 (+ X2 1))
(> X1 2)
(not (= X1 X2))
{X1 = 3, X2 = 4}

Sudoku Extension Usage (Z3.LinqBinding.Sudoku)

using (var ctx = new Z3Context())
{
    var theorem = from t in SudokuTheorem.Create(ctx)
                  where t.Cell13 == 2 && t.Cell16 == 1 && t.Cell18 == 6
                  where t.Cell23 == 7 && t.Cell26 == 4
                  where t.Cell31 == 5 && t.Cell37 == 9
                  where t.Cell42 == 1 && t.Cell44 == 3
                  where t.Cell51 == 8 && t.Cell55 == 5 && t.Cell59 == 4
                  where t.Cell66 == 6 && t.Cell68 == 2
                  where t.Cell73 == 6 && t.Cell79 == 7
                  where t.Cell84 == 8 && t.Cell87 == 3
                  where t.Cell92 == 4 && t.Cell94 == 9 && t.Cell97 == 2
                  select t;

    var result = theorem.Solve();
    Console.WriteLine(result);
}

Output:

-------------------------------
| 4  3  2 | 5  9  1 | 7  6  8 |
|         |         |         |
| 9  6  7 | 2  8  4 | 1  3  5 |
|         |         |         |
| 5  8  1 | 6  7  3 | 9  4  2 |
-------------------------------
| 6  1  4 | 3  2  8 | 5  7  9 |
|         |         |         |
| 8  2  3 | 7  5  9 | 6  1  4 |
|         |         |         |
| 7  5  9 | 4  1  6 | 8  2  3 |
-------------------------------
| 2  9  6 | 1  3  5 | 4  8  7 |
|         |         |         |
| 1  7  5 | 8  4  2 | 3  9  6 |
|         |         |         |
| 3  4  8 | 9  6  7 | 2  5  1 |
-------------------------------

Change Log

  • 2020/11/09 - Howard van Rooijen (endjin) upgrades the project to .NET 6.0 and Microsoft.Z3 nuget package 4.8.12
  • 2015/05/09 - Ricardo Wickel (Microsoft) publishes project using .NET 4.5 and Z3 binaries 4.4.0

About

LINQ to Z3 - an esoteric LINQ binding based on Bart De Smet's idea

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • C# 95.5%
  • PowerShell 4.5%