MatrixAI / Architect

Programming Language for Type Safe Composition of Distributed Infrastructure
Apache License 2.0
1 stars 0 forks source link

Investgate Piggybacking off the Haskell Compiler Infrastructure (or other libraries) #14

Open CMCDragonkai opened 6 years ago

CMCDragonkai commented 6 years ago

Since we're not writing a full language, we would like to piggy back Haskell's compilation infrastructure as much as possible.

The good thing is that Haskell's GHC is really modular. So we can just reuse much of it.

For example there's the GHC as a library: https://hackage.haskell.org/package/ghc

And there's the hint library that wraps around the GHC library into an easier to consume API: https://hackage.haskell.org/package/hint

More details on this here: http://downloads.haskell.org/~ghc/latest/docs/html/users_guide/extending_ghc.html

Discussed with @olligobber

CMCDragonkai commented 6 years ago

GHC currently has type checker plugins capability which allows people to extend GHC with their own type checking code to type check new things:

http://christiaanb.github.io/posts/type-checker-plugin/

This may or may not be useful, since it's plugging into the GHC ecosystem to typecheck Haskell code (plus new code that you write).

Whereas we will have a target language we want to type check that is an external DSL. If we are converting our external DSL into Haskell ADTs, plugins like these would type check the Haskell code that we write for the compiler/interpreter instead of the target language.

An example of type checking would be like the units of measure: https://github.com/adamgundry/uom-plugin/

The examples show Haskell source file that has special types added in using the quasiquoter. The quasiquoter seems like it is still necessary to make sure that the syntax is still correctly parsed as this is a type checker plugin, not a syntax plugin.

CMCDragonkai commented 6 years ago

There's a plugin system here: https://github.com/stepcut/plugins described by: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.9.7627&rep=rep1&type=pdf

It appears to show how you can load Haskell code at runtime.

The introduction to the paper shows an interesting idea. What if we did write our type system rules as an extension/plugin to the GHC system, and simply translate our Architect expressions directly into standard Haskell code.

The only problem would be error reporting. How to make sure that makes sense to the end user who is interacting at the Architect level, and not at the Haskell level.

CMCDragonkai commented 6 years ago

http://blog.reverberate.org/2013/09/ll-and-lr-in-context-why-parsing-tools.html

CMCDragonkai commented 6 years ago

I'm working of the hnix compiler. https://github.com/haskell-nix/hnix

Since nix was a DSL for just package building, but also pure lazy and functional. I figured it's a good place to start.

After studying it for a couple weeks here are my comments.

CMCDragonkai commented 6 years ago

Weird syntax I discovered by investigating the hnix source code (nix syntax that I didn't know was legal):

{ a = 3; }.b or 4
# 4
({ a = { a = 3; }; }).a         .a
# 3
{ a = { a = 3;}; }.a or { a = 4; }    .a
# { a = 3; }
CMCDragonkai commented 6 years ago

The book Practical Foundations of Programming Langauges first starts off with simple languages like T or M. Eventually it reaches chapter 19 which it introduces language PCF.

PCF stands for "Programming Computable Functions"

https://en.wikipedia.org/wiki/Programming_Computable_Functions

Every other advanced features like exceptions, continuations... etc are then built as "extensions" to PCF.

Here's a worked through example taking a PCF reified as a haskell data type and building a compiler that targets C. https://jozefg.bitbucket.io/posts/2015-03-24-pcf.html

It uses a cool little library called c-dsl that builds on top of language-c http://hackage.haskell.org/package/c-dsl and http://hackage.haskell.org/package/language-c for generation of the C code.

Architect language isn't generating C any time soon, but it's interesting that even the Nix language ultimately shares its origin with these extensions on top of lambda calculus.

CMCDragonkai commented 6 years ago

I'm currently only working on the frontend of the language (parser and AST). Since this is right now being based on Nix, the language is untyped, but I'm going to try to work types into it, while also removing some Nix stuff that isn't relevant.

CMCDragonkai commented 6 years ago

Some potential resources for exploring implementing type checkers (with polymorphism):

I want to explore PCF since it's the most basic implementation of a typed lambda calculus that has some extra features. And Nix doesn't have types, so while it's a good way of deriving our requirements for laziness and parsing and functional semantics, there's no type theory involved! So PCF is a good starting point.

Another megaparsec article:

CMCDragonkai commented 5 years ago

The recursive nature of attribute sets or let-rec declarations combined with the defaulting syntax allows you do some interesting things here:

let f = { a ? 1, b ? a }: a + b; in f {}
# 2