3

次の操作可能なHaskellコードについて考えてみましょう。

コードA

describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
    where what [] = "empty."
          what [x] = "a singleton list."
          what xs = "a longer list."

これは、 FunctionsのSyntaxから抜粋したスニペットです。ここで、次のひどくインデントされて動作しないHaskellコードから始めると仮定します。

コードB

describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."

次に、正しくインデントされていないコード(コードB)を取得し、正しくインデントして操作コード(コードA)を生成できるツールはありますか?

4

1 に答える 1

6

インデントされていないHaskellコードは非常にあいまいになる可能性があるため、これを行うことはほとんど不可能です(これを実行できるツールを自分で探していたので、これを応答として書いています)。

次のコードを検討してください。

describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
  where what [] = "empty."
        what [x] = "a singleton list."
        what xs = "a longer list."

いくつかの代替の有効な解釈も考えてみましょう。

 -- top-level function "what" without type signature
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
  where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."

-- same as above
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
  where what [] = "empty."
        what [x] = "a singleton list."
what xs = "a longer list."

-- There might be a `data String a b`, so `String describeList xs` is
-- a type. The clause then becomes a guarded pattern match (similar to 
-- `let bar :: Int = read "1"`) with scoped type variables. The `where`
-- clause is still syntactically valid. The whole thing might not compile,
-- but a syntax tool can't know that.
describeList :: [a] -> String
                       describeList xs = "The list is " ++ what xs
  where what [] = "empty."
        what [x] = "a singleton list."
what xs = "a longer list."
于 2012-08-18T11:43:40.387 に答える