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.ByteString.Lazy.Char8 as BS
12import qualified Data.Text as Text
13import Text.PrettyPrint as P
15import qualified AST.Helpers as Help
23 | Record [(String, Type)] (Maybe Type)
28data Context = None | ADT | Function
31toString :: Type -> String
33 P.render (toDoc None tipe)
36toDoc :: Context -> Type -> P.Doc
41 map (toDoc Function) (collectLambdas tipe)
44 P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
54 P.text (if name == "_Tuple0" then "()" else name)
57 | Help.isTuple name ->
59 [ P.cat (zipWith (<+>) (P.lparen : repeat P.comma) (map (toDoc None) args))
64 let adt = P.hang (P.text name) 2 (P.sep $ map (toDoc ADT) args)
66 case (context, args) of
67 (ADT, _ : _) -> P.parens adt
72 "Somehow ended up with an unexpected type, please post a minimal example that\n"
73 ++ "reproduces this error to <https://github.com/elm-lang/elm-compiler/issues>"
76 case flattenRecord tipe of
82 [ P.cat (zipWith (<+>) (P.lbrace : repeat P.comma) (map prettyField fields))
88 (P.lbrace <+> P.text x <+> P.text "|")
91 [ P.cat (zipWith (<+>) (P.space : repeat P.comma) (map prettyField fields))
95 prettyField (field, tipe) =
96 P.text field <+> P.text ":" <+> toDoc None tipe
99collectLambdas :: Type -> [Type]
102 Lambda arg body -> arg : collectLambdas body
106flattenRecord :: Type -> ( [(String, Type)], Maybe String )
109 Var x -> ([], Just x)
111 Record fields Nothing -> (fields, Nothing)
113 Record fields (Just ext) ->
114 let (fields',ext') = flattenRecord ext
116 (fields' ++ fields, ext')
118 _ -> error "Trying to flatten ill-formed record."
123instance Json.ToJSON Type where
125 Json.object (getFields tipe)
130 [ "tag" .= ("lambda" :: Text.Text)
131 , "in" .= Json.toJSON t1
132 , "out" .= Json.toJSON t2
136 [ "tag" .= ("var" :: Text.Text)
137 , "name" .= Json.toJSON x
141 [ "tag" .= ("type" :: Text.Text)
142 , "name" .= Json.toJSON name
146 [ "tag" .= ("app" :: Text.Text)
147 , "func" .= Json.toJSON t
148 , "args" .= Json.toJSON ts
152 [ "tag" .= ("record" :: Text.Text)
153 , "fields" .= Json.toJSON (map (Json.toJSON . second Json.toJSON) fields)
154 , "extension" .= Json.toJSON ext
158instance Json.FromJSON Type where
159 parseJSON (Json.Object obj) =
160 do tag <- obj .: "tag"
161 case (tag :: String) of
163 Lambda <$> obj .: "in" <*> obj .: "out"
166 Var <$> obj .: "name"
169 Type <$> obj .: "name"
172 App <$> obj .: "func" <*> obj .: "args"
175 Record <$> obj .: "fields" <*> obj .: "extension"
178 fail $ "Error when decoding type with tag: " ++ tag
182 fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)
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