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

Safer Anoma decoding proposal #3273

Open
janmasrovira opened this issue Jan 9, 2025 · 0 comments
Open

Safer Anoma decoding proposal #3273

janmasrovira opened this issue Jan 9, 2025 · 0 comments

Comments

@janmasrovira
Copy link
Collaborator

The current type of the primitive anomaDecode is equivalent to:

anomaDecode {T} (encoded : Nat) -> T;

If the encoded natural cannot be decoded into a term of type T it will crash, which can be problematic.

We have discussed some solutions to the problem and we have agreed (at least on the general idea) to proceed as follows:

  1. The axiom will change to the following

    unsafeAnomaDecode {T} (encoded : Nat) -> Maybe T;
    

    This is marked as unsafe because we can only check that the decoded term has the shape of some term of T, however, this could be coincidental (E.g. Bool and Nat are overlapping on their nockma encodings).

    To make the problem more explicit we would expose the following definitions:

    type DecodeResult (A : Type) :=
       decodeFail
       | decodeSameShape A;
       
    anomaDecode {T} : (encoded : Nat) -> DecodeResult T := unsafeAnomaDecode >> maybe decodeFail decodeSameShape;
    
  2. Then the users might add a unique type identifier to make decoding safer. E.g.

    MyArgumentsUid : Nat := 1238772879238821273;
    
    type MyArguments := mkArgs {
      uid : Nat   -- should always be set to MyArgumentsUid
      arg1 : ...
      ...
    };
    
    
    myDecode {A} (x : Nat) : Maybe A := case decode x of
      Fail := error "no match"
      SameShape args
          | if uid args == MyArgumentsUid := ok args
          | else := error "type uid does not match";
    

Extension 1 (derived type uid)

  • In order to facilitate the above strategy, the juvix compiler could provide a primitive that would return some uid for any type. The details of how we should compute this uid are to be discussed.
    typeUid : Type -> Nat;
    
    Another option would be to define a trait and support deriving it.
    trait 
    type HasUid (A : Type) := mkHasUid@{
       getTypeUid : Nat
     };
    

Extension 2 (default arguments for record types)

  • Default values for record fields #2427. With default arguments for record types we could make it so that the uid type is pupulated with the desired value by default.
    type MyArguments := mkArgs {
        {uid} : Nat := MyArgumentsUid;
        arg1 : ...
        ...
      };
    
@janmasrovira janmasrovira changed the title Anoma decoding proposal Safer Anoma decoding proposal Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant