Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Language with types as first-class values?

Is there a language, which is:

1) functional

2) has type inference

3) has currying

4) and has types as first-class values

also would like to compile from it to JVM and/or CLR

like image 646
Suzan Cioc Avatar asked Jan 25 '13 09:01

Suzan Cioc


People also ask

Which languages have first-class functions?

Functional programming languages, such as Erlang, Scheme, ML, Haskell, F#, and Scala, all have first-class functions. When Lisp, one of the earliest functional languages, was designed, not all aspects of first-class functions were then properly understood, resulting in functions being dynamically scoped.

What are first class values in programming?

A programming language is said to have First-class functions when functions in that language are treated like any other variable. For example, in such a language, a function can be passed as an argument to other functions, can be returned by another function and can be assigned as a value to a variable.

Is C++ a first class?

Functions in C/C++ are not first-class. While (1) and (3) are arguably available through function pointers, (2) is not supported for functions proper.

Is Java a first-class function?

Functions as First Class Objects Functions can also be passed as parameters to other functions. In Java, methods are not first class objects. The closest we get is Java Lambda Expressions.


2 Answers

F# is functional and has type inference, currying and types as first-class values in the sense that you can dissect types at run-time via reflection. It compiles to the CLR and works well on Mono.

EXAMPLE: Taken from my (non-free) article Structural Typing in the F#.NET Journal:

The following createType function creates a new .NET assembly, new module and new public class type of the given name:

> let createType typeName =
    let name = System.Reflection.AssemblyName(Name="tmpAssembly")
    let run = System.Reflection.Emit.AssemblyBuilderAccess.Run
    let builder = System.Threading.Thread.GetDomain().DefineDynamicAssembly(name, run)
    let mdl = builder.DefineDynamicModule "tmpModule"
    let attrs = TypeAttributes.Public ||| TypeAttributes.Class
    mdl.DefineType(typeName, attrs);;
val createType : string -> TypeBuilder
like image 165
J D Avatar answered Oct 03 '22 17:10

J D


I just started learning it but Coq might work for you.

It's quite possible to have a function which takes in a type (yes a raw type, not an instance of that type) and return another type (again, just the type, not an instance). If you're at all interested in formal verification of programs it's worth a look.

It also has the nice little benefit of being able to convert it's code to Haskell/OCaml/Scheme so that you can use their IO/Libraries since Coq tends lacks them.

It has type inference and currying but the type inference isn't perfect as the language's type system is well beyond (and more expressive than) a standard Milner-Hindley type system.

like image 23
Daniel Gratzer Avatar answered Oct 03 '22 19:10

Daniel Gratzer