# 120: s@(c:_) がコンパイルエラー [↑up](bunny_notes) - issued: 2021-01-18 - 分類: A サンプルコードが fail - status: Open ## 概要 [ここ](https://www.sampou.org/haskell/report-revised-j/char.html) の showLitChar をもってこようとして、protestEsc にある記述で引っかかった。 以下をコンパイルしようとすると、Rename.hs の removeInfix でエラーする。 $$
{
cont p s@(c:_) | p c = "\\&" ++ s
cont p s       = s

f c = c >= '0' && c <= '9'

main = do putStrLn $ cont f "abc"
          putStrLn $ cont f "123"
$$}

エラーメッセージは以下の通り:

$$
{
$ bunny testrun cont.hs
/home/unno/bunny/0.9.0/bin/bunnyc -d ./jout/cont --xno-implicit-prelude /home/unno/bunny/0.9.0/lib/Prelude.hs
/home/unno/bunny/0.9.0/bin/bunnyc -d ./jout/cont --xlibrary-path /home/unno/bunny/0.9.0/lib cont.hs
bunnyc: removeInfix :AsPat (Name {origName = "s", namePos = (1,8), isConName = False}) (ParExp (InfixExp (VarExp (Name {origName = "c", namePos = (1,11), isConName = False})) (Name {origName = ":", namePos = (1,12), isConName = True}) WildcardPat))
CallStack (from HasCallStack):
  error, called at src/Rename.hs:135:21 in main:Rename
CallStack (from -prof):
  Rename.scanDecls.removeInfix (src/Rename.hs:(108,5)-(135,53))
  Rename.scanDecls.scanValueDecl2 (src/Rename.hs:(39,5)-(93,33))
  Rename.scanDecls (src/Rename.hs:(28,1)-(326,63))
  Semant.renProg (src/Semant.hs:(21,1)-(52,11))
  Main.doCompile.(...) (app/Main.hs:83:7-70)
  Main.doCompile (app/Main.hs:(77,1)-(114,33))
  Main.main (app/Main.hs:(117,1)-(132,42))
testrun: failed to compile cont.hs
$$}

## 調査ログ
## 2021-01-18

removeInfix と renPat それぞれについて、As Pattern に対応する処理を追加した:

