equivalent of EXPLAIN PLAN with GHC?

Don Stewart dons at galois.com
Sun Aug 2 04:35:10 EDT 2009


dons:
> Showing what transformations happened. Notably, 2 occurences of the "streamU/unstreamU"
> transformation, to remove intermediate structures.
> 
> The final code looks like:
> 
>     $s$wfold :: Int# -> Int#
>    $s$wfold =
>   \ (sc_s19l :: Int#) ->
>     case modInt# (-9223372036854775807) 3 of wild21_a14L {
>       __DEFAULT ->
>         case modInt# (-9223372036854775807) 5 of wild211_X159 {
>           __DEFAULT -> $wfold sc_s19l (-9223372036854775806);
>           0 ->
>             $wfold
>               (+# sc_s19l (-9223372036854775807)) (-9223372036854775806)
>         };
>       0 ->
>         $wfold
>           (+# sc_s19l (-9223372036854775807)) (-9223372036854775806)
>     }
>     $wfold :: Int# -> Int# -> Int#

Oh, this is also a good illustration of why you should use `rem` instead
of `mod` , if you're not expecting negative numbers:

The core with filterU (\x -> x `rem` 3 == 0 || x `rem` 5 == 0) avoids all the
checks for -9223372036854775806.

    $s$wfold :: Int# -> Int#

    $s$wfold =
      \ (sc_s18m :: Int#) ->
        $wfold sc_s18m (-9223372036854775806)
    $wfold :: Int# -> Int# -> Int#

    $wfold =
      \ (ww_s17v :: Int#) (ww1_s17z :: Int#) ->
        case ># ww1_s17z 999 of wild_a15d {
          False ->
            case ww1_s17z of wild2_a14K {
              __DEFAULT ->
                case remInt# wild2_a14K 3 of wild1_B1 {
                  __DEFAULT ->
                    case remInt# wild2_a14K 5 of wild21_Xp {
                      __DEFAULT -> $wfold ww_s17v (+# wild2_a14K 1);
                      0 ->
                        $wfold
                          (+# ww_s17v wild2_a14K) (+# wild2_a14K 1)
                    };
                  0 ->
                    $wfold
                      (+# ww_s17v wild2_a14K) (+# wild2_a14K 1)
                };
              (-9223372036854775808) -> $s$wfold ww_s17v
            };
          True -> ww_s17v
        }


Yes, that's better code.

But remember,

    Prelude Test.QuickCheck> quickCheck (\x -> x `mod` 3 == x `rem` 3)
    Falsifiable, after 2 tests:
    -1


-- Don


More information about the Glasgow-haskell-users mailing list