用于缩进严重缩进代码的工具

时间:2012-08-18 10:54:41

标签: haskell indentation

考虑以下操作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."

这是摘自Syntax in Functions的摘录。现在假设我们从以下一段非常缩进和不可操作的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)?

1 个答案:

答案 0 :(得分: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."