Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Too lazy reduction when using seq. #67

Open
BigET opened this issue Dec 14, 2013 · 10 comments
Open

Too lazy reduction when using seq. #67

BigET opened this issue Dec 14, 2013 · 10 comments
Labels

Comments

@BigET
Copy link

BigET commented Dec 14, 2013

If you want to make a pass of a BIG list with foldl you run the risk of stack overflow.
The solution is to use foldl' that is stric in the accumulator.
and your function should be stric in the accumulator.
here is an example that will count the words and lines in an input.

import Data.Char
import Data.List

swc d@(a,b,c) e = a `seq` b `seq` c `seq` d `seq` wwc d e
wwc (0,0,False) c = wwc (1,0,False) c
wwc (l,w,_) '\n' = (l + 1, w,False)
wwc (l,w,False) c | isSpace c = (l,w,False)
                  | otherwise = (l,w+1,True)
wwc (l,w,_) c | isSpace c = (l,w,False)
              | otherwise = (l,w,True)

main = interact (show . foldl' swc (0,0,False))

If I compile with ghc or hugs I get a constant space execution no matter the input.
With ajhc it will do stack overflow because we are using a recursive call (seen with gdb at the coredump).

@master-q
Copy link
Member

Also I see the BUG.

$ ajhc Main.hs -fdebug --tdir=tdir
$ cat /dev/zero | ./hs.out
zsh: broken pipe                       cat /dev/zero | 
zsh: segmentation fault (core dumped)  ./hs.out
$ gdb hs.out core.4195
Program terminated with signal 11, Segmentation fault.
#0  0x0000000000401199 in eval (gc=<error reading variable: Cannot access memory at address 0x7ffff5f42ff8>, 
    arena=<error reading variable: Cannot access memory at address 0x7ffff5f42ff0>, 
    s=<error reading variable: Cannot access memory at address 0x7ffff5f42fe8>) at tdir/rts/jhc_rts.c:134
134     {
(gdb) bt
#0  0x0000000000401199 in eval (gc=<error reading variable: Cannot access memory at address 0x7ffff5f42ff8>, 
    arena=<error reading variable: Cannot access memory at address 0x7ffff5f42ff0>, 
    s=<error reading variable: Cannot access memory at address 0x7ffff5f42fe8>) at tdir/rts/jhc_rts.c:134
#1  0x0000000000404afb in bRfW$__fR$__fMain_wwc (gc=0x7ff3937c7220, arena=0x2419020, v135371087=1, v29375222=0x7ff3935a4060, v44000781=0x6, 
    v215884592=0x2) at tdir/main_code.c:509
#2  0x0000000000406d30 in fW$__fR$__fMain_wwc (gc=0x7ff3937c7208, arena=0x2419020, v135370990=1, v29375124=0x7ff3935a4060, v44000682=0x6, 
    v215884492=0x2) at tdir/main_code.c:934
#3  0x0000000000405397 in bRfW$__fW$__fData_List_610__lgo (gc=0x7ff3937c71c8, arena=0x2419020, v209623990=0x7ff393617e90, 
    v227981241=0x7ff3935a4060, v105553558=0x6, v61835305=0x7ff393616e91) at tdir/main_code.c:624
#4  0x0000000000405470 in bRfW$__fW$__fData_List_610__lgo (gc=0x7ff3937c7178, arena=0x2419020, v209623990=0x7ff393617e88, 
    v227981241=0x7ff3935a4060, v105553558=0x6, v61835305=0x7ff393616e89) at tdir/main_code.c:630
#5  0x0000000000405470 in bRfW$__fW$__fData_List_610__lgo (gc=0x7ff3937c7128, arena=0x2419020, v209623990=0x7ff393617e80, 
    v227981241=0x7ff3935a4060, v105553558=0x6, v61835305=0x7ff393616e81) at tdir/main_code.c:630
#6  0x0000000000405470 in bRfW$__fW$__fData_List_610__lgo (gc=0x7ff3937c70d8, arena=0x2419020, v209623990=0x7ff393617e78, 
    v227981241=0x7ff3935a4060, v105553558=0x6, v61835305=0x7ff393616e79) at tdir/main_code.c:630
--snip--

Ajhc output C source code is at https://gist.github.com/master-q/7998665.

But it's difficult bug. Could you give time to fix it?

@master-q
Copy link
Member

Ajhc's foldl' is definded at https://github.com/ajhc/ajhc/blob/v0.8.0.10/lib/haskell-extras/Data/List.hs#L321.

