Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Haskell code from the Agda spec #1315

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

javierdiaz72
Copy link
Contributor

@javierdiaz72 javierdiaz72 commented Nov 21, 2024

This PR resolves #1312 and #1343.

@javierdiaz72 javierdiaz72 added enhancement New feature or request formal-spec Changes related to formal specifications conformance Changes related to conformance testing labels Nov 21, 2024
@javierdiaz72 javierdiaz72 self-assigned this Nov 21, 2024
@javierdiaz72
Copy link
Contributor Author

Perhaps at this point, before I move forward with other STS's, there is a good opportunity to properly integrate this work into the current Cabal project.

@amesgen: What's your opinion? Is this perhaps something you can do? (no pressure 🙂).

@abailly
Copy link
Contributor

abailly commented Nov 25, 2024

I have tried make hs locally, without using nix, and it fails with the following error:

    Checking Interface.HasOrder.Instance (ouroboros-consensus/docs/agda-spec/src/Interface/HasOrder/Instance.agda).
ouroboros-consensus/docs/agda-spec/src/Interface/HasOrder/Instance.agda:34,55-64
No instance of type
((HasPartialOrder.hasPreorder ℚ-hasPartialOrder HasPreorder.≤ x) y
 ⁇)
was found in scope.
when checking that the expression record {} has type
HasDecPartialOrder
make: *** [latex/Spec/PDF.tex] Error 42

I am using agda-stdlib 2.0. What am I doing wrong?

@amesgen
Copy link
Member

amesgen commented Nov 25, 2024

Perhaps at this point, before I move forward with other STS's, there is a good opportunity to properly integrate this work into the current Cabal project.

@amesgen: What's your opinion? Is this perhaps something you can do? (no pressure 🙂).

Will have a look 👍


I have tried make hs locally, without using nix, and it fails with the following error:

    Checking Interface.HasOrder.Instance (ouroboros-consensus/docs/agda-spec/src/Interface/HasOrder/Instance.agda).
ouroboros-consensus/docs/agda-spec/src/Interface/HasOrder/Instance.agda:34,55-64
No instance of type
((HasPartialOrder.hasPreorder ℚ-hasPartialOrder HasPreorder.≤ x) y
 ⁇)
was found in scope.
when checking that the expression record {} has type
HasDecPartialOrder
make: *** [latex/Spec/PDF.tex] Error 42

I am using agda-stdlib 2.0. What am I doing wrong?

I am not sure exactly, but maybe you need to setup agda-stdlib-classes and agda-stdlib-meta?

agdaStdlibClasses = customAgda.agdaPackages.mkDerivation {
inherit (locales) LANG LC_ALL LOCALE_ARCHIVE;
pname = "agda-stdlib-classes";
version = "2.0";
src = pkgs.fetchFromGitHub {
owner = "omelkonian";
repo = "agda-stdlib-classes";
rev = "v2.0";
hash = "sha256-PcieRRnctjCzFCi+gUYAgyIAicMOAZPl8Sw35fZdt0E=";
};
meta = { };
libraryFile = "agda-stdlib-classes.agda-lib";
everythingFile = "Classes.agda";
buildInputs = [ agdaStdlib ];
};
agdaStdlibMeta = customAgda.agdaPackages.mkDerivation {
inherit (locales) LANG LC_ALL LOCALE_ARCHIVE;
pname = "agda-stdlib-meta";
version = "2.0";
src = pkgs.fetchFromGitHub {
owner = "omelkonian";
repo = "agda-stdlib-meta";
rev = "v2.1.1";
hash = "sha256-qOoThYMG0dzjKvwmzzVZmGcerfb++MApbaGRzLEq3/4=";
};
meta = { };
libraryFile = "agda-stdlib-meta.agda-lib";
everythingFile = "Main.agda";
buildInputs = [ agdaStdlib agdaStdlibClasses ];
};

@abailly
Copy link
Contributor

abailly commented Nov 25, 2024

