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

(IR-Internals) Expose type defs internals.mo to IR passes #2408

Open
nomeata opened this issue Mar 9, 2021 · 3 comments
Open

(IR-Internals) Expose type defs internals.mo to IR passes #2408

nomeata opened this issue Mar 9, 2021 · 3 comments
Labels
compiler Motoko → Wasm P3 low priority, resolve when there is time

Comments

@nomeata
Copy link
Collaborator

nomeata commented Mar 9, 2021

Currently, we have a few type definintions in construct.ml:

let answerT = T.unit

let contT typ = T.Func (T.Local, T.Returns, [], T.as_seq typ, [])

let err_contT =  T.Func (T.Local, T.Returns, [], [T.catch], [])

let cpsT typ = T.Func (T.Local, T.Returns, [], [contT typ; err_contT], [])

some of which are just manully parsed copies of the same types in internals.mo:

type @Cont<T> = T -> () ;
type @Async<T> = (@Cont<T>,@Cont<Error>) -> ();

The point of internals.mo is to define values and types for internal use, and for values that works nicely. It would be more ergonomic (for us) if IR passes (e.g. async.ml) would could somehow just these types without duplicating them manually.

This is harder than for values because values are still bound at the IR stage, while type definitions just float around.

Maybe the pipeline somehow has to pass the initial_stat_env (or portions from it – maybe just the @-prefixed type definitions) to the IR code?

This came up in the context of #2348, where I wasn’t looking forward to replica

type @TypRep = {
  #null_;
  #bool;
  #nat;
  #nat8;
  #nat16;
  #nat32;
  #nat64;
  #int;
  #int8;
  #int16;
  #int32;
  #int64;
  #word8;
  #word16;
  #word32;
  #word64;
  #float;
  #char;
  #text;
  #blob;
  #error;
  #principal;
  #obj : ({#object_; #actor_; #module_; #memory}, @Fields);
  #variant : @Fields;
  #array : @TypRep;
  #opt : @TypRep;
  #tup : [@TypRep];
  #func_; // TODO: function type arguments
  #any;
  #non;
};

by writing

let typRepT =
  let open T in
  let c = Con.fresh "TypRep" (Abs ([], Pre)) in
  let t = Con (c, []) in
  let rhs =
    Variant [
      { lab = "bool"; typ = T.unit; depr = None };
      { lab = "int"; typ = T.unit; depr = None };
      …
      { lab = "tup"; typ = T.Array t; depr = None }
      …
    ] in
  set_kind c (Def ([], rhs));
  t

(although maybe it’s not that bad, our IR type checker would catch mistakes.)

@nomeata nomeata changed the title (IR-Internals) Somehow expose type definitions in internals.mo to IR passes (IR-Internals) Expose type defs internals.mo to IR passes Mar 9, 2021
@crusso
Copy link
Contributor

crusso commented Mar 9, 2021

The types in construct.ml that you mention actually have some (dodgy) inbuilt flattening, so they are not really the same as the prelude defined, unary ones.

@nomeata
Copy link
Collaborator Author

nomeata commented Mar 10, 2021

Oh, ok, thanks for the heads up. Then maybe this issue is mostly about TypRep.

@nomeata
Copy link
Collaborator Author

nomeata commented Mar 10, 2021

Defining this (although kinda large) type twice (once in internals.mo and once in construct.ml) isn’t too bad after all, and the type checker keeps us honest. So not a high priority here.

@rossberg rossberg added compiler Motoko → Wasm P3 low priority, resolve when there is time labels May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler Motoko → Wasm P3 low priority, resolve when there is time
Projects
None yet
Development

No branches or pull requests

3 participants