4 ms·
Having set types like this and refining them smaller is something I wish Haskell would learn from Typescript, especially the automatic inference side. I wonder
by clord 4y ago
Having set types like this and refining them smaller is something I wish Haskell would learn from Typescript, especially the automatic inference side. I wonder if it would help with linear types? Are there any proposals? I know there are type level naturals in the type system, but this is more like wanting to deconstruct existing types like Int or String into subset types.
e.g.,
foo :: Int -> 3::Int | 4::Int
foo 4 = 4
foo _ = 3
bar :: 3::Int | 4::Int -> Bool
bar 4 = True
bar 3 = False
-- (bar 12) is a compiler error, and no need to handle other patterns
baz :: 3::Int | 2::Int -> Bool
bar 3 = True
bar x = False -- Type of x is 2::Int
- consilient 4y ago> Having set types like this and refining them smaller is something I wish Haskell would learn from Typescript, especially the automatic inference side Haskell has far better type inference than Typescript in large part because it doesn't have subtyping. There are libraries for open records and sums (e.g. https://hackage.haskell.org/package/vinyl https://hackage.haskell.org/package/vinyl) but they're almost always the wrong choice.
- Tainnor 4y ago> Haskell has far better type inference than Typescript in large part because it doesn't have subtyping. That's a bit like saying "Go has a faster compiler because the language is so simple". It's true, but there's a genuine tradeoff here. Haskellers have convinced themselves that subtypes are not worth it - and they may be right in many cases - but subtyping is a quite natural way of modeling many concepts (for example: mathematical expressions). Some of that can be recovered by typeclasses, existential types and other machinery (or you could go as far as dependent types), but that seems all more cumbersome and hard to understand than the way subtyping works in languages that support it out of the box.
- consilient 4y agoThat's my point. Haskell hasn't failed to "learn" from Typescript, it's picked a different point on the same frontier. There's no strictly improved structurally typed Haskell out there waiting to be written; there are fundamental obstacles in the way.
- nequo 4y agoLiquidHaskell[1] implements refinement types for Haskell. Would that do what you'd like? [1] https://ucsd-progsys.github.io/liquidhaskell/ https://ucsd-progsys.github.io/liquidhaskell/