potassco / clorm

🗃️ A Python ORM-like interface for the Clingo Answer Set Programming (ASP) reasoner
https://clorm.readthedocs.io
MIT License
52 stars 5 forks source link

Add support for bool #98

Closed florianfischer91 closed 2 years ago

florianfischer91 commented 2 years ago

This PR adds support for bool to be used as a type annotation when defining a predicate class

class T(Predicate):
    a: bool
    b: StrictBool
    c: Union[bool, int]

To convert from/to bool the following lookups will be used (strings converted to lower case)

BOOL_FALSE = {0, '0', 'off', 'f', 'false', 'n', 'no'}
BOOL_TRUE = {1, '1', 'on', 't', 'true', 'y', 'yes'}

If StrictBool is used, just integers 0 and 1 are allowed

In Python < 3.7 Union[bool,int] is simplified to int, so if you can't upgrade you can use the more stricter version Union[StrictBool, int] instead.

Also keep in mind when defining Union annotations, it's recommended to include the most specific type first and followed by less specific types

daveraja commented 2 years ago

I'm unclear about the use-case for a boolean field, as boolean terms don't appear very often in ASP programs. In most cases that I can think of, there would be a more compact way to encoding things.

It could still be useful but I'm a bit wary of having it in the core fields. I wonder if it would make more sense to put it in lib along with the date field?

To make this work nicely I guess it would need an extra API function to core to register type -> field matches. Something like register_field_type(). This way adding extra fields could be done without touching core, and users could potentially add their own fields or override the defaults.

florianfischer91 commented 2 years ago

Well the use-case is the same as for date or time, that you have a different representations in python and asp. Therefore i thought it would be a good idea to have it in clorm (especially because bool is a builtin type). So i can move it to lib and create a separate file bool.py or boolean.py (if you want to have it in clorm) otherwise i can also just add it to my own project. Regarding the register function: I have already seen the related issue, but as long as there is no such functionality we have to touch core.py^^

daveraja commented 2 years ago

Well the use-case is the same as for date or time, that you have a different representations in python and asp. Therefore i thought it would be a good idea to have it in clorm (especially because bool is a builtin type). So i can move it to lib and create a separate file bool.py or boolean.py (if you want to have it in clorm) otherwise i can also just add it to my own project. Regarding the register function: I have already seen the related issue, but as long as there is no such functionality we have to touch core.py^^

Ok, I think adding it to lib/bool.py or lib/boolean.py would be better. My feeling is that it wouldn't be used often, but useful to have when it is needed. I'll look more into the register function.