1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
14import Control.Arrow ((***))
15import Data.Aeson ((.:))
16import qualified Data.Aeson as Aeson
17import qualified Data.Text as Text
18import Data.Text (Text)
19import qualified Text.PrettyPrint as P
20import Text.PrettyPrint ((<+>))
22import qualified AST.Helpers as Help
23import qualified AST.Type as Type
24import qualified AST.Variable as Var
25import qualified Parse.Helpers as Parse
26import qualified Parse.Type as Type
27import qualified Reporting.Annotation as A
28import qualified Json.Decode as Decode
29import qualified Json.Encode as Encode
30import Json.Encode ((==>))
41 | Record [(Text, Type)] (Maybe Type)
47 , _aliases :: [( Text, [Text], Type )]
48 , _unions :: [( Text, [Text], [(Text, [Type])] )]
56data Format = OneLine | MultiLine
59toString :: Format -> Type -> String
69 P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
72data Context = None | InType | InFunction
75toDoc :: Context -> Type -> P.Doc
80 map (toDoc InFunction) (collectLambdas tipe)
83 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
90 P.text (Text.unpack name)
93 P.text (if name == Help.zeroTuple then "()" else Text.unpack name)
95 Type name arguments@(arg:args) ->
96 if Help.isTuple name then
98 [ P.cat ("(" <+> toDoc None arg : map (\a -> "," <+> toDoc None a) args)
106 (P.text (Text.unpack name))
108 (P.sep $ map (toDoc InType) arguments)
118 case flattenRecord tipe of
124 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map prettyField fields))
130 ("{" <+> P.text (Text.unpack x) <+> "|")
133 [ P.sep (P.punctuate "," (map prettyField fields))
138 prettyField (field, fieldType) =
139 P.text (Text.unpack field) <+> ":" <+> toDoc None fieldType
142collectLambdas :: Type -> [Type]
145 Lambda arg body -> arg : collectLambdas body
149flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
155 Record fields Nothing ->
158 Record fields (Just ext) ->
159 let (fields',ext') = flattenRecord ext
161 (fields' ++ fields, ext')
164 error "Trying to flatten ill-formed record."
171encode :: Type -> Encode.Value
173 Encode.text (Text.pack (toString OneLine tipe))
176decoder :: Decode.Decoder Type
180 do txt <- Decode.text
181 case Parse.run Type.expression (Text.replace "'" "_" txt) of
183 Decode.fail "Expecting a type, like (String -> Int)"
185 Right (tipe, _, _) ->
186 Decode.succeed (fromRawType tipe)
192instance Aeson.FromJSON Type where
194 Aeson.withObject "Type" $ \object ->
195 do tag <- object .: "tag"
196 case (tag :: String) of
198 Lambda <$> object .: "in" <*> object .: "out"
201 Var <$> object .: "name"
204 do name <- object .: "name"
205 return (Type name [])
208 do func <- object .: "func"
209 args <- object .: "args"
212 return (Type name args)
215 fail "unexpected type application"
218 Record <$> object .: "fields" <*> object .: "extension"
221 fail $ "Error when decoding type with tag: " ++ tag
224fromRawType :: Type.Raw -> Type
225fromRawType (A.A _ astType) =
227 Type.RLambda t1 t2 ->
228 Lambda (fromRawType t1) (fromRawType t2)
233 Type.RType (A.A _ name) args ->
234 Type (Var.rawToText name) (map fromRawType args)
236 Type.RRecord fields ext ->
237 Record (map (A.drop *** fromRawType) fields) (fmap fromRawType ext)
244encodeProgram :: Program -> Encode.Value
245encodeProgram (Program msg aliases unions) =
247 [ "message" ==> encode msg
248 , "aliases" ==> Encode.object (map toAliasField aliases)
249 , "unions" ==> Encode.object (map toUnionField unions)
253toAliasField :: ( Text, [Text], Type ) -> ( String, Encode.Value )
254toAliasField ( name, args, tipe ) =
257 [ "args" ==> Encode.list Encode.text args
258 , "type" ==> encode tipe
262toUnionField :: ( Text, [Text], [(Text, [Type])] ) -> ( String, Encode.Value )
263toUnionField ( name, args, constructors ) =
266 [ "args" ==> Encode.list Encode.text args
267 , "tags" ==> Encode.object (map toCtorObject constructors)
271toCtorObject :: (Text, [Type]) -> ( String, Encode.Value )
272toCtorObject (name, args) =
273 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