@@ -61,11 +61,11 @@ realias (aliases,localize) tipe = localize (canonicalRealias aliases tipe)
61canonicalRealias :: [(String,[String],Type)] -> Type -> Type
61canonicalRealias :: [(String,[String],Type)] -> Type -> Type
62canonicalRealias aliases tipe =
62canonicalRealias aliases tipe =
63 case concatMap tryRealias aliases of
63 case concatMap tryRealias aliases of
65− _ -> if tipe == tipe' then tipe else f tipe'
64+ [] -> if tipe == tipe' then tipe else f tipe'
65+ tipes -> f (bestType tipes)
67− tryRealias (name, args, t) =
67+ tryRealias (name, args, aliasTipe) =
68− case diff t tipe of
68+ case diff aliasTipe tipe of
69 Nothing -> []
69 Nothing -> []
70 Just kvs ->
70 Just kvs ->
71 let holes = collectFields kvs
71 let holes = collectFields kvs
@@ -87,6 +87,20 @@ canonicalRealias aliases tipe =
87allEqual [] = True
87allEqual [] = True
88allEqual (x:xs) = all (==x) xs
88allEqual (x:xs) = all (==x) xs
90+bestType tipes = fst $ List.minimumBy (\a b -> compare (snd a) (snd b)) pairs
92+ pairs :: [(Type,Int)]
93+ pairs = zip tipes (map numFields tipes)
95+ numFields :: Type -> Int
98+ Lambda t1 t2 -> numFields t1 + numFields t2
100+ Data _ ts -> sum (map numFields ts)
102+ Record fields ext -> length fields + sum (map (numFields . snd) fields) + numFields ext
90diff :: Type -> Type -> Maybe [(String,Type)]
104diff :: Type -> Type -> Maybe [(String,Type)]
91diff general specific =
105diff general specific =
92 case (general, specific) of
106 case (general, specific) of