Homoiconic type theory
Asked Answered
T

5

11

Lisp has the property of being homoiconic, that is, the representation of code used by the language implementation (lists) is also available to, and idiomatically used by, programs that want to represent code for their own purposes.

The other major family of functional programming languages, ML, is based on type theory, which means that the language implementation needs a more complex representation of code, and is also less casual about what you are allowed to do, so typically the internal representation is not available to programs. For example, proof checkers for higher-order logic are often implemented in ML family languages, but normally implement their own type theory system, effectively ignoring the fact that the ML compiler has one already.

Are there any exceptions to this? Any programming languages based on type theory, that expose their code representation for programmatic use?

Trypanosome answered 29/11, 2011 at 1:8 Comment(0)
H
12

Take a look at type systems for staged execution (a weak form of metaprogramming), for example, the one used in MetaML language.

Also note that while attractive at first glance, homoiconicity (and metaprogramming by direct AST manipulation in general) turned out to be inconvenient in practice. Take a look at modern systems of macros in Nemerle and an experimental extension of Scala, which both rely on quasi-quotation if I remember correctly.

As for why ML type theory is not reused, here are a few considerations:

  • ML type system is not expressive enough (think of dependent types)
  • ML type system is polluted with general recursion and mutable references
  • There are no consensus on which type system is usable both for proving and writing general-purpose programs. It is an ongoing research. See for example http://www.nii.ac.jp/shonan/seminar007/ . So every prover implements his own theory just because authors fix flaws in previous type theories.
Hymnology answered 29/11, 2011 at 16:16 Comment(1)
"homoiconicity (and metaprogramming by direct AST manipulation in general) turned out to be inconvenient in practice" Could you elaborate on/point to evidence of this claim?Mam
S
7

The other major family of functional programming languages ... is based on type theory, which means that the language implementation needs a more complex representation of code

I see no reason why this would be true.

If you haven't seen it already, you may be interested in Liskell, which advertises itself as Haskell semantics + Lisp syntax.

Sindee answered 29/11, 2011 at 3:20 Comment(0)
B
3

The main profit from Lisp being homoiconic is strong metaprogramming capacity. I guess you might want to take a look at type-safe metaprogramming, particularly Template Haskell.

Belva answered 29/11, 2011 at 9:20 Comment(1)
"The main profit from Lisp being homoiconic is strong metaprogramming capacity". Languages of the Meta Language (ML) family like SML and OCaml dropped homoiconicity precisely because that is not true.Comprehend
M
2

Shen :

Shen has one of the most powerful type systems within functional programming. Shen runs under a reduced instruction Lisp and is designed for portability.

E.g.:

(define total
  {(list number) --> number}
  [] -> 0
  [X | Y] -> (+ X (total Y)))

Typed Racket :

Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type system that ensures the absence of many common errors.

E.g.:

#lang typed/racket
(: sum-list (-> (Listof Number) Number))
(define (sum-list l)
  (cond [(null? l) 0]
        [else (+ (car l) (sum-list (cdr l)))]))

Mercury :

Mercury is a logic/functional programming language which combines the clarity and expressiveness of declarative programming with advanced static analysis and error detection features.

E.g.:

:- func sum(list(int)) = int.   
sum([]) = 0.
sum([X|Xs]) = X + sum(Xs).
Mam answered 8/9, 2014 at 3:0 Comment(0)
C
1

Are there any exceptions to this? Any programming languages based on type theory, that expose their code representation for programmatic use?

SML does not expose code programmatically but OCaml and F# do. OCaml has a full macro system.

Comprehend answered 10/8, 2015 at 1:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.