1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
20import qualified Data.Map as Map
21import qualified Data.Maybe as Maybe
23import qualified AST.Module.Name as ModuleName
24import qualified Data.Bag as Bag
25import qualified Elm.Name as N
26import qualified Reporting.Doc as D
27import qualified Reporting.Render.Type as RT
28import qualified Reporting.Render.Type.Localizer as L
36 = Lambda Type Type [Type]
40 | FlexSuper Super N.Name
42 | RigidSuper Super N.Name
43 | Type ModuleName.Canonical N.Name [Type]
44 | Record (Map.Map N.Name Type) Extension
46 | Tuple Type Type (Maybe Type)
47 | Alias ModuleName.Canonical N.Name [(N.Name, Type)] Type
63iteratedDealias :: Type -> Type
66 Alias _ _ _ realType ->
67 iteratedDealias realType
77nameToDoc :: L.Localizer -> ModuleName.Canonical -> N.Name -> D.Doc
78nameToDoc localizer (ModuleName.Canonical _ home) name =
79 L.toDoc localizer home name
82toDoc :: L.Localizer -> RT.Context -> Type -> D.Doc
83toDoc localizer ctx tipe =
87 (toDoc localizer RT.Func a)
88 (toDoc localizer RT.Func b)
89 (map (toDoc localizer RT.Func) cs)
109 Type home name args ->
111 (nameToDoc localizer home name)
112 (map (toDoc localizer RT.App) args)
116 entryToDocs (fieldName, fieldType) =
117 ( D.fromName fieldName, toDoc localizer RT.None fieldType )
120 map entryToDocs (Map.toList fields)
122 RT.record fieldDocs $
125 FlexOpen x -> Just (D.fromName x)
126 RigidOpen x -> Just (D.fromName x)
133 (toDoc localizer RT.None a)
134 (toDoc localizer RT.None b)
135 (map (toDoc localizer RT.None) (Maybe.maybeToList maybeC))
137 Alias home name args _ ->
139 (nameToDoc localizer home name)
140 (map (toDoc localizer RT.App . snd) args)
147toDiffDocs :: L.Localizer -> Type -> Type -> (D.Doc, D.Doc, [Problem])
148toDiffDocs localizer a b =
149 case diff localizer RT.None a b of
153 Different aDoc bDoc problems ->
154 (aDoc, bDoc, Bag.toList problems)
159 | Different a a (Bag.Bag Problem)
162instance Functor Diff where
165 Similar a b -> Similar (func a) (func b)
166 Different a b ps -> Different (func a) (func b) ps
169instance Applicative Diff where
175 Similar aFunc bFunc ->
177 Similar a b -> Similar (aFunc a) (bFunc b)
178 Different a b ps -> Different (aFunc a) (bFunc b) ps
180 Different aFunc bFunc ps ->
182 Similar a b -> Different (aFunc a) (bFunc b) ps
183 Different a b ps2 -> Different (aFunc a) (bFunc b) (Bag.append ps ps2)
191 = FieldMismatch [N.Name] [N.Name]
202 | BadFlexSuper Super N.Name Type
203 | BadRigidVar N.Name Type
204 | BadRigidSuper Super N.Name Type
211diff :: L.Localizer -> RT.Context -> Type -> Type -> Diff D.Doc
212diff localizer ctx tipe1 tipe2 =
213 case (tipe1, tipe2) of
214 (FlexVar x, FlexVar y) | x == y -> pure (D.fromName x)
215 (FlexSuper _ x, FlexSuper _ y) | x == y -> pure (D.fromName x)
216 (RigidVar x, RigidVar y) | x == y -> pure (D.fromName x)
217 (RigidSuper _ x, RigidSuper _ y) | x == y -> pure (D.fromName x)
219 (Infinite, Infinite) -> pure "∞"
220 (Error, Error) -> pure "?"
221 (Unit, Unit) -> pure "()"
223 (Tuple a b maybeC, Tuple x y maybeZ) ->
225 <$> diff localizer RT.None a x
226 <*> diff localizer RT.None b y
228 case (maybeC, maybeZ) of
229 (Nothing, Nothing) -> pure []
230 (Just c , Nothing) -> Different [D.dullyellow (toDoc localizer RT.None c)] [] Bag.empty
231 (Nothing, Just z ) -> Different [] [D.dullyellow (toDoc localizer RT.None z)] Bag.empty
232 (Just c , Just z ) -> (:[]) <$> diff localizer RT.None c z
234 (Record fields1 ext1, Record fields2 ext2) -> diffRecord localizer fields1 ext1 fields2 ext2
236 (Type home1 name1 args1, Type home2 name2 args2) | home1 == home2 && name1 == name2 ->
237 RT.apply ctx (nameToDoc localizer home1 name1) <$>
238 sequenceA (zipWith (diff localizer RT.App) args1 args2)
240 (Alias home1 name1 args1 _, Alias home2 name2 args2 _) | home1 == home2 && name1 == name2 ->
241 RT.apply ctx (nameToDoc localizer home1 name1) <$>
242 sequenceA (zipWith (diff localizer RT.App) (map snd args1) (map snd args2))
244 (Alias _ _ _ alias1, other) -> diff localizer ctx alias1 other
245 (other, Alias _ _ _ alias2) -> diff localizer ctx other alias2
247 (Lambda a b cs, Lambda x y zs) -> diffLambda localizer ctx (a:b:cs) (x:y:zs)
249 (FlexVar x, other) -> Similar (D.fromName x) (toDoc localizer ctx other)
250 (other, FlexVar x) -> Similar (toDoc localizer ctx other) (D.fromName x)
254 (Type home name [t1], t2) | isMaybe home name && isSimilar t1 t2 ->
255 Different (yellowApply localizer ctx home name t1) (toDoc localizer ctx t2) (Bag.one AnythingFromMaybe)
257 (t1, Type home name [t2]) | isList home name && isSimilar t1 t2 ->
258 Different (toDoc localizer ctx t1) (yellowApply localizer ctx home name t2) (Bag.one AnythingToList)
262 doc1 = D.dullyellow (toDoc localizer ctx tipe1)
263 doc2 = D.dullyellow (toDoc localizer ctx tipe2)
265 Different doc1 doc2 $
267 (RigidVar x, other) -> Bag.one $ BadRigidVar x other
268 (FlexSuper s x, other) -> Bag.one $ BadFlexSuper s x other
269 (RigidSuper s x, other) -> Bag.one $ BadRigidSuper s x other
270 (other, RigidVar x) -> Bag.one $ BadRigidVar x other
271 (other, FlexSuper s x) -> Bag.one $ BadFlexSuper s x other
272 (other, RigidSuper s x) -> Bag.one $ BadRigidSuper s x other
274 (Type home1 name1 [], Type home2 name2 [])
275 | isInt home1 name1 && isFloat home2 name2 -> Bag.one IntFloat
276 | isFloat home1 name1 && isInt home2 name2 -> Bag.one IntFloat
277 | isInt home1 name1 && isString home2 name2 -> Bag.one StringFromInt
278 | isFloat home1 name1 && isString home2 name2 -> Bag.one StringFromFloat
279 | isString home1 name1 && isInt home2 name2 -> Bag.one StringToInt
280 | isString home1 name1 && isFloat home2 name2 -> Bag.one StringToFloat
281 | isBool home2 name2 -> Bag.one AnythingToBool
286isBool :: ModuleName.Canonical -> N.Name -> Bool
288 home == ModuleName.basics && name == N.bool
291isInt :: ModuleName.Canonical -> N.Name -> Bool
293 home == ModuleName.basics && name == N.int
296isFloat :: ModuleName.Canonical -> N.Name -> Bool
298 home == ModuleName.basics && name == N.float
301isString :: ModuleName.Canonical -> N.Name -> Bool
303 home == ModuleName.string && name == N.string
306isChar :: ModuleName.Canonical -> N.Name -> Bool
308 home == ModuleName.char && name == N.char
311isMaybe :: ModuleName.Canonical -> N.Name -> Bool
313 home == ModuleName.maybe && name == N.maybe
316isList :: ModuleName.Canonical -> N.Name -> Bool
318 home == ModuleName.list && name == N.list
321isSimilar :: Type -> Type -> Bool
322isSimilar tipe1 tipe2 =
323 case diff L.empty RT.None tipe1 tipe2 of
325 Different _ _ _ -> False
328yellowApply :: L.Localizer -> RT.Context -> ModuleName.Canonical -> N.Name -> Type -> D.Doc
329yellowApply localizer ctx home name tipe =
331 (D.dullyellow (nameToDoc localizer home name))
332 [toDoc localizer RT.App tipe]
340-- INVARIANT: length types1 >= 2 && length types2 >= 2
342diffLambda :: L.Localizer -> RT.Context -> [Type] -> [Type] -> Diff D.Doc
343diffLambda localizer ctx types1 types2 =
345 (result1:revArgs1) = reverse types1
346 (result2:revArgs2) = reverse types2
348 numArgs1 = length revArgs1
349 numArgs2 = length revArgs2
351 resultDiff = diff localizer RT.Func result1 result2
354 Similar resultDoc1 resultDoc2 ->
355 if numArgs1 == numArgs2 then
356 (\resultDoc revArgsDoc ->
357 let (x:y:zs) = reverse (resultDoc:revArgsDoc) in
361 <*> sequenceA (zipWith (diff localizer RT.Func) revArgs1 revArgs2)
363 else if numArgs1 < numArgs2 then
364 diffArgMismatch localizer ctx revArgs1 resultDoc1 revArgs2 resultDoc2
367 diffArgMismatch localizer ctx revArgs2 resultDoc2 revArgs1 resultDoc1
369 Different resultDoc1 resultDoc2 problems ->
371 (x1:y1:zs1) = reverse (resultDoc1 : map (toDoc localizer RT.Func) revArgs1)
372 (x2:y2:zs2) = reverse (resultDoc2 : map (toDoc localizer RT.Func) revArgs2)
375 (RT.lambda ctx x1 y1 zs1)
376 (RT.lambda ctx x2 y2 zs2)
377 (Bag.append (Bag.one ReturnMismatch) problems)
381-- INVARIANT: length shortRevArgs >= 2 && length longRevArgs >= 2
383diffArgMismatch :: L.Localizer -> RT.Context -> [Type] -> D.Doc -> [Type] -> D.Doc -> Diff D.Doc
384diffArgMismatch localizer ctx shortRevArgs shortResult longRevArgs longResult =
385 case toGreedyMatch localizer shortRevArgs longRevArgs of
386 Just (GreedyMatch shortRevArgDocs longRevArgDocs) ->
388 (a:b:cs) = reverse (shortResult:shortRevArgDocs)
389 (x:y:zs) = reverse (longResult:longRevArgDocs)
392 (RT.lambda ctx a b cs)
393 (RT.lambda ctx x y zs)
394 (Bag.one (MissingArgs (length longRevArgs - length shortRevArgs)))
397 case toGreedyMatch localizer (reverse shortRevArgs) (reverse longRevArgs) of
398 Just (GreedyMatch shortArgDocs longArgDocs) ->
400 (a:b:cs) = shortArgDocs ++ [shortResult]
401 (x:y:zs) = longArgDocs ++ [longResult ]
404 (RT.lambda ctx a b cs)
405 (RT.lambda ctx x y zs)
406 (Bag.one (MissingArgs (length longRevArgs - length shortRevArgs)))
411 D.dullyellow (toDoc localizer RT.Func tipe)
413 (a:b:cs) = reverse (shortResult : map toYellowDoc shortRevArgs)
414 (x:y:zs) = reverse (longResult : map toYellowDoc longRevArgs )
417 (RT.lambda ctx a b cs)
418 (RT.lambda ctx x y zs)
419 (Bag.one (MissingArgs (length longRevArgs - length shortRevArgs)))
427 GreedyMatch [D.Doc] [D.Doc]
431-- INVARIANT: length shorterArgs < length longerArgs
433toGreedyMatch :: L.Localizer -> [Type] -> [Type] -> Maybe GreedyMatch
434toGreedyMatch localizer shorterArgs longerArgs =
435 toGreedyMatchHelp localizer shorterArgs longerArgs (GreedyMatch [] [])
438toGreedyMatchHelp :: L.Localizer -> [Type] -> [Type] -> GreedyMatch -> Maybe GreedyMatch
439toGreedyMatchHelp localizer shorterArgs longerArgs match@(GreedyMatch shorterDocs longerDocs) =
442 D.dullyellow (toDoc localizer RT.Func tipe)
444 case (shorterArgs, longerArgs) of
446 case diff localizer RT.Func x y of
448 toGreedyMatchHelp localizer xs ys $
449 GreedyMatch (a:shorterDocs) (b:longerDocs)
452 toGreedyMatchHelp localizer shorterArgs ys $
453 GreedyMatch shorterDocs (toYellowDoc y : longerDocs)
459 Just (GreedyMatch shorterDocs (map toYellowDoc longerArgs))
469diffRecord :: L.Localizer -> Map.Map N.Name Type -> Extension -> Map.Map N.Name Type -> Extension -> Diff D.Doc
470diffRecord localizer fields1 ext1 fields2 ext2 =
472 only1 = Map.keys (Map.difference fields1 fields2)
473 only2 = Map.keys (Map.difference fields2 fields1)
475 if null only1 && null only2 then
476 case findBadOverlaps localizer fields1 fields2 of
478 case diffExt ext1 ext2 of
480 Similar (toBoringRecord fields1 ext1) (toBoringRecord fields2 ext2)
482 Different dExt1 dExt2 problems ->
483 Different (RT.record [] dExt1) (RT.record [] dExt2) problems
485 overlaps@(bad:bads) ->
486 if length overlaps < Map.size fields1 then
488 (RT.recordSnippet (toOverlapDoc _doc1 bad) (map (toOverlapDoc _doc1) bads))
489 (RT.recordSnippet (toOverlapDoc _doc2 bad) (map (toOverlapDoc _doc2) bads))
490 (foldr Bag.append Bag.empty (map _problems overlaps))
493 (toOverlapRecord _doc1 bad bads ext1)
494 (toOverlapRecord _doc2 bad bads ext2)
495 (foldr Bag.append Bag.empty (map _problems overlaps))
499 (toMissingDoc fields1 only1 ext1)
500 (toMissingDoc fields2 only2 ext2)
501 (Bag.one (FieldMismatch only1 only2))
504toOverlapDoc :: (BadOverlap -> D.Doc) -> BadOverlap -> (D.Doc, D.Doc)
505toOverlapDoc getDoc overlap@(BadOverlap field _ _ _) =
506 (D.fromName field, getDoc overlap)
509toOverlapRecord :: (BadOverlap -> D.Doc) -> BadOverlap -> [BadOverlap] -> Extension -> D.Doc
510toOverlapRecord getDoc bad bads ext =
511 let go = toOverlapDoc getDoc in
513 Closed -> RT.record (map go (bad:bads)) Nothing
514 FlexOpen _ -> RT.recordSnippet (go bad) (map go bads)
515 RigidOpen _ -> RT.recordSnippet (go bad) (map go bads)
518toMissingDoc :: Map.Map N.Name t -> [N.Name] -> Extension -> D.Doc
519toMissingDoc allFields uniqueFields ext =
520 case map emphasizeFieldName uniqueFields of
522 toBoringRecord allFields ext
525 if length uniqueFields < Map.size allFields then
526 RT.recordSnippet doc docs
529 Closed -> RT.record (doc:docs) Nothing
530 FlexOpen _ -> RT.recordSnippet doc docs
531 RigidOpen _ -> RT.recordSnippet doc docs
534emphasizeFieldName :: N.Name -> (D.Doc, D.Doc)
535emphasizeFieldName field =
536 ( D.dullyellow (D.fromName field), "..." )
539toBoringRecord :: Map.Map N.Name t -> Extension -> D.Doc
540toBoringRecord fields ext =
542 Closed -> if Map.null fields then "{}" else "{ ... }"
543 FlexOpen _ -> "{ ... }"
544 RigidOpen _ -> "{ ... }"
548-- DIFF RECORD EXTENSION
551diffExt :: Extension -> Extension -> Diff (Maybe D.Doc)
554 normal = Just . D.fromName
555 yellow = Just . D.dullyellow . D.fromName
557 different x y = Different x y Bag.empty
562 Closed -> Similar Nothing Nothing
563 FlexOpen y -> different Nothing (yellow y)
564 RigidOpen y -> different Nothing (yellow y)
568 Closed -> different (yellow x) Nothing
569 FlexOpen y -> Similar (normal x) (normal y)
570 RigidOpen y -> Similar (normal x) (normal y)
574 Closed -> different (yellow x) Nothing
575 FlexOpen y -> Similar (normal x) (normal y)
578 Similar (normal x) (normal y)
580 Different (yellow x) (yellow y) (Bag.one (BadRigidVar x (RigidVar y)))
592 , _problems :: Bag.Bag Problem
596findBadOverlaps :: L.Localizer -> Map.Map N.Name Type -> Map.Map N.Name Type -> [BadOverlap]
597findBadOverlaps localizer fields1 fields2 =
598 findBadOverlapsHelp [] $ Map.toList $
599 Map.intersectionWith (diff localizer RT.None) fields1 fields2
602findBadOverlapsHelp :: [BadOverlap] -> [(N.Name, Diff D.Doc)] -> [BadOverlap]
603findBadOverlapsHelp badOverlaps fieldPairs =
608 (_, Similar _ _) : otherFieldPairs ->
609 findBadOverlapsHelp badOverlaps otherFieldPairs
611 (field, Different aDoc bDoc problems) : otherFieldPairs ->
612 findBadOverlapsHelp (BadOverlap field aDoc bDoc problems : badOverlaps) otherFieldPairs
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