Open jeanas opened 1 year ago
@jeanas The issue you are experiencing comes from the overloading of the ~>
symbol when compiling your definition from the highl-level K
language to the kore
format used by the haskell/llvm execution engines. If you inspect the definition.kore
file inside the lambda-kompiled/
folder, you will find that the
<bound-variables>
_S ~> BoundRest => BoundRest
</bound-variables>
portion of the problematic rule has been translated into the kore language as:
Lbl'-LT-'bound-variables'-GT-'{}(
append{}(
Var'Unds'S:SortK{},
VarBoundRest:SortK{}
)
)
In the above pattern, both variables have been given the most generic sort possible, namely K
.
However, as per the error message, your <bound-variables>
cell just before the failure looks like this:
Lbl'-LT-'bound-variables'-GT-'{}(
kseq{}(
inj{SortSet{}, SortKItem{}}(
LblSetItem{}(
inj{SortId{}, SortKItem{}}(
\dv{SortId{}}("x")
)
)
),
kseq{}(
inj{SortSet{}, SortKItem{}}(
Lbl'Stop'Set{}()
),
dotk{}()
)
)
)
In the high-level K syntax, this corresponds to
<bound-variables>
SetItem ( x ) ~> .Set ~> .
</bound-variables>
as you can see, in this instance the compiler has transformed ~>
into a kseq{}
. This is because it inferred the types of the arguments to be of a more specific sort than K
, namely that of Set
injected into the sort KItem
. Because of this more specific type,we have a kseq
instead of append. Thus, a fix for your rule would be to add a more specific type to _S
, as you know it must be a Set
:
rule [debugme]: <state> ListFreeVariables() </state>
<list-free-variables>
done(U) ~> AbstracFreezer(X) ~> Rest => done(/\\ X . U) ~> Rest
</list-free-variables>
<bound-variables>
_S:Set ~> BoundRest => BoundRest
</bound-variables>
Restriction - is anything in a K sequence other than the last element in the sequence of sort K? For example, here, we'd emit an error that _S
is inferred as sort K (but it's OK for the continuation BoundRest
to be).
@radumereuta suggests rewriting the ~>
production to be KItem ~> K
rather than K ~> K
or have two overloaded productions with the KItem
one preferred. Investigate whether this breaks things.
@dwightguth please take a look at this issue and comment on any potential issues you see, particularly with respect to the Maude backend.
Renaming the first argument into KItem might not work because of stuff like this:
//You can write a single character to a file using `#putc`. You can also write
//a string to a file using `#write`. The returned value on success is `.K`.
syntax K ::= "#putc" "(" fd: Int "," value: Int ")" [function, hook(IO.putc), impure]
| "#write" "(" fd: Int "," value: String ")" [function, hook(IO.write), impure]
rule ListItem(#ostream(N:Int => {#write(N, S) ~> N:Int}:>Int)) // this fails to parse now
We will have to do a lot of fiddling with productions in order to change it. Probably easier to have a special case in the type inferencer.
Wasn't the consensus that we wanted to avoid modifying the type inferencer @radumereuta? We throw an error if any element of a kseq is inferred as K other than the continuation, but allow users to explicitly sort elements as K if they so choose.
Good catch on the .K
productions - the kast.md change probably doesn't make sense as a result of that.
We should try to infer the correct sort first. If that's too hard, then we can throw an error. We could special case it to consider the LHS of a ~> as having sort KItem when inferring sorts for variables.
For this example, which is common, we need to infer:
(L:KItem => R:K) ~> K:K
All but the last element on the LHS is sort KItem
All the element on the RHS and the last on the LHS is sort K.
When this is fixed, we should also close https://github.com/runtimeverification/k/issues/3478 as it is related. @dwightguth you said you have one more issue that might be related, please add it here.
What component is the issue in?
haskell-backend
Which command
What K Version?
v5.6.90 (Thu May 11 22:42:38 CEST 2023)
Operating System
Linux
K Definitions (If Possible)
This is minimized from a larger file that is supposed to reduce λ-terms (NB I am a beginner with K).
Steps to Reproduce
Save the above as
lambda.k
. Save the following assimple.lambda
:Then run:
This gives this huge error message:
Here is the file it created: kore-exec.tar.gz
If the LLVM backend is used, it doesn't crash, but it stops at a point where I would have thought that the rule marked
debugme
would apply. Not sure if I'm doing something silly.I tried debugging with GDB but it gives errors as well. Not sure if this is related. Excerpt from GDB session:
Expected Results
No crash.