agda / agda2hs

Compiling Agda code to readable Haskell
https://agda.github.io/agda2hs
MIT License
178 stars 37 forks source link

Integration with SMT solvers through Schmitty #178

Open jespercockx opened 1 year ago

jespercockx commented 1 year ago

It would be very useful if one could use Z3 or another SMT solver to automatically dismiss proof obligations for obvious statements. There is already such an integration with Agda: Schmitty (https://github.com/wenkokke/schmitty/). We should check what is needed to integrate Schmitty into an Agda2Hs project and whether we can do anything to make it easier to use (e.g. provide predefined theories for types and functions from the Prelude).

wenkokke commented 1 year ago

What is Schmitty?

For the purposes of integrating with agda2hs, we should consider Schmitty as two different projects:

  1. An embedding of a subset of the SMTLIB language in Agda & tactics to call Z3 and other solvers at compile-time.
  2. A set of reflection tactics to convert from the types in the standard library to the embedding of SMTLIB.

What solvers does Schmitty support?

Currently, Schmitty has tactics for Z3 and CVC4. Adding bindings for other SMTLIB-compatible solvers is very easy and can be done in only a few lines of code. It's only a matter of crafting the right command-line invocation—the rest is boilerplate. For an example, see Z3.agda.

What theories does Schmitty support?

Currently, Schmitty supports reasoning about naturals, integers, and reals.

Adding support for new theories is possible, but the amount of work will vary with the complexity of the theory. For a theory that is directly supported by SMITLIB, such as integers, you have to specify:

For a theory that uses some sort of encoding, such as the theory of naturals, you also have to specify that encoding.

Support algebraic datatypes should be fairly simple to add, as it was officially added to SMTLIB in version 2.5. This wouldn't be the same as adding a theory, since datatypes are declared by a declare-datatype statement, which would mean extending the Script type. This should be relatively easy, but may involve some complications depending on how precise you want to be about enforcing that the script type is well-scoped by construction.

How stable is Schmitty?

Schmitty is stable, but there is a pending refactoring to use composible languages following composable-semantics.

Difficulties integrating Schmitty with agda2hs

The major difficulties I foresee is that Schmitty relies on the standard library and agdarsec. I assume agda2hs has its own standard library, which is presumably incompatible with the Agda standard library.

Schmitty's use of the standard library is fairly minimal, and it could probably be rewritten to only use Agda builtin modules.

I believe agdarsec is very tightly coupled with the standard library, so for Schmitty to be portable across different standard libraries we would probably have to:

  1. abstract over an implementation of parser combinators;
  2. switch to an implementation of parser combinators that is, itself, portable; or
  3. bundle a minimal implementation of parser combinators with Schmitty itself.
uhbif19 commented 1 year ago

Why this would be different from regular Agda? I thought that most of such proofs would be erased anyway.

jespercockx commented 1 year ago

Yes, the proofs would indeed be erased. What does need to be done are the issues Wen lists above: the dependencies on stdlib and agdarsec, and the work of mapping the types and functions in the Agda2Hs prelude to the appropriate SMT theories.

uhbif19 commented 1 year ago

@jespercockx Could not agda-stdlib be mapped instead? I understand that is not in the paper, but I do not understand why. Like code base already has some lists for specific types translation, could not some pragma or typeclass be used to define new Agda -> Haskell translations for terms of well-known Agda libs?

wenkokke commented 1 year ago

@jespercockx Could not agda-stdlib be mapped instead? I understand that is not in the paper, but I do not understand why. Like code base already has some lists for specific types translation, could not some pragma or typeclass be used to define new Agda -> Haskell translations for terms of well-known Agda libs?

That'd work, but then you still can't use Schmitty with conventional agda2hs, right?