on master help sign in
style: modernize for Haskell style guide
commit41dfc6ce01983c86ade40ca067b49423ac99dada authorEvan Czaplicki <info@elm-lang.org> dateMon 10 Nov 2014, 17:08 (12 years ago) parent0a27d89
1 file changed+34−3
src/AST/Variable.hs+34−3
src/AST/Variable.hs+34−3
@@ -7,27 +7,37 @@ import Text.PrettyPrint as P
77import qualified AST.Helpers as Help
88import AST.PrettyPrint
99
10+
11+-- VARIABLES
12+
1013newtype Raw = Raw String
1114 deriving (Eq,Ord,Show)
1215
16+
1317data Home
1418 = BuiltIn
1519 | Module [String]
1620 | Local
1721 deriving (Eq,Ord,Show)
1822
23+
1924data Canonical = Canonical
2025 { home :: !Home
2126 , name :: !String
22− } deriving (Eq,Ord,Show)
27+ }
28+ deriving (Eq,Ord,Show)
29+
2330
2431local :: String -> Canonical
2532local x = Canonical Local x
2633
34+
2735builtin :: String -> Canonical
2836builtin x = Canonical BuiltIn x
2937
30−-- To help with pattern matching on some common canonical variables:
38+
39+-- VARIABLE RECOGNIZERS
40+
3141is :: [String] -> String -> Canonical -> Bool
3242is home name var =
3343 var == Canonical (Module home) name
@@ -83,12 +93,16 @@ isText =
8393 is ["Text"] "Text"
8494
8595
96+-- VARIABLE TO STRING
97+
8698class ToString a where
8799 toString :: a -> String
88100
101+
89102instance ToString Raw where
90103 toString (Raw x) = x
91104
105+
92106instance ToString Canonical where
93107 toString (Canonical home name) =
94108 case home of
@@ -96,14 +110,20 @@ instance ToString Canonical where
96110 Module path -> List.intercalate "." (path ++ [name])
97111 Local -> name
98112
113+
114+-- LISTINGS
115+
99116-- | A listing of values. Something like (a,b,c) or (..) or (a,b,..)
100117data Listing a = Listing
101118 { _explicits :: [a]
102119 , _open :: Bool
103120 } deriving (Eq,Ord,Show)
104121
122+
105123openListing :: Listing a
106−openListing = Listing [] True
124+openListing =
125+ Listing [] True
126+
107127
108128-- | A value that can be imported or exported
109129data Value
@@ -112,18 +132,24 @@ data Value
112132 | ADT !String !(Listing String)
113133 deriving (Eq,Ord,Show)
114134
135+
136+-- PRETTY VARIABLES
137+
115138instance Pretty Raw where
116139 pretty (Raw var) = variable var
117140
141+
118142instance Pretty Canonical where
119143 pretty var = P.text (toString var)
120144
145+
121146instance Pretty a => Pretty (Listing a) where
122147 pretty (Listing explicits open) =
123148 P.parens (commaCat (map pretty explicits ++ dots))
124149 where
125150 dots = [if open then P.text ".." else P.empty]
126151
152+
127153instance Pretty Value where
128154 pretty portable =
129155 case portable of
@@ -132,6 +158,9 @@ instance Pretty Value where
132158 ADT name ctors ->
133159 P.text name <> pretty (ctors { _explicits = map P.text (_explicits ctors) })
134160
161+
162+-- BINARY SERIALIZATION
163+
135164instance Binary Canonical where
136165 put (Canonical home name) =
137166 case home of
@@ -146,6 +175,7 @@ instance Binary Canonical where
146175 2 -> Canonical Local <$> get
147176 _ -> error "Unexpected tag when deserializing canonical variable"
148177
178+
149179instance Binary Value where
150180 put portable =
151181 case portable of
@@ -160,6 +190,7 @@ instance Binary Value where
160190 2 -> ADT <$> get <*> get
161191 _ -> error "Error reading valid import/export information from serialized string"
162192
193+
163194instance (Binary a) => Binary (Listing a) where
164195 put (Listing explicits open) =
165196 put explicits >> put open