Skip to content

Instantly share code, notes, and snippets.

@danfal
Created March 14, 2013 17:41
Show Gist options
  • Save danfal/5163438 to your computer and use it in GitHub Desktop.
Save danfal/5163438 to your computer and use it in GitHub Desktop.
Simple example of strange while-loop generation in Feldspar
import qualified Prelude as P
import Feldspar
import Feldspar.Compiler
-- Function that takes a y and returns a tuple (x,y) where x = y/2
test :: Data Int32 -> (Data Int32, Data Int32)
test y = whileLoop (0,y) (\(a,b) -> (cond a (b-a)) --(a*a)==((b-a)*(b-a)))
(\(a,b) -> ((a+1),b))
-- Dumb way of checking that two integers are not equal
cond :: Data Int32 -> Data Int32 -> Data Bool
cond a b = a*a /= b*b
-- Sane way of checking that two integers are not equal
cond2 :: Data Int32 -> Data Int32 -> Data Bool
cond2 a b = a /= b
{-
When evaluating the function test using the eval function in GHCi it behaves
as expected, but calling icompile on the function results in the following
C code:
void test(int32_t v0, struct s_int32_t_int32_t_ * out)
{
int32_t v5;
struct s_int32_t_int32_t_ v2;
(* out).member1 = 0;
(* out).member2 = v0;
v5 = ((* out).member2 - (* out).member1);
{
while((((* out).member1 * (* out).member1) != (v5 * v5)))
{
v2.member1 = ((* out).member1 + 1);
v2.member2 = (* out).member2;
(* out) = v2;
}
}
}
As you can see, this code makes the condition for the while-loop static
by assigning v5 before entering the loop, and then never updating the v5
variable.
Oddly, this does not happen if we use cond2 instead of the dumb cond.
It does not matter if the conditions are separate functions or if they are
passed directly in the first lambda expression given to the whileLoop-function.
Is there any reasonable explanation for the discrepancy between the results of
the eval function and the compiled C code?
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment