M2 icon indicating copy to clipboard operation
M2 copied to clipboard

Allow truthiness for types

Open mahrud opened this issue 1 year ago • 3 comments

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.

mahrud avatar May 14 '24 22:05 mahrud

I like this idea a lot!

d-torrance avatar May 14 '24 22:05 d-torrance

@mahrud What use case do you imagine for this?

mikestillman avatar May 15 '24 06:05 mikestillman

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

mahrud avatar May 15 '24 22:05 mahrud