[Haskell] Layout

Haskell permits the omission of the braces and semicolons used in several grammar productions, by using layout to convey the same information. This allows both layout-sensitive and layout-insensitive styles of coding, which can be freely mixed within one program. Because layout is not required, Haskell programs can be straightforwardly produced by other programs.

The effect of layout on the meaning of a Haskell program can be completely specified by adding braces and semicolons in places determined by the layout. The meaning of this augmented program is now layout insensitive.

Informally stated, the braces and semicolons are inserted as follows. The layout (or “off-side”) rule takes effect whenever the open brace is omitted after the keyword where, let, do, or of.
(1)When this happens, the indentation of the next lexeme (whether or not on a new line) is remembered and the omitted open brace is inserted (the whitespace preceding the lexeme may include comments).
(2)For each subsequent line, if it contains only whitespace or is indented more, then the previous item is continued (nothing is inserted);
(3)if it is indented the same amount, then a new item begins (a semicolon is inserted);
(4)and if it is indented less, then the layout list ends (a close brace is inserted).

If the indentation of the non-brace lexeme immediately following a where, let, do or of is less than or equal to the current indentation level, then instead of starting a layout, an empty list “{}” is inserted, and layout processing occurs for the current level (i.e. insert a semicolon or close brace).

Note: If the first token after a where (say) is not indented more than the enclosing layout context, then the block must be empty, so empty braces are inserted. The {n} token is replaced by < n >, to mimic the situation if the empty braces had been explicit. —— P133

(5)A close brace is also inserted whenever the syntactic category containing the layout list ends; that is, if an illegal lexeme is encountered at a point where a close brace would be legal, a close brace is inserted.

The layout rule matches only those open braces that it has inserted; an explicit open brace must be matched by an explicit close brace. Within these explicit open braces, no layout processing is performed for constructs outside the braces, even if a line is indented to the left of an earlier implicit open brace.

Section 10.3 gives a more precise definition of the layout rules.

Given these rules, a single newline may actually terminate several layout lists. Also, these rules permit:

f x = let a = 1; b = 2 
          g y = exp2 
       in exp1

making a, b and g all part of the same layout list.

As an example, Figure 2.1 shows a (somewhat contrived) module and Figure 2.2 shows the result of applying the layout rule to it. Note in particular:
(a)the line beginning }};pop, where the termination of the previous line invokes three applications of the layout rule, corresponding to the depth (3) of the nested where clauses,
(b)the close braces in the where clause nested within the tuple and case expression, inserted because the end of the tuple was detected, and
(c)the close brace at the very end, inserted because of the column 0 indentation of the end-of-file token.


module AStack( Stack, push, pop, top, size ) where 
data Stack a = Empty 
             | MkStack a (Stack a)

push :: a -> Stack a -> Stack a 
push x s = MkStack x s

size :: Stack a -> Int 
size s = length (stkToLst s) where 
           stkToLst Empty         = [] 
           stkToLst (MkStack x s) = x:xs where xs = stkToLst s

pop :: Stack a -> (a, Stack a) 
pop (MkStack x s) 
  = (x, case s of r -> i r where i x = x) -- (pop Empty) is an error

top :: Stack a -> a 
top (MkStack x s) = x                     -- (top Empty) is an error

Figure 2.1: A sample program


module AStack( Stack, push, pop, top, size ) where 
{data Stack a = Empty 
             | MkStack a (Stack a)

;push :: a -> Stack a -> Stack a 
;push x s = MkStack x s

;size :: Stack a -> Int 
;size s = length (stkToLst s) where 
           {stkToLst Empty         = [] 
           ;stkToLst (MkStack x s) = x:xs where {xs = stkToLst s

}};pop :: Stack a -> (a, Stack a) 
;pop (MkStack x s) 
  = (x, case s of {r -> i r where {i x = x}}) -- (pop Empty) is an error

;top :: Stack a -> a 
;top (MkStack x s) = x                     -- (top Empty) is an error
}

Figure 2.2: Sample program with layout expanded


参考

Haskell 2010 Language Report - P12

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,509评论 0 23
  • 不去想想想 更多的是去做做做 有目标 以结果为导向做做做 不拖延 来一件事做一件事 不找理由 无论结果如何 尽最大...
    ichigobaby阅读 1,234评论 0 0
  • 修行,时间 1.读书《我就是这样一个活佛》70页完结 1.5小时! 2.论坛事物 4小时! 3.猫群最后一次分享...
    橘子侠阅读 1,342评论 1 3
  • 咏东方山八景诗 一 灵泉卓锡 卓锡当年渊远长,渺传轶事在灵乡。共存古迹三溪水,汇与神州九派江。泻碧秋波堪...
    方征民阅读 1,702评论 1 0
  • 你在我眼中是最美
    长安书苑阅读 1,513评论 0 1

友情链接更多精彩内容