foldl'           :: (a -> b -> a) -> a -> [b] -> a
foldl' f z xs = lgo z xs where
    lgo z []     = z
    lgo z (x:xs) = let z' = f z x in z' `seq` lgo z' xs

@master-q
Copy link
Member

Dump debug information ant put it on https://github.com/ajhc/ajhc-dumpyard/tree/master/issue67_too-lazy-reduction.

@master-q
Copy link
Member

"seq" is a foreign import primitive, defined at https://github.com/ajhc/ajhc/blob/v0.8.0.10/lib/jhc/Jhc/Basics.hs#L34.

@master-q
Copy link
Member

The "seq" primitive's implementation is defined at https://github.com/ajhc/ajhc/blob/v0.8.0.10/src/E/Values.hs#L153.

prim_seq a b | isWHNF a = b
prim_seq a b = caseUpdate emptyCase { eCaseScrutinee = a, eCaseBind =  (tVr emptyId (getType a)), eCaseDefault = Just b, eCaseType = getType b }

isWHNF? https://github.com/ajhc/ajhc/blob/v0.8.0.10/src/E/E.hs#L25

isWHNF ELit {} = True
isWHNF ELam {} = True
isWHNF EPi {} = True
isWHNF ESort {} = True
isWHNF ELetRec { eBody = e } = isWHNF e
isWHNF _ = False

Umm...

@master-q
Copy link
Member

I think "caseUpdate" should force "a" arity such like "seq"...

@master-q
Copy link
Member

swc d@(a,b,c) e = a `seq` b `seq` c `seq` d `seq` wwc d e
-- Equal...
swc d@(a,b,c) e = seq a (seq b (seq c (seq d (wwc d e))))

@master-q
Copy link
Member

First E.

in let Main.2_a[](Main.v199[]∷*) = x4[](Main.v199[]∷*); in let
    Main.1_d[](Main.v199[]∷*,Main.v200[]∷*,Bool) =
      x2[](Main.v199[]∷*,Main.v200[]∷*,Bool);
    in (Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.∀Jhc.Basics.17_b.
    (Jhc.Basics.16_a[]∷*)  (Jhc.Basics.17_b[]∷*)  (Jhc.Basics.17_b[]∷*)) (Main.v199[]∷*) (Main.v199[]∷*
                                                                                           ,Main.v200[]∷*
                                                                                           ,Bool) (Main.2_a[](Main.v199[]∷*)) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
                                                                                                                                                  Jhc.Basics.17_b.
    (Jhc.Basics.16_a[]∷*)  (Jhc.Basics.17_b[]∷*)  (Jhc.Basics.17_b[]∷*)) (Main.v200[]∷*) (Main.v199[]∷*
                                                                                           ,Main.v200[]∷*
                                                                                           ,Bool) (Main.3_b[](Main.v200[]∷*)) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
                                                                                                                                                  Jhc.Basics.17_b.
    (Jhc.Basics.16_a[]∷*)  (Jhc.Basics.17_b[]∷*)  (Jhc.Basics.17_b[]∷*)) Bool (Main.v199[]∷*
                                                                                ,Main.v200[]∷*
                                                                                ,Bool) (Main.4_c[]Bool) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
                                                                                                                            Jhc.Basics.17_b.
    (Jhc.Basics.16_a[]∷*)  (Jhc.Basics.17_b[]∷*)  (Jhc.Basics.17_b[]∷*)) (Main.v199[]∷*
                                                                           ,Main.v200[]∷*
                                                                           ,Bool) (Main.v199[]∷*
                                                                                  ,Main.v200[]∷*
                                                                                  ,Bool) (Main.1_d[](Main.v199[]∷*
                                                                                                     ,Main.v200[]∷*
                                                                                                     ,Bool)) ((Main.wwc[]∷∀Main.v133.
                                                                                                                          Main.v134.

@master-q
Copy link
Member

Before remove seq.

                  in
                  (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ]∷∀Jhc.Basics.16_a.
                                                                                                Jhc.Basics.17_b.
                  (Jhc.Basics.16_a[]∷*)  (Jhc.Basics.17_b[]∷*)  (Jhc.Basics.17_b[]∷*)) (Integer
                                                                                         ,Integer
                                                                                         ,Bool) (Integer
                                                                                                ,Integer
                                                                                                ,Bool) (Main.1_d[Arity 0 False,ArT](Integer
                                                                                                                                    ,Integer
                                                                                                                                    ,Bool)) (x114617052[Arity 0 False,ArT](Integer
                                                                                                                                                                           ,Integer
                                                                                                                                                                           ,Bool));
            in let
              Main.4_c[Arity 0 False,ArT,UseInfo {useOccurance = Many, minimumArgs = 0}]Bool =
                x6[]Bool;
              in
              (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ]∷∀Jhc.Basics.16_a.
                                                                                            Jhc.Basics.17_b.
              (Jhc.Basics.16_a[]∷*)  (Jhc.Basics.17_b[]∷*)  (Jhc.Basics.17_b[]∷*)) Bool (Integer
                                                                                          ,Integer
                                                                                          ,Bool) (Main.4_c[Arity 0 False,ArT]Bool) (x94502784[Arity 0 False,ArT](Integer
                                                                                                                                                                  ,Integer
                                                                                                                                                                  ,Bool));
        in let
          Main.3_b[Arity 0 False,ArT,UseInfo {useOccurance = Many, minimumArgs = 0}]Integer =
            x5[]Integer;
          in
          (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ]∷∀Jhc.Basics.16_a.
                                                                                        Jhc.Basics.17_b.
          (Jhc.Basics.16_a[]∷*)  (Jhc.Basics.17_b[]∷*)  (Jhc.Basics.17_b[]∷*)) Integer (Integer
                                                                                         ,Integer
                                                                                         ,Bool) (Main.3_b[Arity 0 False,ArT]Integer) (x103712075[Arity 0 False,ArT](Integer
                                                                                                                                                                     ,Integer
                                                                                                                                                                     ,Bool));
    in let
      Main.2_a[Arity 0 False,ArT,UseInfo {useOccurance = Many, minimumArgs = 0}]Integer =
        x4[]Integer;
      in
      (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ]∷∀Jhc.Basics.16_a.
                                                                                    Jhc.Basics.17_b.
      (Jhc.Basics.16_a[]∷*)  (Jhc.Basics.17_b[]∷*)  (Jhc.Basics.17_b[]∷*)) Integer (Integer
                                                                                     ,Integer
                                                                                     ,Bool) (Main.2_a[Arity 0 False,ArT]Integer) (x35823661[Arity 0 False,ArT](Integer
                                                                                                                                                                ,Integer
                                                                                                                                                                ,Bool));
