Search code examples
parsingtypescompiler-constructiongrammarlanguage-design

Concept of "Excel [Blank] Cell" in any programming language?


Excel has a Blank cell which has some interesting properties when it comes to calculations:

In the below we will assume cell A1 is blank.

  • A blank cell equals another blank cell: =A1=A1.
  • A blank cell equals '', 0, and FALSE: =A1="", =A1=0, =A1=FALSE.
  • A blank cell will coerce to the expected operand type: =A1+A1 (0), =-A1 (0), =A1&A1 ("").

I suppose the closest item I've found is the bool function in python, which covers the first two cases above:

  • bool(None) == bool(None)
  • bool(None) == bool(0), bool(None) == bool(''), bool(None) == bool(False)

But that doesn't cover the third case where it implicitly casts to the expected type. Is there anything in a language that covers that?


Here is a video showing some of the properties: https://gyazo.com/b23989ba1fd28500aff32a6b6cb6dca5.


Solution

  • Is there anything in a language that covers that?

    Short answer:

    Languages that support syntactic unification, e.g. Prolog, Datalog, Answer Set Programming, ... .
    These programming languages are typically Logic programming languages.



    Here are demonstrations of your list using SWI-Prolog

    Note: In Prolog = is unification not comparison ==. In some situations if you think of a variable as a named pointer and unification as setting pointers equal it makes sense but that analogy works only in specific cases, you have been warned.


    • A blank cell equals another blank cell: =A1=A1.
    ?- A = B.
    A = B.
    

    Now if A is bound to a value then B is also bound because they are unified.

    ?- A = B,A=1.
    A = B, B = 1.
    

    • A blank cell equals '', 0, and FALSE: =A1="", =A1=0, =A1=FALSE.

    I will take that to mean that a blank cell has a primitive data type with a default value of that type.

    Since Prolog is not strongly typed a variable has no type until a value is bound to the variable, think Python with Duck typing. Even then the concept of a specific type may not be what one expects.

    However when the second statement is considered with the third statement

    • A blank cell will coerce to the expected operand type: =A1+A1 (0), =-A1 (0), =A1&A1 ("").

    then as noted the Prolog variable (Excel blank cell) acquires the type upon binding.

    ?- (var(B)->write('B is variable');write('B is not variable')),nl,(integer(B)->write('B is integer');write('B is not integer')),nl,A is 2,(integer(A)->write('A is integer');write('A is not integer')),nl,A=B,(integer(B)->write('B is integer');write('B is not integer')).
    B is variable
    B is not integer
    A is integer
    B is integer
    B = A, A = 2.