@amesgen thanks for your answer 🙏 So I have those setup, they are in my ~/.agda/libraries definition. I will double check the versions.
Thing is: when I use nix, eg. nix develop agda, I don't seem to have the needed libraries either. It does not even bring agda into scope so I am probably misusing nix here. It would be nice to add some documentation in does/agda-spec/README.d for example to help the poor soul ending up here how to hack on these things.

@abailly
Copy link
Contributor

abailly commented Nov 25, 2024

It seems I had outdated version of agda-stdlib-classes and agda-stdlib-meta. Refreshing to match the revisions given in nix file does yield to different errors.

@amesgen
Copy link
Member

amesgen commented Nov 25, 2024

Thing is: when I use nix, eg. nix develop agda, I don't seem to have the needed libraries either. It does not even bring agda into scope so I am probably misusing nix here. It would be nice to add some documentation in does/agda-spec/README.d for example to help the poor soul ending up here how to hack on these things.

It should work with

nix develop .#agda-spec

We definitely want a good readme for docs/agda-spec, hopefully we can get to that soon (the spec is still fairly new and evolving).

@abailly
Copy link
Contributor

abailly commented Nov 25, 2024

It's funny that nix develop agda-spec does not work, but nix develop agda worked although it yielded me no usable environment

@amesgen
Copy link
Member

amesgen commented Nov 25, 2024

It's funny that nix develop agda-spec does not work, but nix develop agda worked although it yielded me no usable environment

Just to explain this behavior: These two commands use the Nix registry which is completely independent of the local project you are working on. Therefore, nix develop agda works because agda is a registry entry in the global flake, but agda-spec is not. However, nix develop agda will drop you in a shell that you can use to work on Agda (https://github.com/agda/agda), but this is not what you want here, in a project that uses Agda.

In contrast, nix develop .#agda-spec uses the agda-spec shell defined in the local flake (ie in . or in a parent directory):

agda-spec = pkgs.agda-spec.shell;

@abailly
Copy link
Contributor

abailly commented Nov 25, 2024

I still wish I could make hs without needing nix though :)

@javierdiaz72 javierdiaz72 force-pushed the javierdiaz72/agda-spec-to-haskell branch 3 times, most recently from 43c9f54 to f3361a3 Compare December 12, 2024 19:07
@javierdiaz72 javierdiaz72 force-pushed the javierdiaz72/agda-spec-to-haskell branch from f3361a3 to a98ba32 Compare December 12, 2024 19:14
Fixes #1343
Note that this PR targets the branch of
#1315 and not
`main`.

This PR adds Nix derivations that:
- generate Haskell code from the Agda spec using the `Makefile`-based
build system in `./docs/agda-spec/`
- wrap the generated code into Nix using `cabal2nix`

