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

Type-based compilation of everything + fix for #264 #304

Merged
merged 22 commits into from
Mar 13, 2024

Conversation

jespercockx
Copy link
Member

This PR continues on the path that @flupe has started with #270 and pushes type-based compilation to every corner of agda2hs. It also fixes #264 by removing the horrible compileTopLevelType function and introducing a better way to handle explicit foralls.

Ignoring a number of refactorings, this PR makes the following semantic changes to agda2hs:

  • Parameters of data and record types and of type aliases are now compiled according to their type rather than their hiding/quantity annotations. In particular, hidden data parameters are now supported.
  • Instance declarations are now also compiled according to their type, so non-erased hidden fields are now supported.
  • The check that unboxed records have a single non-erased field is now type-based, so in particular (visible) fields of type Level, Size, and any type in Prop are now allowed (and dropped during compilation).
  • Non-erased module parameters are now allowed. Type parameters are turned into explicit foralls to each top-level function in the module, while other parameters are compiled as normal arguments.

The test suite passes and I have added some minimal extra tests, but it could definitely still use some more testing, so please let me know if you have any examples you'd like me to try.

We only call `compileType` on terms whose type compiles to a Haskell type, so if we encounter a lambda it is safe to erase it.
… foralls

- removed old and buggy `compileTopLevelType` function
- new possible output `DomForall` of `compileDomType`, which is produced for type arguments coming from the module telescope
- new `compilingLocal` field in `CompileEnv` keeps track of whether the function we are currently compiling is a local function (e.g. where function or extended lambda)
- `compileFun'` now always starts in the top-level context and then introduces module parameters for local functions
@jespercockx
Copy link
Member Author

One current limitation is that you cannot have where declarations with foralls in their type signature. In fact, if you try to do that by using a module inside your where block, the definitions in that block simply get dropped. For example,

module _ {a : Set} where
  f1 : a  a
  f1 x = f2 id
    where module _ {b : Set} where
      f2 : (a  b)  b
      f2 g = f3 (g x)
        where module _ {c : Set} where
          f3 : c  c
          f3 x = x
      
{-# COMPILE AGDA2HS f1 #-}

is compiled to the rather sad

f1 : forall a . a -> a
f1 x = f2 id

The reason why the definitions of f2 and f3 are dropped is nothing new: it is a consequence of our imperfect attempt at reconstructing the information of which definitions belong to which clause. We use the function isFatherModuleOf (

isFatherModuleOf :: ModuleName -> ModuleName -> Bool
isFatherModuleOf m = maybe False (mnameToList m ==) . initMaybe . mnameToList
) which only considers direct submodules but not indirect ones.

Copy link
Contributor

@flupe flupe left a comment

Choose a reason for hiding this comment

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

Very nice! I have more questions than remarks really, have yet to think of more tests to add.

src/Agda2Hs/Compile/Type.hs Show resolved Hide resolved
src/Agda2Hs/Compile/Function.hs Show resolved Hide resolved
src/Agda2Hs/Compile/Function.hs Show resolved Hide resolved
src/Agda2Hs/Compile/Function.hs Outdated Show resolved Hide resolved
src/Agda2Hs/Compile/Function.hs Outdated Show resolved Hide resolved
src/Agda2Hs/Compile/Record.hs Show resolved Hide resolved
src/Agda2Hs/Compile/Type.hs Outdated Show resolved Hide resolved
@jespercockx jespercockx merged commit f73415a into agda:master Mar 13, 2024
5 checks passed
jespercockx added a commit to jespercockx/agda2hs that referenced this pull request Mar 15, 2024
This was a regression introduced by agda#304 that went unnoticed due to a lack of a (failing) test case
jespercockx added a commit to jespercockx/agda2hs that referenced this pull request Mar 15, 2024
This was a regression introduced by agda#304 that went unnoticed due to a lack of a (failing) test case
jespercockx added a commit to jespercockx/agda2hs that referenced this pull request Mar 15, 2024
This was a regression introduced by agda#304 that went unnoticed due to a lack of a (failing) test case
@jespercockx jespercockx linked an issue Mar 22, 2024 that may be closed by this pull request
jespercockx added a commit that referenced this pull request Mar 22, 2024
This was a regression introduced by #304 that went unnoticed due to a lack of a (failing) test case
@jespercockx jespercockx added this to the 1.3 milestone Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compilation of non-erased module parameters Internal error with erased module parameter and 'where' function
2 participants