Do you know of a language with Static Type checking where Code is Data? [closed]
Asked Answered
H

6

17

Can you name languages with static type checking (like Java) and where code is data (like in LISP)? I mean both things in one language.

Headstrong answered 18/6, 2010 at 14:18 Comment(4)
I would tend to agree with the answers to this question: #2953641 that it is simply not possible to have both.Sideway
@Daniel: Only the lowest-voted answer to that question claims that it's not possible to have both. It does this by claiming that the typing would be really hard. Hard, yes, but not impossible.Vina
This is very similar to #3038143Kweiyang
en.wikipedia.org/wiki/NemerleChang
D
14

Qi is a statically-typed Lisp dialect. Also, many other Lisp dialects have (optional) static typing.

Java itself has very limited capabilities of this kind.

The interesting question is not so much whether you can have metaprogramming and static typing, it's whether you can have dynamic metaprogramming be statically type-safe.

There is Template Haskell which does metaprogramming and is type-safe, but it is static metaprogramming.

At the moment I can not think of any language that I know for a fact allows dynamic metaprogramming and where dynamic metaprogramming is statically type-safe. Qi might be bale to do it, but I'm not sure.

Doorknob answered 18/6, 2010 at 14:35 Comment(1)
Can I ask for a clarification of terms? I'm not sure I understand your point as is. What do you mean by dynamic versus static metaprogramming? Is static metaprogramming "at compile time", ie macros, vs dynamic, "at run time" ie arbitrary expression quotation?Vina
H
11

Racket (formerly PLT Scheme) has a statically typed dialect, which is designed to work nicely with Scheme idioms -- including macros. (It works by type-checking the expansion results.)

Hiltonhilum answered 18/6, 2010 at 18:39 Comment(0)
V
4

F# has Quotation expressions. From the MSDN page:

// typed
let e : Expr<int> = <@ 1 + 1 @>
// untyped
let e' : Expr = <@@ 1 + 1 @@>
// splicing with %
// similar to Lisp's unquote-splicing, but type-checked:
// you can only splice expressions of the appropriate type
<@ 1 + %e @>

I think these are available in C#, but (1) I don't know what the syntax is (2) the data structures are different.

These languages allow code as data at compile time, like Lisp macros:

Disclaimer: I haven't really used any of these. As far as I know, they are all much more complicated than Lisp's quote.

However, 90% of "Code as data" using quote can be accomplished with closures, since they delay evaluation too. Many languages have a convenient syntax for making closures (C#, Clojure, Scala and Ruby especially come to mind) and don't need quote that much. Even in Scheme, which is a definitive Lisp, the prevailing style favours passing functions over writing macros.

Vina answered 18/6, 2010 at 14:32 Comment(4)
Macros are not 'compile time'. Macros work perfectly well in Lisp interpreters. Lisp Macros also are not 90% about delaying evaluation. Macros have a lot of purposes. Most have nothing to do with delaying evaluation.Uncommunicative
Macro evaluation occurs at 'read time' in Common Lisp and Scheme (as far as I recall). I did not know whether the asker would recognise the term so I used a less accurate but more common one. There have been experimental Lisps with first-class macros, but they're not widespread. As for delaying evaluation, that's precisely what quote is for, and what closures give you as well, albeit less conveniently. I was not talking only about macros, since they are a restricted use of code-as-data.Vina
Nathan, macro evaluation sure does not happen at 'read time' in Common Lisp. Not a bit. Macro expansion happens during interpretation and/or compilation and that is described in the CL standard. If code runs interpreted, then macros can be expanded during interpretation. QUOTE is also not for delaying evaluation like 'closures'. QUOTE is there to denote literal data. Quoted code is not delayed, so that it can be 'forced'. If one wants to executed quoted code, then it has to be evaulated. That's a huge difference.Uncommunicative
F# also has Type Providers, which are custom DLLs that run at compile time. These use expressions to produce compiled code rather than runtime data structures.Autoradiograph
T
4

Template Haskell is statically typed but allows you to manipulate code as data, aka metaprogramming. Related languages include MetaML and MetaOCaml. Look up the work of Tim Sheard.

Twocolor answered 18/6, 2010 at 16:28 Comment(2)
MetaML and MetaOCaml are quite different than TH - the former are doubly-typed (typed generators guarantee to only generate typed code) while TH is only singly-typed, i.e. only the results of template expansion is type-checked.Kweiyang
@Jacques good point, but they're still similar enough that I'm willing to put them in the same answer :-)Twocolor
L
0

If you're simply looking for the ability to execute code dynamically in a statically typed language, then Java itself can do that:

http://www.javaworld.com/javaworld/jw-06-2006/jw-0612-dynamic.html

If you need more than that (want methods and classes as first-class objects, etc.), then you'll want to use something like Haskell or C# (as mentioned in other answers) instead.

Lamanna answered 18/6, 2010 at 14:31 Comment(0)
S
0

Maybe Strongtalk or Zero which are reflective system a la Smalltalk, but are statically typed.

Scaramouch answered 22/6, 2010 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.