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)
36 , _aliases :: [( String, [String], Type )]
37 , _unions :: [( String, [String], [(String, [Type])] )]
45data Context = None | ADT | Function
48toString :: Type -> String
50 P.render (toDoc None tipe)
53toDoc :: Context -> Type -> P.Doc
58 map (toDoc Function) (collectLambdas tipe)
61 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
71 P.text (if name == "_Tuple0" then "()" else name)
74 | name == "_Tuple0" ->
77 | Help.isTuple name ->
79 [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
84 let adt = P.hang (P.text name) 2 (P.sep $ map (toDoc ADT) args)
86 case (context, args) of
87 (ADT, _ : _) -> P.parens adt
92 "Somehow ended up with an unexpected type, please post a minimal example that\n"
93 ++ "reproduces this error to <https://github.com/elm-lang/elm-compiler/issues>"
96 case flattenRecord tipe of
102 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
108 (P.lbrace <+> P.text x <+> P.text "|")
111 [ P.sep (P.punctuate P.comma (map prettyField fields))
116 prettyField (field, tipe) =
117 P.text field <+> P.text ":" <+> toDoc None tipe
120collectLambdas :: Type -> [Type]
123 Lambda arg body -> arg : collectLambdas body
127flattenRecord :: Type -> ( [(String, Type)], Maybe String )
130 Var x -> ([], Just x)
132 Record fields Nothing -> (fields, Nothing)
134 Record fields (Just ext) ->
135 let (fields',ext') = flattenRecord ext
137 (fields' ++ fields, ext')
139 _ -> error "Trying to flatten ill-formed record."
146instance Json.ToJSON Type where
148 Json.String (Text.unwords (Text.words (Text.pack (toString tipe))))
151instance Json.FromJSON Type where
156 "Trying to decode a type string, but could not handle this value:\n"
157 ++ BS.unpack (Json.encode value)
161 either failure (return . fromRawType)
162 (Parse.iParse Type.expr (Text.unpack text))
171fromObject :: Json.Object -> Json.Parser Type
173 do tag <- obj .: "tag"
174 case (tag :: String) of
176 Lambda <$> obj .: "in" <*> obj .: "out"
179 Var <$> obj .: "name"
182 Type <$> obj .: "name"
185 App <$> obj .: "func" <*> obj .: "args"
188 Record <$> obj .: "fields" <*> obj .: "extension"
191 fail $ "Error when decoding type with tag: " ++ tag
194fromRawType :: Type.Raw -> Type
195fromRawType (A.A _ astType) =
197 Type.RLambda t1 t2 ->
198 Lambda (fromRawType t1) (fromRawType t2)
204 Type (Var.toString var)
207 App (fromRawType t) (map fromRawType ts)
209 Type.RRecord fields ext ->
210 Record (map (second fromRawType) fields) (fmap fromRawType ext)
217instance Json.ToJSON Program where
218 toJSON (Program msg aliases unions) =
221 , "aliases" .= Json.object (map toAliasField aliases)
222 , "unions" .= Json.object (map toUnionField unions)
226toAliasField :: ( String, [String], Type ) -> Json.Pair
227toAliasField ( name, args, tipe ) =
228 Text.pack name .= Json.object
234toUnionField :: ( String, [String], [(String, [Type])] ) -> Json.Pair
235toUnionField ( name, args, constructors ) =
236 Text.pack name .= Json.object
238 , "tags" .= Json.object (map toCtorObject constructors)
242toCtorObject :: (String, [Type]) -> Json.Pair
243toCtorObject (name, args) =
244 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