daml icon indicating copy to clipboard operation
daml copied to clipboard

Improve type checker error context

Open akrmn opened this issue 2 years ago • 0 comments

In the (haskell) LF typechecker, error messages are given a context with the Error constructor EContext, which takes a Context, taken from the Gamma field _locCtx. The Context type has different constructors for each part of a module, in particular the parts of a template are represented by

data Context
  = {- ... -}
  | ContextTemplate !Module !Template !TemplatePart
  | {- ... -}

data TemplatePart
  = TPWhole
  | TPStakeholders
  | TPPrecondition
  | TPSignatories
  | TPObservers
  | {- ... -}

For typechecking a new part of a template, one would have to add a TemplatePart constructor and set the _locCtx field at the beginning of the checks for that part. Crucially, that means that the function that typechecks that part would need to either construct a ContextTemplate (meaning it would need access to the Module and Template even if nothing else in the function needs them), or it would need to take a partially applied ContextTemplate (making the type of the function more complex).

Instead, we could "flatten" the Context type to talk about individual typechecking contexts, while changing EContext and Gamma to carry a stack of Contexts. For example, a function checkTemplateObservers would set the context to ContextTemplateObservers, which wouldn't mention the Template; instead, checkTemplateObservers caller (e.g. checkTemplate) would have previously pushed a ContextTemplate Template.

This came up during the work on #14683, see https://github.com/digital-asset/daml/pull/14683#discussion_r942639375

akrmn avatar Aug 11 '22 08:08 akrmn