简体   繁体   中英

Naming strategies for describing HTML structure in Haskell

Consider the following haskell types, which describe the basic structure of a HTML document:

data HTML               = HTML HTMLElement [HTMLElement]
data HTMLElement        = HTMLElement HTMLElementTagname [HTMLAttribute]
data HTMLElementTagname = HTMLElementTagname String
data HTMLAttribute      = HTMLAttribute (HTMLAttributeKey, HTMLAttributeValue)
data HTMLAttributeKey   = HTMLAttributeKey String
data HTMLAttributeValue = HTMLAttributeValue String

My question is mainly about a proper naming strategy for this structure. Look for example at the last type HTMLAttributeValue , it shows clearly the hierarchy of the defined types, but just by the camel-case convention I'm using. Nothing would hold someone from calling it value_of_html_attribute , just to give an example. This could be regarded as problematic.

Another naming could look like this:

data HTML      = HTML HTML [Element]
data Element   = Element Tagname [Attribute]
data Tagname   = Tagname String
data Attribute = Attribute (Key, Value)
data Key       = Key String
data Value     = Value String

This however would pollute the global namespace. Maybe the types Key or Value for instance would make sense to use somewhere else in the code for other data structures. However, the later example looks way more readable to me, whereas the first one seems to be quite pedantic.

How would you implement this?

Reflecting the full hierarchy in the type names, as in your first example, seems excessive. As for the second example, if namespace pollution eventually turns out to be a problem, qualified imports are an easy workaround:

module FooBar where -- etc.

data HTML      = HTML HTML [Element]
data Element   = Element Tagname [Attribute]
data Tagname   = Tagname String
data Attribute = Attribute (Key, Value)
data Key       = Key String
data Value     = Value String
import qualified FooBar as H

someHtml :: H.HTML
someHtml = -- etc.

Some relevant prior art: blaze-markup , threepenny-gui .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM