4 ms·
I think it's worth talking about the ability to propagate user-defined invariants. To me the most useful aspect of a type system is that it enables you to decla
by lmm 12y ago
I think it's worth talking about the ability to propagate user-defined invariants. To me the most useful aspect of a type system is that it enables you to declare that all Xs have some property, and then the language guarantees that this holds. "Has a method named .foo(...)", while useful, shouldn't be the end of what we try to encode, but there's a big gap between allowing things like "an X is either a Y or a Z" and allowing arbitrary type-properties, liquidhaskell style, which the article just glosses over.
Which is a shame, because it's precisely this range - the gap between non-liquid Haskell or Ocaml and Java - which is most relevant to today's programmers making choices of programming language.
- colanderman 12y agoYes, I've long wanted to be able to attach arbitrary predicates to types. This is great for functional state machines; say the state machine type is sm, you can specify functions like fun initialize(sm) -> sm' where sm.state == uninitialized, sm'.state != uninitialized or fun ping_clients(sm) -> sm' where has_clients(sm) in concert with fun has_clients(sm) -> bool The compiler can then track these, and ensure e.g. that "has_clients" is called and returns "true" before "ping_clients" is called on a given value. An even smarter compiler can, if it knows the definitions of these functions, determine statically whether a given predicate is guaranteed to produce the same result on a modified object. (e.g. simply by inspecting which fields are inspected and modified)
- tel 12y agoYou can get this example working with indexed monads in Haskell. You can also do it quite nicely in dependently typed languages like Idris/Agda. However, abitrary predicates is a different matter. Even if you have a language which allows arbitrary computation in types there exist undecidable predicates which may weaken your desire for arbitrary ones.
- AnimalMuppet 12y agoIt seems to me that you probably don't want to do arbitrary computation in types at compile time. Even "this type is an integer that must be non-negative" is a problem if that type allows subtraction. Now your compiler needs to know the the value of all such types at the time that subtraction was applied to them, to know if the operation is valid. That's not a good position to put your compiler in. It seems to me to be better to make this a runtime check, which the compiler makes sure to call every time subtraction is applied to a value of that type.
- dllthomas 12y agoI think even better is to have a subtraction that returns a different type, where you can't prove that a > b. Where you can, I don't know that I object to the compiler tracking such things, though I also don't actually use a dependently typed language.
- tel 12y agoWell, you'd probably have to introduce a type such that every time you subtracted n from m you must prove that m is at least as big as n. Which would be a pain. As usual, I really suggest playing with Agda or Idris to get a feel for what sophisticated types actually look and feel like.
- judk 12y agohttps://hackage.haskell.org/package/type-level-numbers-0.1.1.1/docs/TypeLevel-Number-Nat.html https://hackage.haskell.org/package/type-level-numbers-0.1.1... But it's not clear to me what happens when you try to compile and typecheck program that attempts to subtract numbers provided as input at runtime.
- tel 12y agoThat link only goes part way. What you'd need to do is something like createProof : (n : Nat) -> (m : Nat) -> Maybe (n >= m) which could either succeed or fail to produce the needed proof at runtime. Without the proof it'd be impossible to perform the subtraction, so that branch of your program is statically inaccessible.
- ced 12y agoCommon Lisp supports arbitrary type predicates, and I think that SBCL enforces them (not sure) at runtime. For instance, it will call the predicate before setting a struct's field to make sure that it returns true (under high safety compilation, low safety just trusts that it's correct)
- tel 12y agoThat's more of a contract system than a type system, though.