1{-# LANGUAGE OverloadedStrings #-}
2module Elm.Compiler.Type
8import Control.Arrow (second)
9import Data.Aeson ((.:), (.=))
10import qualified Data.Aeson as Json
11import qualified Data.Aeson.Types as Json
12import qualified Data.ByteString.Lazy.Char8 as BS
13import qualified Data.Text as Text
14import Text.PrettyPrint as P
16import qualified AST.Helpers as Help
17import qualified AST.Type as Type
18import qualified AST.Variable as Var
19import qualified Parse.Helpers as Parse
20import qualified Parse.Type as Type
21import qualified Reporting.Annotation as A
30 | Record [(String, Type)] (Maybe Type)
37 , _aliases :: [( String, [String], Type )]
38 , _unions :: [( String, [String], [(String, [Type])] )]
46data Context = None | ADT | Function
49toString :: Type -> String
51 P.render (toDoc None tipe)
54toDoc :: Context -> Type -> P.Doc
59 map (toDoc Function) (collectLambdas tipe)
62 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
72 P.text (if name == "_Tuple0" then "()" else name)
75 | name == "_Tuple0" ->
78 | Help.isTuple name ->
80 [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
85 let adt = P.hang (P.text name) 2 (P.sep $ map (toDoc ADT) args)
87 case (context, args) of
88 (ADT, _ : _) -> P.parens adt
93 "Somehow ended up with an unexpected type, please post a minimal example that\n"
94 ++ "reproduces this error to <https://github.com/elm-lang/elm-compiler/issues>"
97 case flattenRecord tipe of
103 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
109 (P.lbrace <+> P.text x <+> P.text "|")
112 [ P.sep (P.punctuate P.comma (map prettyField fields))
117 prettyField (field, tipe) =
118 P.text field <+> P.text ":" <+> toDoc None tipe
121collectLambdas :: Type -> [Type]
124 Lambda arg body -> arg : collectLambdas body
128flattenRecord :: Type -> ( [(String, Type)], Maybe String )
131 Var x -> ([], Just x)
133 Record fields Nothing -> (fields, Nothing)
135 Record fields (Just ext) ->
136 let (fields',ext') = flattenRecord ext
138 (fields' ++ fields, ext')
140 _ -> error "Trying to flatten ill-formed record."
147instance Json.ToJSON Type where
149 Json.String (Text.unwords (Text.words (Text.pack (toString tipe))))
152instance Json.FromJSON Type where
157 "Trying to decode a type string, but could not handle this value:\n"
158 ++ BS.unpack (Json.encode value)
162 either failure (return . fromRawType)
163 (Parse.iParse Type.expr (Text.unpack text))
172fromObject :: Json.Object -> Json.Parser Type
174 do tag <- obj .: "tag"
175 case (tag :: String) of
177 Lambda <$> obj .: "in" <*> obj .: "out"
180 Var <$> obj .: "name"
183 Type <$> obj .: "name"
186 App <$> obj .: "func" <*> obj .: "args"
189 Record <$> obj .: "fields" <*> obj .: "extension"
192 fail $ "Error when decoding type with tag: " ++ tag
195fromRawType :: Type.Raw -> Type
196fromRawType (A.A _ astType) =
198 Type.RLambda t1 t2 ->
199 Lambda (fromRawType t1) (fromRawType t2)
205 Type (Var.toString var)
208 App (fromRawType t) (map fromRawType ts)
210 Type.RRecord fields ext ->
211 Record (map (second fromRawType) fields) (fmap fromRawType ext)
218instance Json.ToJSON Program where
219 toJSON (Program model msg aliases unions) =
223 , "aliases" .= Json.object (map toAliasField aliases)
224 , "unions" .= Json.object (map toUnionField unions)
228toAliasField :: ( String, [String], Type ) -> Json.Pair
229toAliasField ( name, args, tipe ) =
230 Text.pack name .= Json.object
236toUnionField :: ( String, [String], [(String, [Type])] ) -> Json.Pair
237toUnionField ( name, args, constructors ) =
238 Text.pack name .= Json.object
240 , "type" .= Json.object (map toCtorObject constructors)
244toCtorObject :: (String, [Type]) -> Json.Pair
245toCtorObject (name, args) =
246 Text.pack name .= 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