1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Elm.Compiler.Type
17import Data.Text (Text)
18import qualified Data.Text as Text
19import qualified Data.Text.Encoding as Text
20import qualified Text.PrettyPrint as P
21import Text.PrettyPrint ((<+>))
23import qualified AST.Source as Src
24import qualified Elm.Name as N
25import qualified Parse.Primitives 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 [(N.Name, Type)] (Maybe Type)
43 | Tuple Type Type [Type]
54data Alias = Alias N.Name [N.Name] Type
55data Union = Union N.Name [N.Name] [(N.Name, [Type])]
62data Format = OneLine | MultiLine
65toString :: Format -> Type -> String
75 P.renderStyle (P.Style mode 80 1.0) (toDoc None tipe)
78data Context = None | InType | InFunction
81toDoc :: Context -> Type -> P.Doc
86 map (toDoc InFunction) (collectLambdas tipe)
89 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
96 P.text (Text.unpack name)
104 [ "(" <+> toDoc None a
105 , "," <+> toDoc None b
107 ++ map ("," <+>) (map (toDoc None) cs)
114 P.text (Text.unpack name)
120 (P.text (Text.unpack name))
122 (P.sep $ map (toDoc InType) args)
132 case flattenRecord tipe of
138 [ P.cat (zipWith (<+>) ("{" : repeat ",") (map prettyField fields))
144 ("{" <+> P.text (Text.unpack x) <+> "|")
147 [ P.sep (P.punctuate "," (map prettyField fields))
152 prettyField (field, fieldType) =
153 P.text (Text.unpack field) <+> ":" <+> toDoc None fieldType
156collectLambdas :: Type -> [Type]
159 Lambda arg body -> arg : collectLambdas body
163flattenRecord :: Type -> ( [(Text, Type)], Maybe Text )
169 Record fields Nothing ->
172 Record fields (Just ext) ->
173 let (fields',ext') = flattenRecord ext
175 (fields' ++ fields, ext')
178 error "Trying to flatten ill-formed record."
185encode :: Type -> Encode.Value
187 Encode.text (Text.pack (toString OneLine tipe))
190decoder :: Decode.Decoder Type
192 do txt <- Decode.text
193 case Parse.run Type.expression (Text.encodeUtf8 (Text.replace "'" "_" txt)) of
195 Decode.fail "Expecting a type, like (String -> Int)"
197 Right (tipe, _, _) ->
198 Decode.succeed (fromRawType tipe)
201fromRawType :: Src.Type -> Type
202fromRawType (A.At _ astType) =
205 Lambda (fromRawType t1) (fromRawType t2)
219 Src.TType _ _ name args ->
220 Type name (map fromRawType args)
222 Src.TRecord fields ext ->
223 let fromField (A.At _ field, tipe) = (field, fromRawType tipe) in
225 (map fromField fields)
226 (fmap fromRawType ext)
233encodeMetadata :: DebugMetadata -> Encode.Value
234encodeMetadata (DebugMetadata msg aliases unions) =
236 [ "message" ==> encode msg
237 , "aliases" ==> Encode.object (map toAliasField aliases)
238 , "unions" ==> Encode.object (map toUnionField unions)
242toAliasField :: Alias -> ( String, Encode.Value )
243toAliasField (Alias name args tipe) =
246 [ "args" ==> Encode.list Encode.text args
247 , "type" ==> encode tipe
251toUnionField :: Union -> ( String, Encode.Value )
252toUnionField (Union name args constructors) =
255 [ "args" ==> Encode.list Encode.text args
256 , "tags" ==> Encode.object (map toCtorObject constructors)
260toCtorObject :: (Text, [Type]) -> ( String, Encode.Value )
261toCtorObject (name, args) =
262 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