Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between C++ "type deduction" and Haskell "type inference"?

In English semantics, does "type deduction" equal to "type inferring"?

I'm not sure if

  1. this is just an idiom preference chosen by different language designers, or
  2. there's computer science that tells a strict "type deduction" definition, which is not "type inference"?

Thanks.

like image 419
vik santata Avatar asked Apr 07 '16 02:04

vik santata


2 Answers

The C++ specification and working drafts use 'type deduce' extensively in reference to the type of expressions that don't have an type declaration as reference; for example this working draft on concepts uses it when talking about auto-declared variables and I remember lots of books using it when talking about templates way way back when I had to learn – and then subsequently forget most of – C++. Type inference, however, has its own Wikipedia page and is also the name of a significant field of study in programming-language theory. If you say type inference, people will immediately think of modern typed functional programming languages. You can even use it as a ruler to compare languages; some might say that their language X or their library Y is easier to type inference and is therefore better or friendlier.

I would say that type inference is the more specific, more precise, and more widely-used term. Type deduction as a phrase probably only holds cachet in the C++ community. The terms are close cousins, but the context they've been used in have given them dictional shades of color.

like image 187
hao Avatar answered Sep 28 '22 07:09

hao


As mentioned, type inference is studied for years in theory. It is also a common feature provided by several programming languages, not only Haskell.

On the other hand, type deduction is the general name of several processes defined in the C++ specification, including return type deduction, placeholder type deduction, and the origin of them, template (type) argument deduction. It is about to identify the type implied by a term (within a C++ simple-type-specifier or template-argument) with yet unknown to-be-deduced type. It is similar to type inference, which is about typing -- determining the type of a term, but more restricted.

The major differences are:

  1. Type deduction is the process to get the type of terms with some restricted form. Normally a C++ expression is typed without type deduction rules. In most cases the static type of an expression is determined by specific semantic rules forming the expression. Type inference can be used instead as the one truly general method of static typing.
  2. As the term "type" has more restricted meaning in C++ compared to that in type theory, programming language theory and many contemporary programming languages, the domain of the result is also more restricted. Namely, type deduction must deduce a well-formed C++ type, not a higher-order type (which is not expressible as a C++ type).

These restrictions would not likely change without a massive redesigning of the type system of C++, so they can be essential.

like image 25
FrankHB Avatar answered Sep 28 '22 07:09

FrankHB