1

It is a common K idiom to define a programming language's syntax with a top-sort of well-formed programs (e.g. Pgm) and then to restrict the <k> cell to have this sort in the configuration declaration using the special $PGM variable which is passed automatically by krun. This prevents users from executing programs with krun that are not well-formed. My question is:

  1. Are the sort of cells checked only at start-up time or after each rule evaluation?
  2. Do different cells show different behavior depending on their identity (e.g. the <k> cell) or how they are typed (e.g. user-defined types versus builtin types)?

Here is a partial example to show what I mean:

configuration 
  <mylang>
    <k> $PGM:Pgm </k>
    <env> .Env:Env </env> // Env is a custom map structure defined for environments
    <store> .Map </store> // For the store we use the K builtin Map
    ...
  </mylang>

For the <k> cell, I conclude that it is definitely only checked at start-up time, since program evaluation typically tears a program apart into an expression and a continuation (e.g. ADD ~> ...) which cannot have the sort Pgm anymore because ~> is builtin.

So, elaborating on questions (1-2) above, is the <k> cell exceptional in this sense?

1 Answers1

1

Each rule is sort-checked at kompile time to be sort-preserving, so it's not needed to check this at runtime. If something of the correct sort goes in, something of the correct sort comes out.

The <k> cell gets sort K, at least for example, in this definition: https://github.com/kframework/evm-semantics/blob/272608d70f363ed3d8d921887b98a26102a03032/evm.md#configuration

it results in compiled.txt (found at .build/defn/java/driver-kompiled/compiled.txt) which looks like:

...

  syntax KCell ::= "project:KCell" "(" K ")" [function, projection]
  syntax KCell ::= "initKCell" "(" Map ")" [function, initializer, noThread]
  syntax KCell ::= "<k>" K "</k>" [cell, cellName(k), contentStartColumn(7), contentStartLine(31), format(%1%i%n%2%d%n%3), maincell, org.kframework.definition.Production(syntax #RuleContent ::= #RuleBody [klabel(#ruleNoConditions), symbol])]
...

But other cells get more specific sorts:

...
  syntax JumpDestsCell ::= "project:JumpDestsCell" "(" K ")" [function, projection]
  syntax JumpDestsCell ::= "initJumpDestsCell" [function, initializer, noThread]
  syntax JumpDestsCell ::= "<jumpDests>" Set "</jumpDests>" [cell, cellName(jumpDests), contentStartColumn(7), contentStartLine(31), format(%1%i%n%2%d%n%3), org.kframework.definition.Production(syntax #RuleContent ::= #RuleBody [klabel(#ruleNoConditions), symbol])]
...

I'm not sure how K decides that the <k> cell needs to get sort K, but I don't think it's based on analyzing the rules. I think it's likely that it sees $PGM in that cell, so it adds the maincell attribute you see and gives it sort K. Everething is a subsort of K.

I'm fairly certain it's not any $ variable in the configuration that gives it sort K, because the <chainID> cell in KEVM gets these declarations:

...
  syntax ChainIDCell ::= "project:ChainIDCell" "(" K ")" [function, projection]
  syntax ChainIDCell ::= "initChainIDCell" "(" Map ")" [function, initializer, noThread]
  syntax ChainIDCell ::= "<chainID>" Int "</chainID>" [cell, cellName(chainID), contentStartColumn(7), contentStartLine(31), format(%1%i%n%2%d%n%3), org.kframework.definition.Production(syntax #RuleContent ::= #RuleBody [klabel(#ruleNoConditions), symbol])]
...

Note that there isn't very much special about the _~>_ operator. It's declared here: https://github.com/kframework/k/blob/135469ea0ebea96dacf0f9a49261ff1171440c20/k-distribution/include/kframework/builtin/kast.k#L57

ehildenb
  • 316
  • 1
  • 5