Skip to content

Instantly share code, notes, and snippets.

@roboguy13
Last active August 29, 2015 14:26
Show Gist options
  • Save roboguy13/134c3e7763c326b9b244 to your computer and use it in GitHub Desktop.
Save roboguy13/134c3e7763c326b9b244 to your computer and use it in GitHub Desktop.
examples/factorial let/app issues in HERMIT
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE CPP #-}
module Main where
import Prelude hiding ((*),(-))
import GHC.Exts
------------------------------------
fac :: Int -> Int
fac 0 = 1
fac n = n * fac (n -1)
unwrap :: (Int -> Int) -> Int# -> Int#
unwrap h x = case h (I# x) of
I# y -> y
wrap :: (Int# -> Int#) -> Int -> Int
wrap h (I# x) = I# (h x)
main :: IO ()
main = print (fac 10)
(*) :: Int -> Int -> Int
(I# x) * (I# y) = I# (x *# y)
(-) :: Int -> Int -> Int
(I# x) - (I# y) = I# (x -# y)
------------------------------------
set-auto-corelint True
load-as-rewrite "WWA" "WW-Ass-A.hss"
flatten-module
binding-of 'fac
ww-split [| wrap |] [| unwrap |] (ww-AssA-to-AssC WWA)
bash-extended-with [ case-elim-inline-scrutinee , inline [ 'unwrap, 'wrap, '*, '- ] ]
{ [def-rhs, let-body] ; alpha-lam 'n } -- cosmetic
*** Core Lint errors : in result of Core plugin: HERMIT0 ***
<no location info>: Warning:
In the expression: *# x_a1J6 (work_s2zJ (-# x_a1J6 1))
This argument does not satisfy the let/app invariant:
work_s2zJ (-# x_a1J6 1)
*** Offending Program ***
Rec {
* :: Int -> Int -> Int
[LclIdX, Str=DmdType]
* =
\ (ds_d2z5 [Occ=Once!] :: Int) (ds_d2z6 [Occ=Once!] :: Int) ->
case ds_d2z5 of _ [Occ=Dead] { I# x_a1MI [Occ=Once] ->
case ds_d2z6 of _ [Occ=Dead] { I# y_a1MJ [Occ=Once] ->
I# (*# x_a1MI y_a1MJ)
}
}
- :: Int -> Int -> Int
[LclIdX, Str=DmdType]
- =
\ (ds_d2z3 [Occ=Once!] :: Int) (ds_d2z4 [Occ=Once!] :: Int) ->
case ds_d2z3 of _ [Occ=Dead] { I# x_a1MK [Occ=Once] ->
case ds_d2z4 of _ [Occ=Dead] { I# y_a1ML [Occ=Once] ->
I# (-# x_a1MK y_a1ML)
}
}
fac [Occ=LoopBreaker] :: Int -> Int
[LclIdX, Str=DmdType]
fac =
letrec {
work_s2zJ [Occ=LoopBreaker] :: Int# -> Int#
[LclId, Str=DmdType]
work_s2zJ =
\ (x_a1J6 :: Int#) ->
case x_a1J6 of _ [Occ=Dead] {
__DEFAULT -> *# x_a1J6 (work_s2zJ (-# x_a1J6 1)); -- *** I think the error is here ***
0 -> 1
}; } in
\ (ds_d2z7 [Occ=Once!] :: Int) ->
case ds_d2z7 of _ [Occ=Dead] { I# x_a1J9 [Occ=Once] ->
I# (work_s2zJ x_a1J9)
}
unwrap :: (Int -> Int) -> Int# -> Int#
[LclIdX, Str=DmdType]
unwrap =
\ (h_a1J5 [Occ=Once!] :: Int -> Int) (x_a1J6 [Occ=Once] :: Int#) ->
case h_a1J5 (I# x_a1J6) of _ [Occ=Dead] { I# y_a1J7 [Occ=Once] ->
y_a1J7
}
wrap :: (Int# -> Int#) -> Int -> Int
[LclIdX, Str=DmdType]
wrap =
\ (h_a1J8 [Occ=Once!] :: Int# -> Int#)
(ds_d2z7 [Occ=Once!] :: Int) ->
case ds_d2z7 of _ [Occ=Dead] { I# x_a1J9 [Occ=Once] ->
case h_a1J8 x_a1J9 of wild_X9 { __DEFAULT -> I# wild_X9 }
}
main :: IO ()
[LclIdX, Str=DmdType]
main = print @ Int $fShowInt (fac (I# 10))
main :: IO ()
[LclIdX, Str=DmdType]
main = runMainIO @ () main
end Rec }
*** End of Offense ***
<no location info>:
Compilation had errors
Error in expression: bash-extended-with [case-elim-inline-scrutinee,inline ['unwrap,'wrap,'*,'-]]
Core Lint Failed:
<no location info>: Warning:
In the expression: *# x (work_s2zJ (-# x 1))
This argument does not satisfy the let/app invariant:
work_s2zJ (-# x 1)
fac = (let {
f_s2zu :: (Int -> Int) -> Int -> Int
[LclId, Str=DmdType]
f_s2zu =
\ (fac_s2zk :: Int -> Int) (ds_d2z9 :: Int) ->
case ds_d2z9 of _ [Occ=Dead] { I# ds_d2za ->
case ds_d2za of _ [Occ=Dead] {
__DEFAULT ->
(\ _ [Occ=Dead, OS=OneShot] ->
* ds_d2z9 (fac_s2zk (- ds_d2z9 (I# 1))))
void#;
0 -> I# 1
}
} } in
letrec {
work_s2zJ :: Int# -> Int#
[LclId, Str=DmdType]
work_s2zJ = unwrap (f_s2zu (wrap work_s2zJ)); } in
wrap work_s2zJ)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment