0x0f0f0f / gobba

A purely functional dynamically typed programming language.
https://0x0f0f0f.github.io/gobba-book/
MIT License
56 stars 2 forks source link

Static typing #6

Closed 0x0f0f0f closed 4 years ago

0x0f0f0f commented 4 years ago

Implement static typing for expressions and function:

chrisnevers commented 4 years ago

I'd like to work on this issue. I can implement a constraint based type inference algorithm, that's easy to learn & extend

0x0f0f0f commented 4 years ago

Thank you! :D Do you have any material to read about this kind of inference algorithms? I may also use your ocamline library for #8

chrisnevers commented 4 years ago

Nice, thanks for choosing ocamline! 🎉 I'm using an approach described by Matthew Flatt's book Programming Language and Lambda Calculi from Chapter 16

0x0f0f0f commented 4 years ago

Also: Should static and dynamic typing be mixed? Like introducing a type for statically typed Lambdas.

chrisnevers commented 4 years ago

What's the use case for mixing static and dynamic typing? How would an expression be marked as dynamic (To inform the compiler not to perform inference on it)?

0x0f0f0f commented 4 years ago

I think the opposite (dynamic by default) should be the best usage case. Static functions may be defined with the concrete syntax sfun x y z -> body or slambda. We have to see if mixing static and dynamic typing is appropriate, or if migrating to static typing completely is the best option. If we migrate to static typing completely, we should find a way to decide how arguments are passed to primitives.

In dynamically typed functions, I'd like inference to be separated in two different steps, one infers the purity of an expression (Impure, Pure or Numerical which is only numerical computations and will be needed for differentiation). The other step makes normal type inference.

Type inference should be performed before normalization (optimizer.ml). I think the order that makes most sense is infer type -> normalize expression -> infer expression purity -> evaluate if purity is appropriate

0x0f0f0f commented 4 years ago

This is my first and really naive approach to inference: this function infers if an expression contains pure, impure or numerical code. Note that numericality depends on parameters. https://github.com/0x0f0f0f/minicaml/blob/157fed56c12ecda628b70ee04b73ffc4583ae614/lib/typecheck.ml#L61-L107

This is how i avoid repeating inference when evaluating expressions that have a nested body: eval now accepts a labeled argument with default value called knownpurity. If the purity of a nested expression is known, do not calculate it again. https://github.com/0x0f0f0f/minicaml/blob/157fed56c12ecda628b70ee04b73ffc4583ae614/lib/eval.ml#L25-L37

Please tell me if you have any suggestions! 🥂😀

0x0f0f0f commented 4 years ago

I will keep gobba dynamically typed for the time being.