by hood_syntax · 10y ago · 0 comments
I don't think it can manage "full" dependent types, for example situations I often find myself in where I want to calculate a type from incoming data, e.g. something like:
parseJSON : String -> Maybe JSON
parseJSON = ...
parseMyData : JSON -> Maybe MyData
parseMyData = ...
HasSomeProperty : MyData -> Type
HasSomeProperty x = case x of
MyCase1 -> ...
...
someProcessing : (x : MyData) -> HasSomeProperty x -> ResultOfProcessing x
someProcessing x p = ...
Often, we write "HasSomeProperty" such that it performs some arbitrary calculation to decide between returning the unit type ("does have this property") or returning the empty type ("doesn't have the property, or can't tell"). Whenever we need to provide a value of type "HasSomeProperty x", we just put a unit value and the type-checker will enforce that our arbitrary calculation resulted in a unit type. We can then use that property to access a bunch of functionality which the compiler would otherwise forbid us from using.This sort of code is trivial in a dynamically typed language: there's no requirement to prove anything, so we don't need any "HasSomeProperty x" values at all: we just go ahead and use whatever code we like, and hope that we didn't mess something up.
We hit problems when we want to write our program in a language like Haskell with a (relatively) weak type system: the type system quite rightly forbids us from writing the unsafe version that dynamic languages would accept, but the language isn't expressive enough for us to (easily) encode the proofs required to convince the type checker that what we're doing is safe.
As somebody with familiarity with Haskell in the small but not in large applications, is it not similarly trivial to write in Haskell with the same level of safety that dynamic languages give you (i.e. explosion at runtime)?
Consider as an example the following safe lookup function (standard Haskell)
lookup :: Eq a => a -> [(a, b)] -> Maybe b
lookup _ [] = Nothing
lookup x ((a, b):abs)
| a == x = Just b
| otherwise = lookup x abs
and the following unsafe lookup function lookup :: Eq a => a -> [(a, b)] -> b
lookup x ((a, b):abs)
| a == x = b
| otherwise = lookup x abs
How is the latter any worse than lookup in a dynamically-typed language where you are assuming the lookup will succeed (and hence don't write any error handling logic)? I know this sort of code is frowned upon in Haskell, but it can be done.Presumably you mean "better", not "worse". Well, it's better because you will (or should) get a compiler warning (or ideally an error) about the latter version.
I don't understand how you could possibly write any non-trivial (e.g. just unit-returning) code in the body of "someProcessing" with having some sort of proof object (generated by HasSomeProperty) with which to extract whatever you'd be interested in. As the example is written you'd only have a Type, but I don't see how that's enough.
Can you explain or perhaps give an example of what a "someProcessing" function body might look like?