As far as I know,any programming language which doesn’t require to write type annotations in the source while writing a function or module and if that chunk of code is “type-correct” , compiler will infer the types and compiles the code. is there more to it?
is(are) there a such language(s)? if yes are there any limitations on it’s type system?
Update 1:
Just to be really clear , I am asking about a statically typed,fully type-inferred programming language not a dynamically typed programming language.
The limitation of full type inference is that it doesn’t work with many advanced type system features. As an example consider Haskell and OCaml. Both of these languages are almost fully type inferred, but have some features that can interfere with type inference.
In Haskell it’s type classes combined with polymorphic return types:
Here
readis a function of typeRead a => String -> a, which means “for any typeathat supports the type classReadthe functionreadcan take aStringand return ana. So iffis a method that takes an int, I can writef (read "123")and it will convert “123” to the Int 123 and callfwith it. It knows that it should convert the string to an Int becauseftakes an Int. If f took a list of ints, it would try to convert the string to a list of Ints instead. No problem.But for the
readAndPrintfunction above that approach does not work. The problem here is thatprintcan take an argument of any type that can be printed (that is any type that supports theShowtypeclass). So there’s no way for the compiler to know whether you want to convert the string to an int, or a list of ints, or anything else that can be printed. So in cases like this you need to add type annotations.In OCaml the problematic feature is polymorphic functions in classes: If you define a function that takes an object as its argument and calls a method on that object, the compiler will infer a monomorphic type for that method. For example:
Here the compiler will infer that
objmust be an instance of a class that has a method namedmethof typeint -> int, i.e. a method that takes an Int and returns an Int. You can now define a bunch of classes that have such a method and pass instances of that class as arguments tof. No problem.The problem occurs if you define a class with a method of type
'a. 'a -> int, i.e. a method that can take an argument of any type and return an int. You can not pass an object of that class as an argument tofbecause it doesn’t match the inferred type. If you wantfto take such an object as its argument, the only way is to add a type annotation tof.So those were examples of languages that are almost fully type inferred and of cases where they’re not. If you’d remove the problematic features from those languages, they’d be fully type inferred.
Consequently basic dialects of ML without such advanced features are fully type inferred. For instance I assume that Caml Light is fully type inferred since it’s basically OCaml without classes (however I don’t actually know the language, so that’s just an assumption).