In combination with the existing set-up in `flake.nix`, we can now build
the generated code and run it's tests using the following flake output:
```
nix build .#hydraJobs.x86_64-linux.native.agda-spec.hsExe
```
which will also be done in CI, i.e. see the logs from testing this PR
- the job
[x86_64-linux.native.agda-spec.hsExe](https://ci.iog.io/build/6263029/log)
builds and runs tests for the generated code

some other jobs, e.g. the linting of the unit tests for the generated
code, are failing, but we can fix those in
#1315
@geo2a geo2a linked an issue Dec 16, 2024 that may be closed by this pull request
3 tasks
…pec-to-haskell' into javierdiaz72/agda-spec-to-haskell
@geo2a geo2a force-pushed the javierdiaz72/agda-spec-to-haskell branch 2 times, most recently from b307ad4 to 5987184 Compare December 18, 2024 10:41
- dos2unix
- hlint
- make `cardano-consensus-executable-spec.cabal` pass `cabal.check`

Make `cardano-consensus-executable-spec.cabal` pass `cabal.check`

Copy licence files instead of linking

fix cabal file
@geo2a geo2a force-pushed the javierdiaz72/agda-spec-to-haskell branch from 5987184 to f2af22c Compare December 18, 2024 11:22
@geo2a
Copy link
Contributor

geo2a commented Jan 13, 2025

Hi @javierdiaz72, Happy New Year! Shall we merge this PR so that it does not bit-rot?

@javierdiaz72
Copy link
Contributor Author

Hi @javierdiaz72, Happy New Year! Shall we merge this PR so that it does not bit-rot?

Hi, @geo2a! Happy New Year to you too! There is still quite a bit of work to do regarding this PR (that's why it's still in draft mode), so we should not merge it yet.

@javierdiaz72
Copy link
Contributor Author

javierdiaz72 commented Jan 23, 2025

Hi, @geo2a! Do you have an idea why some of the CI checks are still failing?

@amesgen
Copy link
Member

amesgen commented Jan 23, 2025

CI is complaining about the fact that some files don't have LF (unix style) line endings: https://ci.iog.io/build/6541916/nixlog/1 (also see #1253/#1252).
Concretely, the file in question is docs/agda-spec/src/Spec/hs-src/test/OperationalCertificateSpec.hs. Maybe this is something you could configure in your editor?

@javierdiaz72
Copy link
Contributor Author

CI is complaining about the fact that some files don't have LF (unix style) line endings: https://ci.iog.io/build/6541916/nixlog/1 (also see #1253/#1252). Concretely, the file in question is docs/agda-spec/src/Spec/hs-src/test/OperationalCertificateSpec.hs. Maybe this is something you could configure in your editor?

Thanks for pointing that out, Alex! I've just fixed the issue with the file and now the CI is not complaining anymore. 🙂

@javierdiaz72
Copy link
Contributor Author

Hey, folks! I'm not sure why the last CI check is failing. Could anybody help me out?

@geo2a
Copy link
Contributor

geo2a commented Feb 17, 2025

@javierdiaz72 it seems like the failure was transient, I've re-triggered the job and the failure went away.

@javierdiaz72 javierdiaz72 marked this pull request as ready for review February 17, 2025 14:13
@geo2a
Copy link
Contributor

geo2a commented Feb 18, 2025

Thanks @javierdiaz72 for this monumental work!

Before we merge this, we need to work a little more on documentation and Git history:

  • Could you please write a README.md for the docs/agda-spec directory? I think it should include:
    • the outline structure of the project: what Haskell code is static, what is generated and a could of words on how the two are tied together
    • instructions on how to generate, build and test the Haskell code, assuming that we are in the Nix shell, i.e. nix develop .#agda-spec
  • Once the above is done, could you also create a small section in the top-level README.md that references docs/agda-spec/README.md
  • Finally, could you rebase on top of current main and squash all commits into one, keeping the rough sequence in the commit message?

Please let me know if you need any help with these!

Another question:

  • Do we still need the files in docs/agda-spec/nix? I do think they are used anywhere.

Otherwise the PR looks great! I have verified that the Haskell code is generated, build and tested with:

nix build .#agda-spec.hsExe --rebuild --print-build-logs 2>&1 | tee tmp.log

the test results should be somewhere near the end of the output.

@geo2a
Copy link
Contributor

geo2a commented Feb 18, 2025

@javierdiaz72 another request I got form the team is to provide instructions to build the spec and run the tests without Nix. How hard would that be? Would it be possible for you to outline in the README what additional steps one needs to follow besides the Agda installation instructions?

base >=4.14 && <4.21,
text,
ieee
-- This will be generated automatically when building with nix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When building the source derivation with Nix, e.g. nix build .#agda-spec.hsSrc, this section is indeed filled in with a long list of module names. @javierdiaz72 do you know exactly what bit of code is responsible for this? Is this a blocker for providing a non-Nix build option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code responsible for that is:

find $(HS_DIST)/MAlonzo -name "*.hs" -print\
| sed "s#^$(HS_DIST)/# #;s#\.hs##;s#/#.#g"\
>> $(HS_DIST)/$(CABAL_FILE)

Therefore, it's not related to Nix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conformance Changes related to conformance testing enhancement New feature or request formal-spec Changes related to formal specifications
Projects
Status: 🏗 In progress
4 participants