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

[New module system] Interfaces with definitions #1457

Closed
yav opened this issue Oct 14, 2022 · 2 comments
Closed

[New module system] Interfaces with definitions #1457

yav opened this issue Oct 14, 2022 · 2 comments
Labels
design needed We need to specify precisely what we want feature request Asking for new or improved functionality language Changes or extensions to the language parameterized modules Related to Cryptol's parameterized modules

Comments

@yav
Copy link
Member

yav commented Oct 14, 2022

Consider the following example of an "interface" that's currently rejected:

// I_BlockCipher.cry
interface module I_BlockCipher where
 type Key: *
 type BlockSize: #

 type Block = [BlockSize]  // illegal assignment in interface

 encipher: Key -> Block -> Block
 decipher: Key -> Block -> Block

We currently reject such declarations because interfaces are not allowed to define anything, they just declare the kinds/types of things. While semantically this makes sense, it is quite inconvenient as a language construct, and indeed the current system does not seem to have a way to express this sort of thing at all, which is unfortunate.

It seems that we could allow definitions in interfaces, which would essentially make them into a kind of functor. The idea would be that when a module imports an interface, it would get the parameters (just like now) but also it'd get the extra definitions. When instantiating a functor, we just need to add the definitions to the new module, at the same time when we add the parameters.

If we adapt this plan, then we can also change the handling of functors with parameter style parameters:

  • currently these are desugared into a "new style" functor that imports an anonymous interface computed from the parameters block
  • instead, these functors would become "interfaces" (i.e., the things that can be imports via "interface" imports in new-style functors)

There is however a question of what definitions should be allowed in an interface. In particular, I am not quite clear if it makes sense to define modules nested in "interfaces".

@yav yav added feature request Asking for new or improved functionality parameterized modules Related to Cryptol's parameterized modules language Changes or extensions to the language design needed We need to specify precisely what we want labels Oct 14, 2022
yav added a commit that referenced this issue Oct 14, 2022
When loaded an interface is treated like an empty functor.
Adding this capability would also be useful if we generalize
interfaces to allow for definitions, as discussed in #1457

This also fixes #1456
@yav
Copy link
Member Author

yav commented Oct 21, 2022

If we do implement this, and also backtick imports we'll have to make sure that the two interact properly. They should work OK, as long we just allow declarations that are valid in a where clause. Here is an example:

-- This is basically an "interface"
submodule I where
   parameter
      type n : #
      type constraint (fin n, n > 1)

   type T = [n]

  parameter
      f : T

   g = f + 1

submodule M where
   import `submodule I

   h = g + 1

Then module M should be desugared like this:

submodule M where
   h : {n} (fin n, n > 1) => [n] -> [n]
   h f = g + 1
      where
      type T = [n]
      g = f + 1

Basically the parameters to the module become parameters to definition as before, and any local definitions go in a local where block. Since local type synonyms are not in scope in the signature, they'll have to be expanded there. This rewrting happens on already typechecked syntax though, so it is also possible to simply expand the type synonyms defined in the interfaces everywhere.

@yav
Copy link
Member Author

yav commented Oct 26, 2022

The new module system branch now allows type/prop synonyms in interfaces, which allows definitions of type synonyms which may be used in the value parameters of a module. This is the main component that couldn't be expressed before---other definitions can go into a functor.

@yav yav closed this as completed Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design needed We need to specify precisely what we want feature request Asking for new or improved functionality language Changes or extensions to the language parameterized modules Related to Cryptol's parameterized modules
Projects
None yet
Development

No branches or pull requests

1 participant