phillipstanleymarbell / Noisy-lang-compiler

Noisy language compiler
MIT License
17 stars 1 forks source link

Newton Invariant Signature ambiguity #107

Open hyuglim opened 7 years ago

hyuglim commented 7 years ago

Relevant conversation https://psm.slack.com/archives/D0NTASB6X/p1488489574000016 which is copied at the bottom of this issue

If there are multiple invariants with same signatures, then the Newton API call will just return the first invariant whose signature matches the set of parameters passed in. In the context of a sensor platform, since each type represents a signal, we would expect each invariant to have a unique signature (after https://github.com/phillipstanleymarbell/Noisy-lang-compiler/pull/106 is merged).

However, should the hardware manufacturer, for some reason, want this feature, we should examine how this can be achieved. If it cannot be achieved, then we need to make a pass to detect if there is any two invariants with identical signatures.

Convo:

Hmmm I think I am encountering a situation where I previously thought of the need for tagging. I have the following invariant.

{
    period >= 3 * second,
    period <= 9 * second
}```
and this pendulum code in C.
```    acceleration prevXacceleration = (acceleration) readFromXAccelerometer();
    time durationInSeconds = 1000 * 30; // thirty seconds
    time startTime = readFromSystemClock();
    time period = -1;

    int swingCount = 0;

    while (readFromSystemClock() < startTime + durationInSeconds)
    {
        acceleration xAcceleration = (acceleration) readFromXAccelerometer();

        if (prevXacceleration * xAcceleration < 0) {
            swingCount++;
        }

        if (xAcceleration != 0) { // do not double count the change
            prevXacceleration = xAcceleration;
        }

        if (swingCount == 2) {
            period = readFromSystemClock() - startTime;    
        }
    }

    printf("detected %d swings in the pendulum\n", swingCount);

[4:21]
There are 3 variables that match to the SimplePendulum invariant because they are of type time, but the programmer only wants to check the variable period with the invariant. Checking all three of these variables with API call will result in 3 separate API reports, and the programmer will have to discern which API report is relevant. If this is the case, the programmer has the burden of matching the variable period to SimplePendulum with tagging or this following method I just thought of now, which doesn't completely solve the problem, but should help.

[4:24]
Furthermore, another ambiguous case arises if there is another invariant

ComplexPendulum: invariant(period: time) ....

, in which case we would have multiple variables that match to multiple invariants. This particular invariant can be avoided if we prohibit having multiple invariants that can have the same set of parameter Physics types, but doing so will restrict the expressiveness of Newton. Note that this ambiguous case is not handled by the new method I suggested above (edited)

[4:26]
One way we could mitigate this issue is to make the programmer name their variables period, that requires the programmer to know that invariant parameter is named period, which was the whole point of not implementing tagging in the first place.

hyuglim commented 7 years ago

Add a pass to scan the Newton description and then detect with multiple invariants have the same signature. Should be straight-forward => just see if there are invariants with same prime number IDs