1{-# LANGUAGE OverloadedStrings #-}
2module Elm.Compiler.Type
7import Control.Arrow (second)
8import Data.Aeson ((.:))
9import qualified Data.Aeson as Json
10import qualified Data.Aeson.Types as Json
11import qualified Data.ByteString.Lazy.Char8 as BS
12import qualified Data.Text as Text
13import Text.PrettyPrint as P
15import qualified AST.Helpers as Help
16import qualified AST.Type as Type
17import qualified AST.Variable as Var
18import qualified Parse.Helpers as Parse
19import qualified Parse.Type as Type
20import qualified Reporting.Annotation as A
28 | Record [(String, Type)] (Maybe Type)
33data Context = None | ADT | Function
36toString :: Type -> String
38 P.render (toDoc None tipe)
41toDoc :: Context -> Type -> P.Doc
46 map (toDoc Function) (collectLambdas tipe)
49 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
59 P.text (if name == "_Tuple0" then "()" else name)
62 | name == "_Tuple0" ->
65 | Help.isTuple name ->
67 [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
72 let adt = P.hang (P.text name) 2 (P.sep $ map (toDoc ADT) args)
74 case (context, args) of
75 (ADT, _ : _) -> P.parens adt
80 "Somehow ended up with an unexpected type, please post a minimal example that\n"
81 ++ "reproduces this error to <https://github.com/elm-lang/elm-compiler/issues>"
84 case flattenRecord tipe of
90 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
96 (P.lbrace <+> P.text x <+> P.text "|")
99 [ P.sep (P.punctuate P.comma (map prettyField fields))
104 prettyField (field, tipe) =
105 P.text field <+> P.text ":" <+> toDoc None tipe
108collectLambdas :: Type -> [Type]
111 Lambda arg body -> arg : collectLambdas body
115flattenRecord :: Type -> ( [(String, Type)], Maybe String )
118 Var x -> ([], Just x)
120 Record fields Nothing -> (fields, Nothing)
122 Record fields (Just ext) ->
123 let (fields',ext') = flattenRecord ext
125 (fields' ++ fields, ext')
127 _ -> error "Trying to flatten ill-formed record."
132instance Json.ToJSON Type where
134 Json.String (Text.unwords (Text.words (Text.pack (toString tipe))))
137instance Json.FromJSON Type where
142 "Trying to decode a type string, but could not handle this value:\n"
143 ++ BS.unpack (Json.encode value)
147 either failure (return . fromRawType)
148 (Parse.iParse Type.expr (Text.unpack text))
157fromObject :: Json.Object -> Json.Parser Type
159 do tag <- obj .: "tag"
160 case (tag :: String) of
162 Lambda <$> obj .: "in" <*> obj .: "out"
165 Var <$> obj .: "name"
168 Type <$> obj .: "name"
171 App <$> obj .: "func" <*> obj .: "args"
174 Record <$> obj .: "fields" <*> obj .: "extension"
177 fail $ "Error when decoding type with tag: " ++ tag
180fromRawType :: Type.Raw -> Type
181fromRawType (A.A _ astType) =
183 Type.RLambda t1 t2 ->
184 Lambda (fromRawType t1) (fromRawType t2)
190 Type (Var.toString var)
193 App (fromRawType t) (map fromRawType ts)
195 Type.RRecord fields ext ->
196 Record (map (second fromRawType) fields) (fmap fromRawType ext)
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