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

Allow truthiness for types #3229

Open
mahrud opened this issue May 14, 2024 · 3 comments
Open

Allow truthiness for types #3229

mahrud opened this issue May 14, 2024 · 3 comments

Comments

@mahrud
Copy link
Member

mahrud commented May 14, 2024

I'll explain via an example:

i1 : Truthy = new Type of List;

i2 : Truthy == Boolean := (T, B) -> T#0 === B;

i3 : T = new Truthy from {true}

o3 = {true}

o3 : Truthy

i4 : F = new Truthy from {false}

o4 = {false}

o4 : Truthy

Given such a type, it would be desirable to use it in if .. then .. else .. clauses, but this fails:

i5 : if T then print "true"
stdio:5:4:(3): error: expected true or false

Even though this works fine:

i6 : T == true

o6 = true

i8 : T == false

o8 = false

We can define binary operations fine:

i9 : not Truthy := T -> new Truthy from {not T#0};

i10 : not T

o10 = {false}

o10 : Truthy

i17 : Truthy and Truthy := (T1, T2) -> new Truthy from {T1#0 and T2#0};

i18 : T and F

o18 = {false}

o18 : Truthy

i19 : T and T

o19 = {true}

o19 : Truthy

One solution is having if and while clauses evaluate truthiness using a method that can be installed from the top level.

One application of this is having lazily evaluated predicates and binary operations that take advantage of this, or even basic symbolic logic.

@d-torrance
Copy link
Member

I like this idea a lot!

@mahrud mahrud added Engine Macaulay2/e enhancement and removed Engine Macaulay2/e labels May 14, 2024
@mikestillman
Copy link
Member

@mahrud What use case do you imagine for this?

@mahrud
Copy link
Member Author

mahrud commented May 15, 2024

Addendum: there should also be a method isError which can be installed on types and is used by try .. then .. else .. clauses.

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

No branches or pull requests

3 participants