oh, btw I am using GHC 6.10.1 (on Linux x86_64)<br><br><div class="gmail_quote">2008/12/18 Cetin Sert <span dir="ltr"><<a href="mailto:cetin.sert@gmail.com">cetin.sert@gmail.com</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div></div><div class="Wj3C7c">Hi,<br><br>I tested the following, why does the rewrite rules not fire
when using tuples also in testRewrite2, testRewriteReverse2? compiling:
<span style="font-family: courier new,monospace;">rm *.o; ghc -fglasgow-exts -ddump-simpl-stats -O9 --make rules.hs</span><br>
<br><span style="font-family: courier new,monospace;">module Main where</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">main :: IO ()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">main = do</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print $ test 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print $ test2 0</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print $ testRewrite 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print $ testRewriteReverse 0</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print $ testRewrite2 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print $ testRewriteReverse2 0</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">test :: a → Bool</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">test x = pi</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> f = replicate 2000 x</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> i = repeat x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pf = lenGT f 300</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pi = lenGT i 300</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">test2 :: a → (Bool,Bool)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">test2 x = (pf,pi)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> f = replicate 2000 x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> i = repeat x</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pf = lenGT f 300</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pi = lenGT i 300</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">testRewrite :: a → Bool</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">testRewrite x = pi</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> f = replicate 2000 x</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> i = repeat x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> lf = length f</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> li = length i</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pf = lf > 300</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pi = li > 300</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">testRewriteReverse :: a → Bool</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">testRewriteReverse x = pi</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> f = replicate 2000 x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> i = repeat x</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> lf = length f</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> li = length i</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pf = 300 <= lf</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pi = 300 <= li</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">testRewrite2 :: a → (Bool,Bool)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">testRewrite2 x = (pf,pi)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> f = replicate 2000 x</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> i = repeat x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> lf = length f</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> li = length i</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pf = lf > 300</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pi = li > 300</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">testRewriteReverse2 :: a → (Bool,Bool)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">testRewriteReverse2 x = (pf,pi)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> f = replicate 2000 x</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> i = repeat x</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> lf = length f</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> li = length i</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> pf = 300 <= lf</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> pi = 300 <= li</span></div></div><div><br style="font-family: courier new,monospace;"><div class="Ih2E3d"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lengthOP :: (Num a, Ord a) ⇒ Bool → (a → a → Bool) → [b] → a → Bool</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">lengthOP v (⊜) [] n = 0 ⊜ n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lengthOP v (⊜) xxs n = co xxs 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;"></div></div><div class="Ih2E3d"><div><span style="font-family: courier new,monospace;"> co (_:xs) c | n > c = co xs (c+1)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> | otherwise = v</span><br style="font-family: courier new,monospace;"></div></div><span style="font-family: courier new,monospace;"> co [] c = c ⊜ n</span><div class="Ih2E3d">
<div>
<br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lenEQ = lengthOP False (==)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lenLT = lengthOP False (<)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">lenLE = lengthOP False (<=)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lenGT = lengthOP True (>)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">lenGE = lengthOP True (>=)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">{-# RULES</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">-- | length</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenEQ_LHS" forall xs n. (length xs) == n = lenEQ xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"lenLT_LHS" forall xs n. (length xs) < n = lenLT xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenLE_LHS" forall xs n. (length xs) <= n = lenLE xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"lenGT_LHS" forall xs n. (length xs) > n = lenGT xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenGE_LHS" forall xs n. (length xs) >= n = lenGE xs n</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenEQ_RHS" forall xs n. n == (length xs) = lenEQ xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenLT_RHS" forall xs n. n < (length xs) = lenGE xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"lenLE_RHS" forall xs n. n <= (length xs) = lenGT xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenGT_RHS" forall xs n. n > (length xs) = lenLE xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"lenGE_RHS" forall xs n. n >= (length xs) = lenLT xs n</span><br style="font-family: courier new,monospace;"></div></div><span style="font-family: courier new,monospace;"> #-}</span><br>
<br>Best Regards,<br>Cetin Sert<br><br><div class="gmail_quote"><div class="Ih2E3d">2008/12/18 Luke Palmer <span dir="ltr"><<a href="mailto:lrpalmer@gmail.com" target="_blank">lrpalmer@gmail.com</a>></span><br></div>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
This does not answer your question, but you can solve this problem without rewrite rules by having length return a lazy natural:<div><div></div><div class="Wj3C7c"><br><br> data Nat = Zero | Succ Nat<br><br>And defining lazy comparison operators on it.<br>
<br>Of course you cannot replace usages of Prelude.length. But I am really not in favor of rules which change semantics, even if they only make things less strict. My argument is the following. I may come to rely on such nonstrictness as in:<br>
<br> bad xs = (length xs > 10, length xs > 20)<br><br>bad [1..] will return (True,True). However, if I do an obviously semantics-preserving refactor:<br><br> bad xs = (l > 10, l > 20)<br> where<br> l = length xs<br>
<br>My semantics are not preserved: bad [1..] = (_|_, _|_) (if/unless the compiler is clever, in which case my semantics depend on the compiler's cleverness which is even worse)<br><br>Luke<br><br>2008/12/18 Cetin Sert <span dir="ltr"><<a href="mailto:cetin.sert@gmail.com" target="_blank">cetin.sert@gmail.com</a>></span><br>
<div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div>Hi *^o^*,<br><br>With the following rewrite rules:<br>
<br><span style="font-family: courier new,monospace;">lengthOP :: (Num a, Ord a) ⇒ Bool → (a → a → Bool) → [b] → a → Bool</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">lengthOP v (⊜) [] n = 0 ⊜ n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lengthOP v (⊜) xxs n = co xxs 0</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> co [] c = c ⊜ n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> co (_:xs) c | n > c = co xs (c+1)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> | otherwise = v</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lenEQ = lengthOP False (==)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lenLT = lengthOP False (<)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">lenLE = lengthOP False (<=)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">lenGT = lengthOP True (>)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">lenGE = lengthOP True (>=)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">{-# RULES</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">-- | length</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenEQ_LHS" forall xs n. (length xs) == n = lenEQ xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"lenLT_LHS" forall xs n. (length xs) < n = lenLT xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenLE_LHS" forall xs n. (length xs) <= n = lenLE xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"lenGT_LHS" forall xs n. (length xs) > n = lenGT xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenGE_LHS" forall xs n. (length xs) >= n = lenGE xs n</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenEQ_RHS" forall xs n. n == (length xs) = lenEQ xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenLT_RHS" forall xs n. n < (length xs) = lenGE xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"lenLE_RHS" forall xs n. n <= (length xs) = lenGT xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"lenGT_RHS" forall xs n. n > (length xs) = lenLE xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"lenGE_RHS" forall xs n. n >= (length xs) = lenLT xs n</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">-- | genericLength</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"glenEQ_LHS" forall xs n. (genericLength xs) == n = lenEQ xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"glenLT_LHS" forall xs n. (genericLength xs) < n = lenLT xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"glenLE_LHS" forall xs n. (genericLength xs) <= n = lenLE xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"glenGT_LHS" forall xs n. (genericLength xs) > n = lenGT xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"glenGE_LHS" forall xs n. (genericLength xs) >= n = lenGE xs n</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"glenEQ_RHS" forall xs n. n == (genericLength xs) = lenEQ xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"glenLT_RHS" forall xs n. n < (genericLength xs) = lenGE xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"glenLE_RHS" forall xs n. n <= (genericLength xs) = lenGT xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">"glenGT_RHS" forall xs n. n > (genericLength xs) = lenLE xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">"glenGE_RHS" forall xs n. n >= (genericLength xs) = lenLT xs n</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> #-}</span><br style="font-family: courier new,monospace;"><br>1) Is there a way to tell where 'length' is mentioned, what is meant is for example 'Prelude.length' or any length that works on lists?<br>
2) How can I avoid the following error messages?<br><br><span style="font-family: courier new,monospace;">module Main where</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">import Data.List</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">main :: IO Int</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print $ length (repeat 0) > 200</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print $ 200 < length (repeat 0)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> print $ genericLength (repeat 0) > 200</span><span style="font-family: courier new,monospace;"> -- error</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> print $ 200 < genericLength (repeat 0) -- error</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return 0</span><br style="font-family: courier new,monospace;">
<br>########:<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Could not deduce (Ord i) from the context (Eq i, Num i)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> arising from a use of `lenEQ' at </span>########<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Possible fix: add (Ord i) to the context of the RULE "glenEQ_LHS"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> In the expression: lenEQ xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> When checking the transformation rule "glenEQ_LHS"</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;">########<span style="font-family: courier new,monospace;">:</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Could not deduce (Ord a) from the context (Eq a, Num a)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> arising from a use of `lenEQ' at </span>########<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Possible fix: add (Ord a) to the context of the RULE "glenEQ_RHS"</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> In the expression: lenEQ xs n</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> When checking the transformation rule "glenEQ_RHS"</span><br style="font-family: courier new,monospace;">
<br>3) What speaks for/against broad usage of such rewrite rules involving list lengths?<br><br>Best Regards,<br><font color="#888888">Cetin Sert<br>
</font><br></div></div>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>
</div></div></blockquote></div><br>
</blockquote></div><br>