Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird errors when abusing functions #100

Closed
mikedanese opened this issue Feb 4, 2016 · 8 comments
Closed

Weird errors when abusing functions #100

mikedanese opened this issue Feb 4, 2016 · 8 comments

Comments

@mikedanese
Copy link
Contributor

Can you tell me if this is expected?

Given this code:

{
    identity(obj)::
        function(obj) obj,

    bind(func1, func2)::
        function(obj) func2(func1(obj)),

    local func = self.bind(function(obj) obj + 2, function(obj) obj * 3),

    "1": self.bind(self.identity, func)(1),
    "2": self.bind(self.identity, function(obj) obj + 1)(1),

    "3": self.bind(func, self.identity)(1),
    "4": self.bind(function(obj) obj + 1, self.identity)(1),

    "5": self.bind(func, self.identity)(1)(1),
    "6": self.bind(function(obj) obj + 1, self.identity)(1)(1),

    "7": func(1),
    "8": self.bind(function(obj) obj + 2, function(obj) obj * 3)(1),
}

case 1 and 2 fail with:

RUNTIME ERROR: Binary operator + requires matching types, got function and double.
        libmonad.jsonnet:8:42-48        function <func1>
        libmonad.jsonnet:6:29-38        thunk <obj>
        libmonad.jsonnet:8:65-67        function <func2>
        libmonad.jsonnet:6:23-39        function <func2>
        libmonad.jsonnet:6:23-39        function <anonymous>
        libmonad.jsonnet:10:10-42       object <anonymous>
        During manifestation

case 3 and 4 (which switch the order of the arguments from case 1 and 2) fail with:

RUNTIME ERROR: Couldn't manifest function in JSON output.
        libmonad.jsonnet:(1:1)-(21:1)   object <anonymous>
        During manifestation

case 5 and 6 (which add an extra (1) to 3 and 4) work without error and produce and semi unexpected result:

{
   "5": 1,
   "6": 1,
}

case 7 and 8 produce the expected result:

{
   "7": 9,
   "8": 9
}

It seems like anonymous and declared functions act differently

@mikedanese
Copy link
Contributor Author

Also weird:

{
    identity(obj)::
        function(obj) obj,

    addOne(obj):
        obj + 1,

    bind(func1, func2)::
        function(obj) func2(func1(obj)),

    "1": self.bind(self.identity, self.addOne)(1),
    "2": self.bind(self.addOne, self.identity)(1),
}

case 1 gives:

RUNTIME ERROR: Binary operator + requires matching types, got function and double.
        libmonad.jsonnet:6:9-14 function <func2>
        libmonad.jsonnet:9:23-39        function <anonymous>
        libmonad.jsonnet:11:10-49       object <anonymous>
        During manifestation

case 2 gives:

RUNTIME ERROR: Couldn't manifest function in JSON output.
        libmonad.jsonnet:(1:1)-(16:1)   object <anonymous>
        During manifestation

@sparkprime
Copy link
Contributor

Why

    identity(obj)::
        function(obj) obj,

and not

    identity(obj):: obj

or

    identity::
        function(obj) obj,

@mikedanese
Copy link
Contributor Author

ya that's definitely what I meant. thanks. Both of these still return an error

{
    identity(obj)::
        obj,

    addOne(obj):
        obj + 1,

    bind1(func1, func2)::
        function(obj) func2(func1(obj)),

    bind2(func1, func2)::
        function(obj)
            local tmp1 = func1(obj);
            local tmp2 = func2(tmp1);
            tmp2,

    "1": self.bind1(self.identity, self.addOne)(1),
    "2": self.bind2(self.identity, self.addOne)(1),
}

Error:

RUNTIME ERROR: Couldn't manifest function in JSON output.
        libmonad.jsonnet:(1:1)-(16:1)   object <anonymous>
        During manifestation

Is that correct?

@mikedanese
Copy link
Contributor Author

and a third:

...
    "3": self.bind2(self.identity, self.addOne)(1)(1),
}

calling what the previous error said was a function, results in:

RUNTIME ERROR: Only functions can be called, got double
        libmonad.jsonnet:19:10-53       object <anonymous>
        During manifestation

@sparkprime
Copy link
Contributor

You only have one colon on addOne, but in this case the stacktrace is bad as it did not point you at addOne. I'll look into that quickly...

@mikedanese
Copy link
Contributor Author

Awesome! Works great. Thanks for the extra pair of eyes.

$ cat libmonad.jsonnet 
{
    identity(obj)::
        obj,

    addOne(obj)::
        obj + 1,

    bind1(func1, func2)::
        function(obj) func2(func1(obj)),

    bind2(func1, func2)::
        function(obj)
            local tmp1 = func1(obj);
            local tmp2 = func2(tmp1);
            tmp2,

    "1": self.bind1(self.identity, self.addOne)(1),
    "2": self.bind1(self.addOne, self.identity)(1),
    "3": self.bind2(self.identity, self.addOne)(1),
    "4": self.bind2(self.addOne, self.identity)(1),
}
$ jsonnet libmonad.jsonnet
{
   "1": 2,
   "2": 2,
   "3": 2,
   "4": 2
}

So the error was coming from trying to serialize addOne where it was declared since it wasn't marked private...

@sparkprime
Copy link
Contributor

s/private/hidden :)

@sparkprime
Copy link
Contributor

I just committed a fix for the stacktrace, it now refers to the line containing the obj + 1 which is not exactly perfect, but other than pointing to the (obj) tokens at which I don't currently record location information, it's the best I can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants