1{-# LANGUAGE OverloadedStrings #-}
2module Elm.Compiler.Type
7import Control.Applicative ((<$>), (<*>))
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
29 | Record [(String, Type)] (Maybe Type)
34data Context = None | ADT | Function
37toString :: Type -> String
39 P.render (toDoc None tipe)
42toDoc :: Context -> Type -> P.Doc
47 map (toDoc Function) (collectLambdas tipe)
50 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
60 P.text (if name == "_Tuple0" then "()" else name)
63 | Help.isTuple name ->
65 [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
70 let adt = P.hang (P.text name) 2 (P.sep $ map (toDoc ADT) args)
72 case (context, args) of
73 (ADT, _ : _) -> P.parens adt
78 "Somehow ended up with an unexpected type, please post a minimal example that\n"
79 ++ "reproduces this error to <https://github.com/elm-lang/elm-compiler/issues>"
82 case flattenRecord tipe of
88 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
94 (P.lbrace <+> P.text x <+> P.text "|")
97 [ P.sep (P.punctuate P.comma (map prettyField fields))
102 prettyField (field, tipe) =
103 P.text field <+> P.text ":" <+> toDoc None tipe
106collectLambdas :: Type -> [Type]
109 Lambda arg body -> arg : collectLambdas body
113flattenRecord :: Type -> ( [(String, Type)], Maybe String )
116 Var x -> ([], Just x)
118 Record fields Nothing -> (fields, Nothing)
120 Record fields (Just ext) ->
121 let (fields',ext') = flattenRecord ext
123 (fields' ++ fields, ext')
125 _ -> error "Trying to flatten ill-formed record."
130instance Json.ToJSON Type where
132 Json.String (Text.unwords (Text.words (Text.pack (toString tipe))))
135instance Json.FromJSON Type where
140 "Trying to decode a type string, but could not handle this value:\n"
141 ++ BS.unpack (Json.encode value)
145 either failure (return . fromRawType)
146 (Parse.iParse Type.expr (Text.unpack text))
155fromObject :: Json.Object -> Json.Parser Type
157 do tag <- obj .: "tag"
158 case (tag :: String) of
160 Lambda <$> obj .: "in" <*> obj .: "out"
163 Var <$> obj .: "name"
166 Type <$> obj .: "name"
169 App <$> obj .: "func" <*> obj .: "args"
172 Record <$> obj .: "fields" <*> obj .: "extension"
175 fail $ "Error when decoding type with tag: " ++ tag
178fromRawType :: Type.Raw -> Type
179fromRawType (A.A _ astType) =
181 Type.RLambda t1 t2 ->
182 Lambda (fromRawType t1) (fromRawType t2)
188 Type (Var.toString var)
191 App (fromRawType t) (map fromRawType ts)
193 Type.RRecord fields ext ->
194 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