-
Notifications
You must be signed in to change notification settings - Fork 38
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
Checking if a value is a number #96
Comments
Kelvinss corrected me on reddit, it is already possible by having the following function
And calling it with A wrapper like In my opinion, there should be a more idiomatic way of doing this, preferably one with very little mana cost, since I think this is a pretty common operation. |
Actually this shouldn't happen. This is equivalent to a runtime error (since Kindelia's language is untyped), and should cause the whole operation to fail. It failing right now, though. Kindelia's HVM should be updated to account for that. That is an important point that I overlooked. A As for actually matching numbers, i don't think
Which is a pretty-cheap way to perform if-then-else with a U60 condition, just like in C. Getting and setting individual bits can be done with bitwise operators. |
If doing these operations should cause the node to reject the block, I think I'll close this issue then, since it won't be a problem.
Yes, I meant this general construction is common, which encompasses an Do you want me to open another issue for the problem you mentioned? |
This issue follows this discussion on Reddit: https://www.reddit.com/r/Kindelia/comments/vxlys5/how_to_match_numbers_in_hvm/
AFAIK, there is currently no way to check in HVM whether a value is a number or some other arbitrary term.
This would be useful for validating the input of functions on Kindelia.
Take the following contract as an example:
When matching on constructors only, it's easy to validate the input of the Counter function, since there is only a finite and small number of acceptable constructors (Inc, Add and Get in this case).
However, for numbers it's not practical to add a rule for each possible value. Even if we were to only use a small subset of numbers, that would still possibly be hundreds or thousands of rules long.
But if we don't validate
n
in this function, someone could call it with(Counter {Add {Tuple0}})
and change the state to an unacceptable(+ #0 {Tuple0})
since the numeric operations only reduce when given two numbers.What is missing here is a way to check if the input is a number and have different behaviour on each case. What I think would make sense is to have a way of matching on numbers only inside of a rule. Something like
(Counter {Add #n} = ...
to bind numbers only to the variable n. This would be the cheapest way in terms of mana cost.Another alternative would be to have an operation that returns if a given value is a number or some other term. Something like
(IsNum number) = #1)
and(IsNum ~) = #0
, whereIsNum
behaves in a similar way to the numeric operators.A third way, which is what I think Kelvinss sugested in the reddit thread (edit: i misunderstood him), is to have an operator that checks if any two arbitrary values are equal, and then we could check whether it's a number with something like
(Equal (% n #1) #0)
, whereEqual
returns 1 when the two terms are equal and 0 otherwise.The text was updated successfully, but these errors were encountered: