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

Implementation of Extractable monad #113

Closed
wants to merge 83 commits into from
Closed

Conversation

gmalecha
Copy link
Contributor

@gmalecha gmalecha commented Apr 1, 2019

This is just a start of #112.

@aa755

open Plugin_core
open Constr

type nat
Copy link
Contributor Author

Choose a reason for hiding this comment

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

What should this get extracted to?

"Coq_constr.tProj"
"Coq_constr.tFix"
"Coq_constr.tCoFix" ] "Coq_constr.constr_match".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will need to handle kername and universes.

| Some typ -> Some (to_constr typ)
in
tmMap (fun x -> Obj.magic (of_kername x))
(tmDefinition (to_ident nm) typ (to_constr trm))
Copy link
Contributor

@aa755 aa755 Apr 7, 2019

Choose a reason for hiding this comment

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

Are we assuming in the line above that to_constr does unquoting as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What does "unquoting" mean? I think to_constr is just one direction of the isomorphism, it shouldn't need to do anything intelligent.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. It's body would be very similar to the following?

let denote_term evm (trm: Constr.t) : Evd.evar_map * Constr.t =

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be similar, except the type will be a little bit different.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok. I am working on it (to_constr).

Copy link
Contributor

Choose a reason for hiding this comment

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

In #11, denote_term was sufficiently functorized so that we could have used its instantiation as to_constr. Somehow, those changes to denote_term were undone since then. My last commit introduces a lot of code duplication and I think going back to the functorized version of denote_term may be the right approach.

let to_constr (t : Ast0.term) : Constr.t =
failwith "to_constr"

let of_constr (t : Constr.t) : Ast0.term =
Copy link
Contributor

Choose a reason for hiding this comment

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

As @mattam82 pointed out, part of this is in checker/src/term_quoter.ml. checker already depends on template-coq. If we import checker in this file (template-coq/gen-src/run_extractable), we would run into cyclic dependencies between the directories template-coq and checker. Would this work? If not, should we move term_quoter to the template-coq directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's move it if we can make it work.

aa755 and others added 21 commits April 7, 2019 21:07
Error: The implementation src/denote.ml
       does not match the interface src/denote.cmi:
       The value `denote_term' is required but not provided
       The value `map_evm' is required but not provided
       The value `unquote_universe_instance' is required but not provided
       The value `unquote_level' is required but not provided
       The value `unquote_string' is required but not provided
       The value `unquote_ident' is required but not provided
       The value `unquote_bool' is required but not provided
       The value `unquote_list' is required but not provided
       The value `unquote_pair' is required but not provided
Makefile.coq:604: recipe for target 'src/denote.cmx' failed
…coq into plugin-tm-interp-functor-denote

git checkout --ours template-coq/src/denote.ml
File "src/denote.ml", line 1:
Error: The implementation src/denote.ml
       does not match the interface src/denote.cmi:
       The value `denote_term' is required but not provided
gmalecha and others added 7 commits April 23, 2019 22:21
let getFields mi =
  Printf.printf "in get fields\n";
  match mi.ind_bodies with
  | [] -> Printf.printf "no ind bodies\n"; None
  | oib :: l ->
    (match l with
     | [] ->
       (match oib.ind_ctors with
        | [] -> Printf.printf "no constructors\n"; None
        | ctor0 :: l0 ->
          (match l0 with
           | [] ->
             Some { coq_type = oib.ind_name; ctor =
               (let (p, _) = ctor0 in let (x0, _) = p in x0); fields =
               oib.ind_projs }
           | _ :: _ -> None))
     | _ :: _ -> Printf.printf "multiple constructors\n"; None)

it turns out there are multiple constructors for the Point record.
let cl2s cl = String.concat "" (List.map (String.make 1) cl)

let getFields mi =
  match mi.ind_bodies with
  | [] -> Printf.printf "no ind bodies\n"; None
  | oib :: l ->
    print_string (String.concat "in get fields: " [cl2s (oib.ind_name)]);
    (match l with
     | [] ->
       (match oib.ind_ctors with
        | [] -> Printf.printf "no constructors\n"; None
        | ctor0 :: l0 ->
          (match l0 with
           | [] ->
             Some { coq_type = oib.ind_name; ctor =
               (let (p, _) = ctor0 in let (x0, _) = p in x0); fields =
               oib.ind_projs }
           | _ :: _ -> None))
     | _ :: _ -> Printf.printf "multiple constructors\n"; None)

Most likely, the inductive structure is corrupt. Some use of
Obj.magic is likely suspect.
it, instead of needing to manually edit the extract
@aa755
Copy link
Contributor

aa755 commented Apr 24, 2019

I am still getting segfault, as illustrated in the comment in the last commit. Most likely, tmQuoteInductive is producing an unsound inductive datastructure.
Is there a way to minimize the use of manually written Obj.magic. Can we generate those occurrences automatically, e.g. via Coq extraction and manually write Obj.magic free glue code?

