@@ -61,11 +61,11 @@ realias (aliases,localize) tipe = localize (canonicalRealias aliases tipe)
6161canonicalRealias :: [(String,[String],Type)] -> Type -> Type
6262canonicalRealias aliases tipe =
6363 case concatMap tryRealias aliases of
64− [tipe''] -> f tipe''
65− _ -> if tipe == tipe' then tipe else f tipe'
64+ [] -> if tipe == tipe' then tipe else f tipe'
65+ tipes -> f (bestType tipes)
6666 where
67− tryRealias (name, args, t) =
68− case diff t tipe of
67+ tryRealias (name, args, aliasTipe) =
68+ case diff aliasTipe tipe of
6969 Nothing -> []
7070 Just kvs ->
7171 let holes = collectFields kvs
@@ -87,6 +87,20 @@ canonicalRealias aliases tipe =
8787allEqual [] = True
8888allEqual (x:xs) = all (==x) xs
8989
90+bestType tipes = fst $ List.minimumBy (\a b -> compare (snd a) (snd b)) pairs
91+ where
92+ pairs :: [(Type,Int)]
93+ pairs = zip tipes (map numFields tipes)
94+
95+ numFields :: Type -> Int
96+ numFields tipe =
97+ case tipe of
98+ Lambda t1 t2 -> numFields t1 + numFields t2
99+ Var _ -> 0
100+ Data _ ts -> sum (map numFields ts)
101+ EmptyRecord -> 0
102+ Record fields ext -> length fields + sum (map (numFields . snd) fields) + numFields ext
103+
90104diff :: Type -> Type -> Maybe [(String,Type)]
91105diff general specific =
92106 case (general, specific) of