$$
{
$ git diff
diff --git a/compiler/src/Rename.hs b/compiler/src/Rename.hs
index ba4e121..da90d57 100644
--- a/compiler/src/Rename.hs
+++ b/compiler/src/Rename.hs
@@ -132,6 +132,9 @@ scanDecls ds = do
 
     removeInfix e@(A.ListExp es) = removeInfix $ expandList es
 
+    removeInfix (A.AsPat n e) = do e' <- removeInfix e
+                                   return (A.AsPat n e')
+
     removeInfix e = error $ "removeInfix :" ++ show e
 
     scandecl (A.VDecl d@(A.ValDecl e _)) = do return ([d], [], [])
@@ -801,6 +804,9 @@ renPat (A.ListExp es) = renPat $ expandList es
 
 renPat A.WildcardPat = return PWildcard
 
+renPat (A.AsPat n e) = do p <- renPat e
+                          return (PAs (origName n) p)
+
 renPat e = error $ "renPat: " ++ show e
$$}

そうすると、処理が少し先に進んだということだと思うが、違うエラーとなった:

$$
{
$ ~/prj/bunny/compiler/bin/bunny tcompile --ddump-assump cont.hs
/home/unno/prj/bunny/compiler/bin/bunnyc -d ./jout/cont --xno-implicit-prelude /home/unno/prj/bunny/compiler/bin/../lib/Prelude.hs
/home/unno/prj/bunny/compiler/bin/bunnyc -d ./jout/cont --xlibrary-path /home/unno/prj/bunny/compiler/bin/../lib -v --ddump-assump cont.hs
implicitPrelude ... done.
doCompile ... Debugging dump of assumptions (results of the type inference).
bunnyc: qname not found: "s"
CallStack (from HasCallStack):
  error, called at src/RenUtil.hs:190:23 in main:RenUtil
CallStack (from -prof):
  RenUtil.qname (src/RenUtil.hs:(188,1)-(190,72))
  Rename.renExp (src/Rename.hs:(868,1)-(1031,66))
  Rename.renRhs (src/Rename.hs:(817,1)-(842,52))
  Rename.renDecls.renDecl (src/Rename.hs:(638,5)-(652,50))
  Rename.renDecls (src/Rename.hs:(590,1)-(652,50))
  Semant.renProg (src/Semant.hs:(21,1)-(52,11))
  Main.doCompile.(...) (app/Main.hs:83:7-70)
  Main.doCompile (app/Main.hs:(77,1)-(114,33))
  Main.main (app/Main.hs:(117,1)-(132,42))
tcompile: failed to compile cont.hs
$$}

make check は通るので、いったんここまでで commit しておく。

なお、この現象は lib/Prelude.hs を書いているときに気づいてあとまわしにしていた模様。何か所か ${-- todo: xs@(x:xs')} といったコメントが残っている。

## 2021-10-21

全件確認中:

$$
{
$ cat t120.hs 
cont p s@(c:_) | p c = "\\&" ++ s
cont p s       = s

f c = c >= '0' && c <= '9'

main = do putStrLn $ cont f "abc"
          putStrLn $ cont f "123"

unno@unno-FMVD70GN7G ~/work/bissues/120 
$ runhaskell t120.hs 
abc
\&123
unno@unno-FMVD70GN7G ~/work/bissues/120 
$ ~/prj/bunny/compiler/bin/bunny testrun t120.hs 
/home/unno/prj/bunny/compiler/bin/bunnyc -d ./jout/t120 --xno-implicit-prelude /home/unno/prj/bunny/compiler/bin/../lib/Prelude.hs
/home/unno/prj/bunny/compiler/bin/bunnyc -d ./jout/t120 --xlibrary-path /home/unno/prj/bunny/compiler/bin/../lib t120.hs
bunnyc: qname not found: "s"
CallStack (from HasCallStack):
  error, called at src/RenUtil.hs:192:23 in main:RenUtil
CallStack (from -prof):
  RenUtil.qname (src/RenUtil.hs:(190,1)-(192,72))
  Rename.renExp (src/Rename.hs:(861,1)-(1024,66))
  Rename.renRhs (src/Rename.hs:(810,1)-(835,52))
  Rename.renDecls.renDecl (src/Rename.hs:(631,5)-(645,50))
  Rename.renDecls (src/Rename.hs:(583,1)-(645,50))
  Semant.renProg (src/Semant.hs:(21,1)-(52,11))
  Main.doCompile.(...) (app/Main.hs:83:7-70)
  Main.doCompile (app/Main.hs:(77,1)-(114,33))
  Main.main (app/Main.hs:(117,1)-(132,42))
testrun: failed to compile t120.hs
$$}

## 2021-11-03

qname not found となっていることから目星をつけ、
renPat (VarPat n) にならって、renPat (AsPat n e) においても renaveVar するように修正した:

$$
{
$ git diff
diff --git a/compiler/src/Rename.hs b/compiler/src/Rename.hs
index 0291016..a8ac938 100644
--- a/compiler/src/Rename.hs
+++ b/compiler/src/Rename.hs
@@ -798,7 +798,8 @@ renPat (A.ListExp es) = renPat $ expandList es
 renPat A.WildcardPat = return PWildcard
 
 renPat (A.AsPat n e) = do p <- renPat e
-                          return (PAs (origName n) p)
+                          qn <- renameVar n
+                          return (PAs qn p)
 
 renPat e = error $ "renPat: " ++ show e
$$}

これによって、qname not found にはならなくなったが、パターンマッチは期待通り行われていない(引数が as pattern の場合には決してマッチしないようだ)。

けいぞく。