@aa755
Copy link
Contributor

aa755 commented Apr 24, 2019

@gmalecha I wasn't able to understand run_extractable.ml, but is a fix like this needed at other branches as well?
2234271#diff-280e8b43ca60f8ce3ad3f730d6945feeL218

@gmalecha
Copy link
Contributor Author

The extraction of genLensN now works. The implementation needs a serious cleanup.

@gmalecha
Copy link
Contributor Author

@mattam82. Any objections to merging this?

@gmalecha
Copy link
Contributor Author

This is a very large PR, so just a quick summary of what happened in it.

  1. There is a new run_extractable.ml file that provides an interpreter for the extracted version of the template monad. It currently lacks support for making inductive types but is able to quote, make definitions, evaluate, etc.
  2. I needed to phase split some of the files so that you don't have to have TemplateCoq loaded in order to run a plugin built with TemplateCoq. This is what produced the new constr_quoter.ml, ast_quoter.ml, quoted.ml files. They are not in the greatest shape right now, but will take a little bit of work to clean up and are mostly orthogonal to the rest of the development.
  3. I broke something about extraction and the current plugins, e.g. checker. The README.md file in plugin-demo explains how extraction works now. There is likely to be a nicer way to implement this, so any suggestions would be welcome. I ended up fixing it by dropping the -open flags since it doesn't use the mlpack file, but if there is a nice way to do that, then that would be better.

Any feedback is welcome.

@gmalecha gmalecha changed the title working on plugin extraction Implementation of Extractable monad May 6, 2019
@SimonBoulier
Copy link
Contributor

Ok, it seems very good. However, I can't go in the details because it is complicated.

So if I understand well, there are two ways to run an extractable plugin: either as an old plugin with Run TemplateProgram or by extracting it and then loading it.

A few remarks:

  1. It would be nice to simplify the build process of MetaCoq, even the repository with all the folders becomes very messy! But I don't know how to do this. I neither don't know how to simplify the build of plugin-demo.

  2. You should document in the Readme.md at least where are the examples to look at (plugin-demo and test-suite/extractable.v if I'm correct).

  3. template-coq/test-plugin -> should go in the test-suite directory

  4. Are the Cd stuff really needed in the Extract.v files?

  5. With an operation tmQuoteRecursively : term -> global_declarations we could reimplement the "Template Check" plugin as an extractable plugin.

Say me when you want that I merge. Can you rebase it on top of coq-8.8?

@gmalecha
Copy link
Contributor Author

gmalecha commented May 7, 2019

Thanks, @SimonBoulier! I will work on these. For the recursive quoting, I was wondering if it makes more sense to write a dependencies function that would construct a list of all of the (recursive) dependencies of a term. Then you could quote individual pieces as much as you want. Does that seem reasonable?

@gmalecha
Copy link
Contributor Author

gmalecha commented May 8, 2019

Ok, it seems very good. However, I can't go in the details because it is complicated.

So if I understand well, there are two ways to run an extractable plugin: either as an old plugin with Run TemplateProgram or by extracting it and then loading it.
This is correct. It seems like a useful property since it lets you debug and test in the Coq REPL and then extract when you have what you want. Ideally they would be identical code paths, but they aren't right now.

A few remarks:

  1. It would be nice to simplify the build process of MetaCoq, even the repository with all the folders becomes very messy! But I don't know how to do this. I neither don't know how to simplify the build of plugin-demo.

I think this would be great, but I'm also not certain. I think I would need a flag that would tell the extraction plugin to not generate code for particular library.

  1. You should document in the Readme.md at least where are the examples to look at (plugin-demo and test-suite/extractable.v if I'm correct).
  2. template-coq/test-plugin -> should go in the test-suite directory
  3. Are the Cd stuff really needed in the Extract.v files?

I really don't like generating ml files into the source directory. It makes it hard for me to browse code.

  1. With an operation tmQuoteRecursively : term -> global_declarations we could reimplement the "Template Check" plugin as an extractable plugin.

Just to be a little bit more clear I opened PR #133 with my current thinking on this.

Say me when you want that I merge. Can you rebase it on top of coq-8.8?

@SimonBoulier
Copy link
Contributor

If you do some rebasing, maybe you can do it on top of #128 so that we can merge one after the other ...

@gmalecha gmalecha mentioned this pull request May 10, 2019
@gmalecha
Copy link
Contributor Author

I'm really bad at rebasing, so I pushed a new PR #134 . We can merge that and close this one.

@gmalecha
Copy link
Contributor Author

Merged in #134

@gmalecha gmalecha closed this May 14, 2019
@mattam82 mattam82 deleted the plugin-tm-interp branch September 29, 2019 07:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants