coalton-lang / coalton

Coalton is an efficient, statically typed functional programming language that supercharges Common Lisp.
https://coalton-lang.github.io/
MIT License
1.12k stars 67 forks source link

Integer literal folding (part 2) #1186

Closed garlic0x1 closed 1 month ago

garlic0x1 commented 1 month ago

I have an ugly cond block that optimizes translate-expression on type tc:node-integer-literal for inferred types other than regular ints.

In the following example, 1 expands to 1.0 instead of (from-int 1), essentially just doing that work at comptime in the expression translator.

COALTON-USER> (coalton-codegen 
                (declare adder (Single-Float -> Single-Float))
                (define (adder x) (+ 1 x)))
(COMMON-LISP:PROGN
 (COALTON-IMPL/GLOBAL-LEXICAL:DEFINE-GLOBAL-LEXICAL ADDER
                                                    COALTON-IMPL/RUNTIME/FUNCTION-ENTRY:FUNCTION-ENTRY)
 (COMMON-LISP:DEFUN ADDER (X-310)
   (COMMON-LISP:DECLARE (COMMON-LISP:IGNORABLE X-310))
   (COALTON-LIBRARY/MATH/NUM::|INSTANCE/NUM SINGLE-FLOAT-COALTON-LIBRARY/CLASSES:+|
    1.0 X-310))
 (COMMON-LISP:SETF ADDER (COALTON-IMPL/RUNTIME/FUNCTION-ENTRY::F1 #'ADDER))
 (COMMON-LISP:SETF (COMMON-LISP:DOCUMENTATION 'ADDER 'COMMON-LISP:VARIABLE)
                     "ADDER :: (SINGLE-FLOAT → SINGLE-FLOAT)")
 (COMMON-LISP:SETF (COMMON-LISP:DOCUMENTATION 'ADDER 'COMMON-LISP:FUNCTION)
                     "ADDER :: (SINGLE-FLOAT → SINGLE-FLOAT)")
 (COMMON-LISP:VALUES))

Repeatedly checking tc:tycon-p is not great but I struggled to create clean control flow here...

stylewarning commented 1 month ago

also figure out where *ufix-type* is because it's saying it's not in the package

also write a comment in the code that this should ideally be done by constant folding, but the benefit is too great to wait for

stylewarning commented 1 month ago

@garlic0x1 CASE will be a lot nicer than COND. :)