type-level-sets icon indicating copy to clipboard operation
type-level-sets copied to clipboard

Add functional dependency to IsMember for better type inference

Open jmorag opened this issue 3 years ago • 4 comments

Resolves #26

jmorag avatar Jan 21 '22 15:01 jmorag

Added another commit for better inference with update. Previously,

ghci> m = Ext (Var @"z") 'b' $ Ext (Var @"w") 'a' Empty
ghci> m
{z :-> 'b', w :-> 'a'}
ghci> update m (Var @"w") 3
    • Could not deduce (Updatable
                          "w" t0 '[ "z" ':-> Char, "w" ':-> Char] n)
      from the context: (Updatable
                           "w" t '[ "z" ':-> Char, "w" ':-> Char] n,
                         Num t)
        bound by the inferred type for ‘it’:
                   forall t (n :: [Mapping Symbol *]).
                   (Updatable "w" t '[ "z" ':-> Char, "w" ':-> Char] n, Num t) =>
                   Map n
        at <interactive>:973:1-26
      The type variable ‘t0’ is ambiguous
    • In the ambiguity check for the inferred type for ‘it’
      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
      When checking the inferred type
        it :: forall t (n :: [Mapping Symbol *]).
              (Updatable "w" t '[ "z" ':-> Char, "w" ':-> Char] n, Num t) =>
              Map n
ghci> update m (Var ::Var "w") (3::Int)
update m (Var ::Var "w") (3::Int)
  :: Updatable "w" Int '[ "z" ':-> Char, "w" ':-> Char] n => Map n

With 686f64d,

ghci> m = Ext (Var @"z") 'b' $ Ext (Var @"w") 'a' Empty
ghci> m
{z :-> 'b', w :-> 'a'}
ghci> update m (Var @"w") 3
{z :-> 'b', w :-> 3}
ghci> :t update m (Var @"w") 3
update m (Var @"w") 3 :: Num t => Map '[ "z" ':-> Char, "w" ':-> t]

jmorag avatar Jan 22 '22 19:01 jmorag

Actually, never mind the update thing. I consider it better, but it changes the number of parameters to the Updatable class, which is a breaking change that shouldn't be included in this PR. Let me know if you'd like me to open a separate PR with that.

jmorag avatar Jan 22 '22 20:01 jmorag

Ping @dorchard

jmorag avatar Mar 10 '22 20:03 jmorag

This change is very useful in some stuff I'm building on top of type-level-sets. Thank you, @jmorag!

jship avatar Apr 05 '22 20:04 jship