by charriu · 11y ago · 0 comments
First, a program can be compiled (static) or interpreted (dynamic) as stated in the article. However, that does not mean that you can't have a dynamic type system in a compiled language, or vice versa.
Also, if you add type inference, the examples given for variables in dynamic languages are perfectly valid examples for variables in a language with a static type system (the type would just be defined on first assignment).
I'm missing a discussion of weak vs strong in the article though. Visual Basic might be statically typed but it does have implicit type coercion which gives it a very different feel from — say — C#.
What is harder is to achieve the same performance jump from interpreted to compiled. Exactly because the "naive" approach to compiling dynamically typed languages gives you similar machinery to a well written interpreter, while for static language the "naive" approach often gets you much more efficient results.
No it is not. All major dynamic languages can be compiled usually JIT. Every major browser has a JIT compiler for Javascript. Python, Ruby, and Lua have JIT-compiled forks/reimplementations.
The difference of JIT vs ahead-of-time compilation is irrelevant for the high level of the article.
JITs are profoundly different from standalone compilers. And are many times more complicated.
> The difference of JIT vs ahead-of-time compilation is irrelevant for the high level of the article.
No it's not irrelevant. JITs are starting to kick in at a very different point than the static compilers: they've got a path trace, runtime type information and profiling information, while the static compiler only got an AST.
JITs can do gradual refinement, and for this they may implement multiple different compilation techniques. Static compiler must do all it can at once, there is no other chance to improve.
I might be better served applying my time towards development on Chicken (Scheme).
In any case, compiled Common Lisp is not something that resembles other compiled languages... and the purpose of my article is to discuss either designing an interpreter or a compiler, not a dynamic compiler, recompiler, or compiled code that requires some kind of runtime interpreter as a side, all of which are extraneous use cases for hackers not people learning how to program. They also make for poor pedagogical tools.
Personally I find that the article needs a bunch of improvements:
static vs. dynamic vs. statically typed and dynamically typed
These are different things. Either we are talking about runtime behavior (a dynamic language can change itself or the program at runtime) or we are talking about typing.
For example Java is a statically typed language, but it allows the use of class loaders which can reload classes at runtime - which provides some dynamic features. On the other end of the spectrum are many compiled Lisp implementations which allow various changes to the programming language at runtime, for example via an extensive meta-object protocol.
> Attempting to access a value that has not been named in a relevant scope leads to a syntax error issued at compile time.
A syntax error? Really? Syntax?
> So called dynamically typed languages are sometimes referred to as duck-typed or duck languages.
Definitely not. Duck typing is a only a special form of dynamic typing, usually in an object-oriented language. Non object-oriented dynamically typed languages don't provide duck typing, because they lack OOP objects.
Does compiled Lisp look different?
Maybe... Let's look at SBCL on a 64bit Intel processor:
* (defun calc (a b c) (+ (* 2 a) (* 3 b) (* 4 c)))
CALC
* (disassemble #'calc)
; disassembly for CALC
; Size: 118 bytes. Origin: #x1002EC2599
; 599: 48895DE8 MOV [RBP-24], RBX ; no-arg-parsing entry point
; 59D: BF04000000 MOV EDI, 4
; 5A2: 488BD1 MOV RDX, RCX
; 5A5: 41BBD0020020 MOV R11D, 536871632 ; GENERIC-*
; 5AB: 41FFD3 CALL R11
; 5AE: 488BF2 MOV RSI, RDX
; 5B1: 488B5DE8 MOV RBX, [RBP-24]
; 5B5: 488975F0 MOV [RBP-16], RSI
; 5B9: BF06000000 MOV EDI, 6
; 5BE: 488BD3 MOV RDX, RBX
; 5C1: 41BBD0020020 MOV R11D, 536871632 ; GENERIC-*
; 5C7: 41FFD3 CALL R11
; 5CA: 488BFA MOV RDI, RDX
; 5CD: 488B75F0 MOV RSI, [RBP-16]
; 5D1: 488BD6 MOV RDX, RSI
; 5D4: 41BBD0010020 MOV R11D, 536871376 ; GENERIC-+
; 5DA: 41FFD3 CALL R11
; 5DD: 488BDA MOV RBX, RDX
; 5E0: 48895DF0 MOV [RBP-16], RBX
; 5E4: 488B55F8 MOV RDX, [RBP-8]
; 5E8: BF08000000 MOV EDI, 8
; 5ED: 41BBD0020020 MOV R11D, 536871632 ; GENERIC-*
; 5F3: 41FFD3 CALL R11
; 5F6: 488BFA MOV RDI, RDX
; 5F9: 488B5DF0 MOV RBX, [RBP-16]
; 5FD: 488BD3 MOV RDX, RBX
; 600: 41BBD0010020 MOV R11D, 536871376 ; GENERIC-+
; 606: 41FFD3 CALL R11
; 609: 488BE5 MOV RSP, RBP
; 60C: F8 CLC
; 60D: 5D POP RBP
; 60E: C3 RET
NIL
Now we can even define the types in Common Lisp: * (defun calc (a b c)
(declare (fixnum a b c))
(the fixnum
(+ (the fixnum (* 2 a))
(the fixnum (* 3 b))
(the fixnum (* 4 c)))))
STYLE-WARNING: redefining COMMON-LISP-USER::CALC in DEFUN
CALC
* (disassemble #'calc)
; disassembly for CALC
; Size: 38 bytes. Origin: #x1002F4B802
; 02: 48D1E1 SHL RCX, 1 ; no-arg-parsing entry point
; 05: 488D047F LEA RAX, [RDI+RDI*2]
; 09: 48D1F9 SAR RCX, 1
; 0C: 48D1F8 SAR RAX, 1
; 0F: 4801C1 ADD RCX, RAX
; 12: 48C1E602 SHL RSI, 2
; 16: 48D1FE SAR RSI, 1
; 19: 4801F1 ADD RCX, RSI
; 1C: 48D1E1 SHL RCX, 1
; 1F: 488BD1 MOV RDX, RCX
; 22: 488BE5 MOV RSP, RBP
; 25: F8 CLC
; 26: 5D POP RBP
; 27: C3 RET
NIL
*However, I don't think manual casting is a cost at all - it's more of a guide telling you something in the design is wrong, or a reminder to make a comment of why you do it. And for any long maintanence projects, you want to catch all the bugs you can as early as you can.
Really any run time could completely disregard types, it just might lead to a huge number of failure cases. These generalizations are there for us to start to get things to work.
When programming languages add features like these, especially 10 or 20 years after they were developed, they definitely feel tacked-on and if you inspect how they work, it's not pretty. If it's an interesting quirk then it might be an example to study.