Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cogent Macros #44

Open
zilinc opened this issue Dec 8, 2016 · 17 comments
Open

Cogent Macros #44

zilinc opened this issue Dec 8, 2016 · 17 comments

Comments

@zilinc
Copy link

zilinc commented Dec 8, 2016

Just as a reminder and placeholder for now. No concrete ideas atm.

@liamoc
Copy link
Collaborator

liamoc commented Dec 8, 2016

Macros are something I want too. We may look to Rust for inspiration here.

@liamoc
Copy link
Collaborator

liamoc commented Dec 8, 2016

I think macros would allow us to avoid baking in too much syntactic sugar (although we probably still want it for error handling).

@zilinc
Copy link
Author

zilinc commented Jan 9, 2017

A semi-related things is, having quasiquotation we can probably get rid of the .ac files.

@zilinc
Copy link
Author

zilinc commented Jan 9, 2017

Rust's macros look more intuitive than TemplateHaskell for systems users.

@liamoc
Copy link
Collaborator

liamoc commented Jan 9, 2017

Yes I don't think we'll want anything even close to TemplateHaskell.

@zilinc zilinc changed the title Template Cogent Cogent Macros Jan 22, 2017
@zilinc
Copy link
Author

zilinc commented Jan 22, 2017

thinking about it more, handling indentations in macros could be challenging; in particular we don't always have the more explicit form with delimiters.

@liamoc
Copy link
Collaborator

liamoc commented Jan 23, 2017

Indentations are part of concrete syntax. Why would it matter to macros?

@zilinc
Copy link
Author

zilinc commented Jan 23, 2017

Am I missing anything? You need to write concrete syntax in the macro definitions. E.g.

macro_def X ($pat:p) = [| | Success p -> 0 |]

foo : <Success U8 | Error ()> -> U8
foo x = x | Error _ -> 1
              X (v)

Does X match on x or on 1?

@liamoc
Copy link
Collaborator

liamoc commented Jan 23, 2017

We shouldn't allow that. Macros should work on abstract syntax.

@liamoc
Copy link
Collaborator

liamoc commented Jan 23, 2017

In particular, you wouldn't be able to quote a pattern without the scrutinee, because the quote needs to be a well-formed syntactic unit.

@liamoc
Copy link
Collaborator

liamoc commented Jan 23, 2017

So you could have:


macro X x (pattern p) = [| 
    $x | Success $p -> 0  
       | Error _ -> 1
     |]

foo :  <Success U8 | Error ()> -> U8
foo x = X x v

@zilinc
Copy link
Author

zilinc commented Jan 23, 2017

But still,

macro_def X ($exp:a) = [| a | Success p -> 0
                                             | Error _ -> 1 |]

How do you align the two guards?

We'll have a macros meeting this Wed so I'll see then what the use cases are and how expressive macros we want.

@liamoc
Copy link
Collaborator

liamoc commented Jan 23, 2017

Same rules apply as with parentheses. The |'s need to line up.

@liamoc
Copy link
Collaborator

liamoc commented Jan 23, 2017

Macros must work on abstract syntax, and they must be hygienic with variable names. If they don't have those features, they're an immense source of bugs (just look at the C preprocessor)

@liamoc
Copy link
Collaborator

liamoc commented Jan 23, 2017

In Lisp, the grandfather of macro systems, you literally just take a parenthesised expression and quote it by putting a ' or the word quote in front.

Our quoting brackets or quoting mechanism should be parsed identically to parentheses.

@zilinc
Copy link
Author

zilinc commented Jan 23, 2017

Hmmm. Don't know if it's powerful enough if we want to use them to generate ad hoc polymorphic functions, generate glue functions to convert between C and Cogent data structures, etc.

@liamoc
Copy link
Collaborator

liamoc commented Jan 23, 2017

Having Cogent be the macro language is going to be troublesome, because Cogent is not Turing complete and doesn't even have recursion or loops.

So, we'll have to layer a macro language over the top. The easiest language to use would be a dynamically typed, turing-complete language where the only data type is Cogent abstract syntax trees (Lisp goes one step further and replaces Cogent with Lisp here)

Exactly what operations you need would be hard to say in advance, but I presume you'll need:

  • Some conditional structure, perhaps pattern matching on (cogent) syntax trees
  • Preservation of variable hygiene - it should never be possible to examine a (local) variable name programmatically.
  • Recursion

We could statically type the language too, but seeing as it runs at compile time any way there may not be any reason to do that.

This should be powerful enough to do anything you want to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants