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

WIP: Expanded documentation (inc. @gteege's manual) #334

Draft
wants to merge 33 commits into
base: master
Choose a base branch
from

Conversation

jashank
Copy link
Contributor

@jashank jashank commented Jan 7, 2020

this PR substantially expands the Cogent documentation. the result is in two parts:

  • An Introduction to Cogent, largely based on introductory text that already existed; and
  • Cogent Reference Manual, largely based on @gteege's manual converted to reStructuredText.

rendered preview: https://cogent--334.org.readthedocs.build/en/334/

@jashank
Copy link
Contributor Author

jashank commented Jan 10, 2020

I've split this PR into two parts --- the majority of the commits (but not the majority of the changes) come from what is now #338; this PR notionally depends on changes introduced there.

@jashank jashank requested a review from zilinc January 10, 2020 04:33
@jashank jashank force-pushed the docs-import-manual branch 2 times, most recently from cf29dc8 to b9e2ae0 Compare February 13, 2020 06:18
@jashank jashank removed the request for review from zilinc February 13, 2020 06:19
@jashank jashank changed the title WIP: Port Gunnar Teege's manual to reST, to fold into the manual. WIP: Expanded documentation (inc. @gteege's manual) Feb 13, 2020
@jashank jashank force-pushed the docs-import-manual branch 3 times, most recently from cf69c6d to 2b81607 Compare February 14, 2020 08:14
@jashank jashank force-pushed the docs-import-manual branch 3 times, most recently from 5233f89 to 2a21bc0 Compare May 1, 2020 06:28
Most of the transformation was done automatically with a gnarly mess of
Emacs-Lisp, and was cleaned up a bit (especially the custom TeX macros)
by hand.

This is a temporary location --- I'll be working on folding this into
the rest of the manual, copy-editing, etc.

[skip ci][skip lemma]
The 'introduction' expands the 'getting started' documentation, a quick
installation reference, and the first programs.

The 'reference' is intended to combines the previous (proto-reference)
documentation with Gunnar Teege's manual, expanding and clarifying them.

[skip ci][skip lemma]
Split up the old 'getting started' page; the examples can be fleshed out
some more and cleaned up, which is on the todo list.

[skip ci][skip lemma]
…SH ^{22})

A gentle, step-wise introduction to the `add' function unfolds, where we
build up the language's capabilities piecewise.  Instead of diving into
a polymorphic type definition, let's start with a very simple function,
and build from there.

[skip ci][skip lemma]
[skip ci][skip lemma]
Based on discussions with Peter Chubb, some of my own experiments to
clarify behaviours, and presented here for first review.  Much, much
more work is needed, of course.

[skip ci][skip lemma]
Includes changes derived from feedback from Louis Cheung -- thanks!
and there are several quotation types:

- ``$id:(IDENT)`` introduces ``IDENT`` as an identifier;
- ``$ty:(TYPE-NAME)`` refers to the type ``TYPE-NAME``;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessarily a "type name". It can be any type, including the parameterised ones, like $ty:(Array t).

- ``$id:(IDENT)`` introduces ``IDENT`` as an identifier;
- ``$ty:(TYPE-NAME)`` refers to the type ``TYPE-NAME``;
- ``$exp:(EXPRESSION)`` inserts code to execute ``EXPRESSION``;
- ``$spec:(TYPE-NAME)`` injects an appropriate C typecast; and
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this antiquote is not for typecasting. It only uses typecast's syntax, but the generated C code will be a higher-order function application. The entire antiquoted C mechanism uses the antiquotation is the Haskell C parser, repurposed. Thus we have to choose antiquotes that already exist in the C parser, and they should make sense intuitively. In this case, the typecast antiquote is the best that I can find that makes some sense (that there is one dispatch function for each function type A -> B and that's the "spec" for us to choose which dispatch function to invoke), but apparently less so compared to the others like $ty or $exp.

Quotation types influence this transformation,
and there are several quotation types:

- ``$id:(IDENT)`` introduces ``IDENT`` as an identifier;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't quite introduce an identifier. It "instantiates" a polymorphic function/type's name. So we only need to use $id when the function/type is polymorphic, so that it can derive the monomorphised name.


We can then implement this function in C::
$ty:(U32) u32_from_u8 ($ty:(U8) x) { return x; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure if antiquoting U8 or U32 are good examples. Usually these type don't need antiquotation at all. We know that they become u8 and u32 defined by the stdlib. I'm afraid it will mislead users to use them this way and lead to a more verbose antiquoted C.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my intent here (and, indeed, in most of this rewrite) is to introduce each specific element relatively carefully and clearly: exemplifying the syntax via trivially simple examples before getting to more complex (and realistic) examples feels (to me) to be a good way to introduce this, and my eventual goal is to provide more reference description. I posit the example of the trivial Cogent type U8 being transformed into the C type u8 is what I want to illustrate here. I will add some more words to suggest this is highly unusual to see, though.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively you can just be vague about the type, say T (we don't even need to know what it is, as long as it's used in the Cogent program), if you think it still keeps the matter simple for your purpose.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it twice, antiquoting primitive types isn't that bad, or at least I shouldn't feel bad about it, as I never complain about the verbosity of the antiquoted C files.


type Result t e = < Ok t | Err e >
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better examples would be to use the structural types in antiquotes directly (e.g. $ty:((U32, U8))). Type names aren't really a thing in Cogent's structural type system, they gets desugared away very early in the compilation. I would like the doc to discourage users to focus on type synonyms too much.

where the type definition ``t1`` was previously generated.
Beware that the |cogent| compiler
may not always emit the type's definition,
even if it emits a reference to it.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe be a bit more explicit: the Cogent type appearing in antiquotes have to be types that already get used in the Cogent code ("used" means that it not only appears in the code, but also used by the entry-point functions or their dependencies.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a common troubleshooting: if it generates a new C type name t42 but without the definition of it, it's usually because some new Cogent type is used in the antiquote.


Antiquoted C introduces an *antiquote*,
which takes the form
``$``, quotation type, ``:``, and a (usually parenthesised) term;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's a single word starting with lowercase, then no parens are needed. It's still useful to reduce the verbosity. On the other hand, if it antiquotes a tuple, then at least two pairs of parens are needed, one for the antiquote, and one for the tuple.

``$esc`` and ``$escstm``: No Escape In Sight
--------------------------------------------

``$esc:(PREPROCESSOR-DIRECTIVE)`` and
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing inside the $esc and $escstm doesn't have to be a cpp directive. It can be anything that the user doesn't want the Cogent compiler to see. The only different is that $esc is top-level, and $escstm is on the statement level. The only criterion is that if you remove the entire $esc and $escstm` antiquotes, the rest should still be valid antiquote C.

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

Successfully merging this pull request may close these issues.

2 participants