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

Conditional formats #339

Closed
brendanzab opened this issue Apr 13, 2022 · 0 comments · Fixed by #362
Closed

Conditional formats #339

brendanzab opened this issue Apr 13, 2022 · 0 comments · Fixed by #362

Comments

@brendanzab
Copy link
Member

brendanzab commented Apr 13, 2022

It would be useful to have format descriptions that are refine other formats with predicates:

{ x <- f | e }

For example, in OpenType we might see something like:

{ sfnt_version <- u32be | sfnt_version == 0x00010000 || sfnt_version == "OTTO" }

Rough specification

The typing rules for the core language could look something like:

  f : Format     x : Repr Format ⊢ e : Bool
──────────────────────────────────────────────
           { x <- f | e } : Format


Repr { x <- f | e } = Repr f


  s .. s' : f ⟹ e₁   x : Repr f = e₁ ⊢ e₂ = true : Bool
────────────────────────────────────────────────────────────
          s .. s' : { x <- f | e₂ } ⟹ e₁
  • { x <- f | e } is a Format when:
    • f is a Format
    • assuming x : Repr Format, e is a Bool
  • the representation of { x <- f | e } is Repr f
  • the bit sequence s .. s' is recognized with { x <- f | e₂ } as an expression e₁ when:
    • the bit sequence s .. s' is recognized with f as an expression e₁
    • assuming x : Repr f = e₁, e₂ is the same Bool as true
Elaborated typing rules

Some might find it easier to read these rules with explicit typing contexts, Γ:

  Γ ⊢ f : Format     Γ, x : Repr Format ⊢ e : Bool
────────────────────────────────────────────────────
           Γ ⊢ { x <- f | e } : Format
  • under the context Γ, { x <- f | e } is a Format when:
    • under the context Γ, f is a Format
    • under the context Γ, x : Repr Format, e is a Bool

Note: The binary interpretation uses a similar notation as Mark Brown in the prolog prototype.

Naming ideas

Some alternative names for these format descriptions could be:

  • conditional formats
  • refinement formats
  • guard formats

Future extensions

Predicate preservation

Eventually we could preserve the guard condition in the representation types, using refinement types:

-   f : Format     x : Repr Format ⊢ e : Bool
+   f : Format     x : Repr Format ⊢ e : Prop
  ──────────────────────────────────────────────
             { x <- f | e } : Format


- Repr { x <- f | e } = Repr f
+ Repr { x <- f | e } = { x : Repr f | e }

Syntactic Sugar

We could eventually add some sugar for record formats:

{
    sfnt_version <- u32be == 0x00010000 || "OTTO",
    ...
}

Or perhaps:

{
    sfnt_version <- u32be if sfnt_version == 0x00010000 || sfnt_version == "OTTO",
    ...
}
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 a pull request may close this issue.

1 participant