on master help sign in
Update input/output syntax

Going towards @rtfeldman’s version of option 2 from this thread https://groups.google.com/forum/#!topic/elm-discuss/Yvl2Q0d4l7E

commit552becccd99d0f6e56418723b0106a93aef0e308 authorEvan Czaplicki <info@elm-lang.org> dateFri 13 Mar 2015, 23:45 (12 years ago) parentdfafe9a
8 files changed+92−92
src/AST/Declaration.hs+23−23 src/AST/Expression/General.hs+1−1 src/Generate/JavaScript.hs+2−2 src/Parse/Declaration.hs+16−16 src/Parse/Helpers.hs+1−1 src/Transform/Canonicalize.hs+8−8 src/Transform/Canonicalize/Wire.hs+34−34 src/Transform/Declaration.hs+7−7
src/AST/Declaration.hs+23−23
@@ -49,10 +49,10 @@ data RawWire
4949 | LoopbackDefinition String Source.Expr
5050
5151
52−data Wire expr var loopback
52+data Wire expr var input
5353 = Input String (T.Type var)
5454 | Output String expr (T.Type var)
55− | Loopback loopback
55+ | Loopback input
5656
5757
5858data ValidLoopback =
@@ -60,7 +60,7 @@ data ValidLoopback =
6060
6161
6262data CanonicalLoopback
63− = Mailbox String T.CanonicalType
63+ = Address String T.CanonicalType
6464 | Promise String T.CanonicalType Canonical.Expr T.CanonicalType
6565
6666
@@ -144,56 +144,56 @@ instance Pretty RawWire where
144144 pretty wire =
145145 case wire of
146146 InputAnnotation name tipe ->
147− prettyWire "input" name ":" tipe
147+ prettyWire "foreign input" name ":" tipe
148148
149149 OutputAnnotation name tipe ->
150− prettyWire "output" name ":" tipe
150+ prettyWire "foreign output" name ":" tipe
151151
152152 OutputDefinition name expr ->
153− prettyWire "output" name "=" expr
153+ prettyWire "foreign output" name "=" expr
154154
155155 LoopbackAnnotation name tipe ->
156− prettyWire "loopback" name ":" tipe
156+ prettyWire "input" name ":" tipe
157157
158158 LoopbackDefinition name expr ->
159− prettyWire "loopback" name "<-" expr
159+ prettyWire "input" name "from" expr
160160
161161
162−instance (Pretty expr, Pretty var, Var.ToString var, Pretty loopback) =>
163− Pretty (Wire expr var loopback) where
162+instance (Pretty expr, Pretty var, Var.ToString var, Pretty input) =>
163+ Pretty (Wire expr var input) where
164164 pretty wire =
165165 case wire of
166166 Input name tipe ->
167− prettyWire "input" name ":" tipe
167+ prettyWire "foreign input" name ":" tipe
168168
169169 Output name expr tipe ->
170170 P.vcat
171− [ prettyWire "output" name ":" tipe
172− , prettyWire "output" name "=" expr
171+ [ prettyWire "foreign output" name ":" tipe
172+ , prettyWire "foreign output" name "=" expr
173173 ]
174174
175− Loopback loopback ->
176− pretty loopback
175+ Loopback input ->
176+ pretty input
177177
178178
179179instance Pretty ValidLoopback where
180180 pretty (ValidLoopback name maybeExpr tipe) =
181181 P.vcat
182− [ prettyWire "loopback" name ":" tipe
183− , maybe P.empty (prettyWire "loopback" name "<-") maybeExpr
182+ [ prettyWire "input" name ":" tipe
183+ , maybe P.empty (prettyWire "input" name "<-") maybeExpr
184184 ]
185185
186186
187187instance Pretty CanonicalLoopback where
188− pretty loopback =
189− case loopback of
190− Mailbox name tipe ->
191− prettyWire "loopback" name ":" tipe
188+ pretty input =
189+ case input of
190+ Address name tipe ->
191+ prettyWire "input" name ":" tipe
192192
193193 Promise name _promiseType expr resultType ->
194194 P.vcat
195− [ prettyWire "loopback" name ":" resultType
196− , prettyWire "loopback" name "<-" expr
195+ [ prettyWire "input" name ":" resultType
196+ , prettyWire "input" name "<-" expr
197197 ]
198198
199199
src/AST/Expression/General.hs+1−1
@@ -67,7 +67,7 @@ data Expr' ann def var
6767
6868
6969data Source
70− = Mailbox
70+ = Address
7171 | PromiseStream
7272 deriving (Show)
7373
src/Generate/JavaScript.hs+2−2
@@ -216,8 +216,8 @@ expression (A region expr) =
216216
217217 LoopbackIn name source ->
218218 case source of
219− Mailbox ->
220− return $ obj ["_P","mailbox"] `call` [ string name ]
219+ Address ->
220+ return $ obj ["_P","input"] `call` [ string name ]
221221
222222 PromiseStream ->
223223 return $ obj ["_P","loopbackIn"] `call` [ string name ]
src/Parse/Declaration.hs+16−16
@@ -12,7 +12,7 @@ import qualified Parse.Type as Type
1212
1313declaration :: IParser D.SourceDecl
1414declaration =
15− typeDecl <|> infixDecl <|> loopback <|> input <|> output <|> definition
15+ typeDecl <|> infixDecl <|> foreign <|> input <|> definition
1616
1717
1818definition :: IParser D.SourceDecl
@@ -54,27 +54,27 @@ infixDecl =
5454 D.Fixity assoc (read [n]) <$> anyOp
5555
5656
57−input :: IParser D.SourceDecl
58−input =
59− do name <- wireBegining "input"
60− wireEnding (D.InputAnnotation name) hasType Type.expr
61−
62−
63−output :: IParser D.SourceDecl
64−output =
65− do name <- wireBegining "output"
57+foreign :: IParser D.SourceDecl
58+foreign =
59+ do try (reserved "foreign")
60+ whitespace
6661 choice
67− [ wireEnding (D.OutputAnnotation name) hasType Type.expr
68− , wireEnding (D.OutputDefinition name) equals Expr.expr
62+ [ do name <- wireBegining "input"
63+ wireEnding (D.InputAnnotation name) hasType Type.expr
64+ , do name <- wireBegining "output"
65+ choice
66+ [ wireEnding (D.OutputAnnotation name) hasType Type.expr
67+ , wireEnding (D.OutputDefinition name) equals Expr.expr
68+ ]
6969 ]
7070
7171
72−loopback :: IParser D.SourceDecl
73−loopback =
74− do name <- wireBegining "loopback"
72+input :: IParser D.SourceDecl
73+input =
74+ do name <- wireBegining "input"
7575 choice
7676 [ wireEnding (D.LoopbackAnnotation name) hasType Type.expr
77− , wireEnding (D.LoopbackDefinition name) (string "<-") Expr.expr
77+ , wireEnding (D.LoopbackDefinition name) (string "from") Expr.expr
7878 ]
7979
8080
src/Parse/Helpers.hs+1−1
@@ -33,7 +33,7 @@ reserveds =
3333 , "import", "as", "hiding", "exposing"
3434 , "export"
3535 , "deriving"
36− , "input", "output", "loopback"
36+ , "input", "output", "foreign"
3737 ]
3838
3939
src/Transform/Canonicalize.hs+8−8
@@ -85,7 +85,7 @@ moduleHelp interfaces modul@(Module.Module _ _ exports _ decls) =
8585 case wire of
8686 D.Input name _ -> name
8787 D.Output name _ _ -> name
88− D.Loopback (D.Mailbox name _) -> name
88+ D.Loopback (D.Address name _) -> name
8989 D.Loopback (D.Promise name _ _ _) -> name
9090
9191
@@ -216,21 +216,21 @@ declaration env decl =
216216 D.Wire <$>
217217 case wire of
218218 D.Input name tipe ->
219− do tipe' <- canonicalize Canonicalize.tipe "input" name env tipe
220− Wire.checkInput name tipe'
219+ do tipe' <- canonicalize Canonicalize.tipe "foreign input" name env tipe
220+ Wire.checkForeignInput name tipe'
221221 return (D.Input name tipe')
222222
223223 D.Output name expr tipe ->
224224 do expr' <- expression env expr
225− tipe' <- canonicalize Canonicalize.tipe "output" name env tipe
226− Wire.checkOutput name tipe'
225+ tipe' <- canonicalize Canonicalize.tipe "foreign output" name env tipe
226+ Wire.checkForeignOutput name tipe'
227227 return (D.Output name expr' tipe')
228228
229229 D.Loopback (D.ValidLoopback name maybeExpr tipe) ->
230230 do maybeExpr' <- T.traverse (expression env) maybeExpr
231− tipe' <- canonicalize Canonicalize.tipe "loopback" name env tipe
232− loopback' <- Wire.loopback name maybeExpr' tipe'
233− return (D.Loopback loopback')
231+ tipe' <- canonicalize Canonicalize.tipe "input" name env tipe
232+ input' <- Wire.input name maybeExpr' tipe'
233+ return (D.Loopback input')
234234
235235 D.Fixity assoc prec op ->
236236 return $ D.Fixity assoc prec op
src/Transform/Canonicalize/Wire.hs+34−34
@@ -1,4 +1,4 @@
1−module Transform.Canonicalize.Wire (checkInput, checkOutput, loopback) where
1+module Transform.Canonicalize.Wire (checkForeignInput, checkForeignOutput, input) where
22
33import Control.Applicative ((<$>))
44import Control.Monad.Error (forM_, throwError)
@@ -17,45 +17,45 @@ throw err =
1717 throwError [ P.vcat err ]
1818
1919
20−checkInput :: String -> ST.CanonicalType -> Env.Canonicalizer [P.Doc] ()
21−checkInput name tipe =
22− checkWire In name tipe tipe
20+checkForeignInput :: String -> ST.CanonicalType -> Env.Canonicalizer [P.Doc] ()
21+checkForeignInput name tipe =
22+ checkForeign In name tipe tipe
2323
2424
25−checkOutput :: String -> ST.CanonicalType -> Env.Canonicalizer [P.Doc] ()
26−checkOutput name tipe =
27− checkWire Out name tipe tipe
25+checkForeignOutput :: String -> ST.CanonicalType -> Env.Canonicalizer [P.Doc] ()
26+checkForeignOutput name tipe =
27+ checkForeign Out name tipe tipe
2828
2929
3030data Direction = In | Out
3131
3232
33−checkWire
33+checkForeign
3434 :: Direction
3535 -> String
3636 -> ST.CanonicalType
3737 -> ST.CanonicalType
3838 -> Env.Canonicalizer [P.Doc] ()
39−checkWire direction name rootType tipe =
39+checkForeign direction name rootType tipe =
4040 case tipe of
4141 ST.Aliased _ args t ->
42− checkWire direction name rootType (ST.dealias args t)
42+ checkForeign direction name rootType (ST.dealias args t)
4343
4444 ST.App (ST.Type signal) [t]
4545 | Var.isStream signal ->
46− validWireType False (Just Stream) direction name rootType t
46+ validForeignType False (Just Stream) direction name rootType t
4747
4848 | Var.isVarying signal ->
49− validWireType False (Just Varying) direction name rootType t
49+ validForeignType False (Just Varying) direction name rootType t
5050
5151 _ ->
52− validWireType False Nothing direction name rootType tipe
52+ validForeignType False Nothing direction name rootType tipe
5353
5454
5555data Signal = Stream | Varying
5656
5757
58−validWireType
58+validForeignType
5959 :: Bool
6060 -> Maybe Signal
6161 -> Direction
@@ -63,12 +63,12 @@ validWireType
6363 -> ST.CanonicalType
6464 -> ST.CanonicalType
6565 -> Env.Canonicalizer [P.Doc] ()
66−validWireType seenFunc seenSignal direction name rootType tipe =
66+validForeignType seenFunc seenSignal direction name rootType tipe =
6767 let valid localType =
68− validWireType seenFunc seenSignal direction name rootType localType
68+ validForeignType seenFunc seenSignal direction name rootType localType
6969
7070 err kind =
71− throw (wireError name direction rootType tipe kind)
71+ throw (foreignError name direction rootType tipe kind)
7272 in
7373 case tipe of
7474 ST.Aliased _ args t ->
@@ -112,7 +112,7 @@ validWireType seenFunc seenSignal direction name rootType tipe =
112112
113113 Nothing ->
114114 forM_ (ST.collectLambdas tipe)
115− (validWireType True seenSignal direction name rootType)
115+ (validForeignType True seenSignal direction name rootType)
116116
117117 ST.Record _ (Just _) ->
118118 err "It contains extended records with free type variables"
@@ -121,22 +121,22 @@ validWireType seenFunc seenSignal direction name rootType tipe =
121121 mapM_ (\(k,v) -> (,) k <$> valid v) fields
122122
123123
124−wireError
124+foreignError
125125 :: String
126126 -> Direction
127127 -> ST.CanonicalType
128128 -> ST.CanonicalType
129129 -> String
130130 -> [P.Doc]
131−wireError name direction rootType localType problemMessage =
132− [ P.text (dir "Input" "Output" ++ " Error:")
131+foreignError name direction rootType localType problemMessage =
132+ [ P.text ("Foreign " ++ dir "Input" "Output" ++ " Error:")
133133 , P.nest 4 $
134134 P.vcat
135− [ txt [ "The ", wire, " named '", name, "' has an invalid type.\n" ]
135+ [ txt [ "The foreign ", wire, " named '", name, "' has an invalid type.\n" ]
136136 , P.nest 4 (PP.pretty rootType) <> P.text "\n"
137137 , txt [ problemMessage, ":\n" ]
138138 , P.nest 4 (PP.pretty localType) <> P.text "\n"
139− , txt [ "Acceptable values for ", wire, "s include:" ]
139+ , txt [ "Acceptable values for foreign ", wire, "s include:" ]
140140 , txt [ " Ints, Floats, Bools, Strings, Maybes, Lists, Arrays, Tuples, unit values," ]
141141 , txt [ " Json.Values, ", dir "" "first-order functions, promises, ", "and concrete records." ]
142142 ]
@@ -153,12 +153,12 @@ wireError name direction rootType localType problemMessage =
153153 dir "input" "output"
154154
155155
156−loopback
156+input
157157 :: String
158158 -> Maybe Canonical.Expr
159159 -> ST.CanonicalType
160160 -> Env.Canonicalizer [P.Doc] D.CanonicalLoopback
161−loopback name maybeExpr tipe =
161+input name maybeExpr tipe =
162162 case maybeExpr of
163163 Nothing ->
164164 case ST.deepDealias tipe of
@@ -168,12 +168,12 @@ loopback name maybeExpr tipe =
168168 ]
169169 Nothing
170170 | Var.isStream stream && Var.isAddress address && a == b ->
171− return (D.Mailbox name tipe)
171+ return (D.Address name tipe)
172172
173173
174174 _ ->
175− throw $ loopbackError name tipe $
176− [ P.text "A loopback like this must be a Mailbox." ]
175+ throw $ inputError name tipe $
176+ [ P.text "An input like this must be a Stream.Input" ]
177177
178178 Just expr ->
179179 case ST.deepDealias tipe of
@@ -187,20 +187,20 @@ loopback name maybeExpr tipe =
187187 return (D.Promise name promiseType expr tipe)
188188
189189 _ ->
190− throw $ loopbackError name tipe $
191− [ P.text "A loopback that runs promises must be a stream of results."
190+ throw $ inputError name tipe $
191+ [ P.text "An input that runs promises must be a stream of results."
192192 , P.text "Something like the following:\n"
193193 , P.text " Stream (Result Http.Error String)"
194194 , P.text " Stream (Result x ())"
195195 ]
196196
197197
198−loopbackError :: String -> ST.CanonicalType -> [Doc] -> [Doc]
199−loopbackError name tipe message =
200− [ P.text "Loopback Error:"
198+inputError :: String -> ST.CanonicalType -> [Doc] -> [Doc]
199+inputError name tipe message =
200+ [ P.text "Input Error:"
201201 , P.nest 4 $
202202 P.vcat $
203− [ P.text ("The loopback named '" ++ name ++ "' has an invalid type.\n")
203+ [ P.text ("The input named '" ++ name ++ "' has an invalid type.\n")
204204 , P.nest 4 (PP.pretty tipe) <> P.text "\n"
205205 ]
206206 ++ message
src/Transform/Declaration.hs+7−7
@@ -69,7 +69,7 @@ combineAnnotations =
6969 (:) (D.Wire (D.Input name tipe)) <$> go wireRest
7070
7171 D.OutputDefinition name _ ->
72− Left (errorMessage "output" name)
72+ Left (errorMessage "foreign output" name)
7373
7474 D.OutputAnnotation name tipe ->
7575 case wireRest of
@@ -78,10 +78,10 @@ combineAnnotations =
7878 (:) (D.Wire (D.Output name expr' tipe)) <$> go rest
7979
8080 _ ->
81− Left (errorMessage "output" name)
81+ Left (errorMessage "foreign output" name)
8282
8383 D.LoopbackDefinition name _tipe ->
84− Left (errorMessage "loopback" name)
84+ Left (errorMessage "input" name)
8585
8686 D.LoopbackAnnotation name tipe ->
8787 case wireRest of
@@ -149,10 +149,10 @@ toDefs moduleName decl =
149149 D.Input name tipe ->
150150 [ definition name (A.none $ E.Input name tipe) tipe ]
151151
152− D.Loopback loopback ->
153− case loopback of
154− D.Mailbox name tipe ->
155− [ definition name (A.none $ E.LoopbackIn name E.Mailbox) tipe ]
152+ D.Loopback input ->
153+ case input of
154+ D.Address name tipe ->
155+ [ definition name (A.none $ E.LoopbackIn name E.Address) tipe ]
156156
157157 D.Promise name promiseType expr resultType ->
158158 let dummyName =