_[](Integer,Integer,Bool) 
  <⊥:Main.hs:4:17: Unmatched pattern(Integer
                                     ,Integer
                                     ,Bool)>;(Integer
                                              ,Integer
                                              ,Bool);

@master-q
Copy link
Member

$ grep -n -e Jhc.Basics.seq -e "^--" ajhc_dump_code.log | head -40
57:                  in (Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.∀Jhc.Basics.17_b.
60:                                                                                                         ,Bool) (Main.2_a[]∷(Main.v199[]∷*)) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
64:                                                                                                         ,Bool) (Main.3_b[]∷(Main.v200[]∷*)) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
68:                                                                                              ,Bool) (Main.4_c[]∷Bool) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
317:                  in (Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.∀Jhc.Basics.17_b.
320:                                                                                                         ,Bool) (Main.2_a[]∷(Main.v199[]∷*)) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
324:                                                                                                         ,Bool) (Main.3_b[]∷(Main.v200[]∷*)) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
328:                                                                                              ,Bool) (Main.4_c[]∷Bool) ((Jhc.Basics.seq[]∷∀Jhc.Basics.16_a.
533:-- PruneUnreachable
638:                                (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
650:                            (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
658:                        (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
666:                    (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
907:-- Simplify-Init-One
1201:                                    (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
1215:                                (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
1225:                            (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
1235:                        (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
1585:                                    (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
1600:                                (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
1611:                            (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
1622:                        (Jhc.Basics.seq[Arity 4 False,\\\\ArT,<4,[A,A,S,S]>,[INLINE,_EXPORTED],λλλλ⊤]∷∀Jhc.Basics.16_a.
1670:-- After Occurance Analysis
1693:-- Simplify-Init-One
2383:-- After Occurance Analysis
2398:-- Simplify-Init-One
3004:-- After Occurance Analysis
3019:-- Simplify-Init-One
3712:-- After Occurance Analysis
3719:-- Simplify-Init-One
4204:-- After Occurance Analysis
4236:-- SimpleRecursive-Init
4481:-- FloatOutward-Init
4755:-- typeAnalyze-PreInit
5015:-- FloatOutward-Init
5293:-- Simplify-Init-Two-FloatOutCleanup
5809:-- After Occurance Analysis
5821:-- Simplify-Init-Two-FloatOutCleanup
6274:-- After Occurance Analysis
6277:-- Simplify-Init-Two-FloatOutCleanup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants