FluxML / Mjolnir.jl

A little less conversation, a little more abstraction
Other
87 stars 13 forks source link

Recursion and type inference #16

Open tshort opened 4 years ago

tshort commented 4 years ago

The following triggers an infinite loop.

function pow(x, n)
    n == one(n)  && return x
    n <= zero(n) && return zero(x)
    return x*pow(x, n-1)
end
using Mjolnir
@trace pow(Float64, Int)
femtomc commented 4 years ago

This actually terminates for me on 1.5:

function pow(x, n)
       n == one(n) && return x
       n <= zero(n) && return zero(x)
       return x * pow(x, n - 1)
end
1: (%1 :: const(pow), %2 :: Float64, %3 :: Int64)
  %4 = (==)(%3, 1) :: Bool
  br 2 unless %4
  br 5 (%2)
2:
  %5 = (<=)(%3, 0) :: Bool
  br 4 unless %5
  br 3
3:
  br 5 (0.0)
4:
  %6 = (-)(%3, 1) :: Int64
  %7 = (pow)(%2, %6) :: Float64
  %8 = (*)(%2, %7) :: Float64
  br 5 (%8)
5: (%9 :: Float64)
  return %9

@shashi you didn't fix this, did you?

femtomc commented 4 years ago

Ah - this is broken on master. But it's working on the last release. That's a good diff.

shashi commented 4 years ago

No I didn't fix this, but we should.