As Rain Henriks said , you could do it in a language with dependent types, which Haskell does not have (yet, quite). In Idris, let's say it would look like
factorial : (n : Int) -> if n <= 20 then Int else Integer factorial n with (n <= 20) factorial n | True = thisfactorial n factorial n | False = thatfactorial n
But how will you use this result? Well, you need to make a comparison to figure out what type to expect, and when everything is said and done, I donโt see how you won anything. For completeness, the usage site might look something like this:
use : Int -> Integer use n with (factorial n) use n | fn with (n <= 20) use n | fn | False = fn use n | fn | True = cast fn
Please note that the order of with sentences is significant! The fn binding gets the type if n <= 20 then Int else Integer ; for reasons that I donโt quite understand, the test n <= 20 should be to the right of it so that a match with the pattern affects its type.
source share