How to apply Operational Semantics to this function Say I have a function like this:

function colorize(integer) {
  var hex = '#'

  if (integer &gt; 1) hex += 'ff'
  else hex += '00'
  if (integer &lt; 100) hex += 'aa'
  else hex += 'bb'
  if (integer &gt; 10) hex += '11'
  else hex += '22'

  return hex
}


It returns CSS hex colors, not named colors or rgb(...) values or anything else. For now we can ignore the low-level / platform specific details of JavaScript and hardware and just deal with this abstractly as if it was in a perfect environment.

The goal is to use small step operational semantics to prove that it returns a CSS hex color. If it requires too much initialization to do the proof, then assume the initialization is already handled so all that's focused on is the specific function and its implementation.

From my understanding this means we would have some rule for var, =, if, ===, +=, and return. In addition, there is sort of a trace, since it is modifying the value in memory step by step.

From my understanding this would involve using the operational semantics configuration evaluation〈a, σ〉→〈a', σ'〉like:

〈=, σ〉→〈=, σ'[hex ↦ #]〉
&nbsp;&nbsp;&nbsp;→
〈if (e) x else y, σ〉→〈...〉
&nbsp;&nbsp;&nbsp;→
&nbsp;&nbsp;&nbsp;...

I am not sure how to:


Actually write the configurations out.
Write out the sequence of the evaluations.
Write a proof that the output will be a hex string such as #00aa22 if given 0 for example.


I was wondering if one could point me in the right direction on how to solve this. Basically:


What some configurations look like using this function as an example.
What a sequence of the evaluations (or trace) looks like.
What a proof would entail (not the actual proof as I imagine there will be lots of steps), maybe just the first few steps of the proof.


Thank you very much for your help.
You are looking for Hoare logic. Writing out the answer to the question would pretty much amount to copying the introduction to Hoare logic from Wikipedia. I suggest that you have a look at proving program correctness using Hoare logic, and try to apply it to your problem. It's actually an easy case because there are no loops. If you get stuck, come back and ask a specific question about Hoare logic.

I would break up the problem into proving several properties about hex:


The length of hex is 7 at the end.
The first character of hex is #.
All characters, except the first one, are hexadecimal digits.


Also not that the proof does not require you to write out any operational semantics. You can just use the Hoare logic to argue about your program, and there is a theorem which guarantees that Hoare logic is correct with respect to the standard operational semantics.