1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
12import Control.Arrow ((***))
13import qualified Data.Text as Text
14import Data.Text (Text)
15import Text.PrettyPrint as P
17import qualified AST.Helpers as Help
18import qualified AST.Type as Type
19import qualified AST.Variable as Var
20import qualified Parse.Helpers as Parse
21import qualified Parse.Type as Type
22import qualified Reporting.Annotation as A
23import qualified Json.Decode as Decode
24import qualified Json.Encode as Encode
25import Json.Encode ((==>))
37 | Record [(Text, Type)] (Maybe Type)
43 , _aliases :: [( Text, [Text], Type )]
44 , _unions :: [( Text, [Text], [(Text, [Type])] )]
52data Context = None | ADT | Function
55toString :: Type -> String
57 P.render (toDoc None tipe)
60toDoc :: Context -> Type -> P.Doc
65 map (toDoc Function) (collectLambdas tipe)
68 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
75 P.text (Text.unpack name)
78 P.text (Text.unpack (if name == "_Tuple0" then "()" else name))
80 App (Type name) args ->
81 if name == "_Tuple0" then
84 else if Help.isTuple name then
86 [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
94 (P.text (Text.unpack name))
96 (P.sep $ map (toDoc ADT) args)
98 case (context, args) of
99 (ADT, _ : _) -> P.parens application
104 "Somehow ended up with an unexpected type, please post a minimal example that\n"
105 ++ "reproduces this error to <https://github.com/elm-lang/elm-compiler/issues>"
108 case flattenRecord tipe of
114 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
120 (P.lbrace <+> P.text (Text.unpack x) <+> P.text "|")
123 [ P.sep (P.punctuate P.comma (map prettyField fields))
128 prettyField (field, fieldType) =
129 P.text (Text.unpack field) <+> P.text ":" <+> toDoc None fieldType
132collectLambdas :: Type -> [Type]
135 Lambda arg body -> arg : collectLambdas body
139flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
145 Record fields Nothing ->
148 Record fields (Just ext) ->
149 let (fields',ext') = flattenRecord ext
151 (fields' ++ fields, ext')
154 error "Trying to flatten ill-formed record."
161encode :: Type -> Encode.Value
163 Encode.text (Text.unwords (Text.words (Text.pack (toString tipe))))
166decoder :: Decode.Decoder Type
168 do txt <- Decode.text
169 case Parse.run Type.expression txt of
171 Decode.fail "a type, like (String -> Int)"
173 Right (tipe, _, _) ->
174 Decode.succeed (fromRawType tipe)
177fromRawType :: Type.Raw -> Type
178fromRawType (A.A _ astType) =
180 Type.RLambda t1 t2 ->
181 Lambda (fromRawType t1) (fromRawType t2)
187 Type (Var.rawToText var)
190 App (fromRawType t) (map fromRawType ts)
192 Type.RRecord fields ext ->
193 Record (map (A.drop *** fromRawType) fields) (fmap fromRawType ext)
200encodeProgram :: Program -> Encode.Value
201encodeProgram (Program msg aliases unions) =
203 [ "message" ==> encode msg
204 , "aliases" ==> Encode.object (map toAliasField aliases)
205 , "unions" ==> Encode.object (map toUnionField unions)
209toAliasField :: ( Text, [Text], Type ) -> ( String, Encode.Value )
210toAliasField ( name, args, tipe ) =
213 [ "args" ==> Encode.list Encode.text args
214 , "type" ==> encode tipe
218toUnionField :: ( Text, [Text], [(Text, [Type])] ) -> ( String, Encode.Value )
219toUnionField ( name, args, constructors ) =
222 [ "args" ==> Encode.list Encode.text args
223 , "tags" ==> Encode.object (map toCtorObject constructors)
227toCtorObject :: (Text, [Type]) -> ( String, Encode.Value )
228toCtorObject (name, args) =
229 Text.unpack name ==> Encode.list encode args
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list