Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
b46e72a
first shot at #2354
ggreif Jan 27, 2022
e6e9915
add test
ggreif Jan 27, 2022
67500bf
improve a bit
ggreif Jan 27, 2022
2d40582
unit grooming
ggreif Jan 27, 2022
1556274
cleaner
ggreif Jan 27, 2022
3b120ee
resolve the type error at the access site
ggreif Jan 27, 2022
bcf3fcb
resolve the module vs. field of module ambiguity
ggreif Jan 27, 2022
59efd68
thread the module note through
ggreif Jan 27, 2022
957f912
clean up
ggreif Jan 27, 2022
a1ee29c
get the module `VarE`'s type right
ggreif Jan 27, 2022
91c3708
cleanup
ggreif Jan 27, 2022
8d7fb3c
make it `'import' <pat_nullary> '='? <text>`
ggreif Jan 27, 2022
8fe6866
pointless
ggreif Jan 27, 2022
ddcef0f
also pointless
ggreif Jan 27, 2022
f581947
Update src/mo_frontend/parser.mly
ggreif Jan 27, 2022
0eff5af
capture the entire pattern
ggreif Jan 27, 2022
999d654
cleanups
ggreif Jan 27, 2022
c32448f
make `comp_unit_of_prog` linear-time
ggreif Jan 27, 2022
58c629c
Revert "pointless"
ggreif Jan 27, 2022
ad314b7
Revert "also pointless"
ggreif Jan 27, 2022
2e9df25
use modern syntax
ggreif Jan 27, 2022
7293902
fix repl tests
ggreif Jan 27, 2022
93dcc88
a few dodgy imports
ggreif Jan 28, 2022
1a0a069
accept
ggreif Jan 28, 2022
e72ce22
more failure modes
ggreif Jan 28, 2022
d43c7ab
ignore explicit imports for now
ggreif Jan 28, 2022
3669465
ocamlformat
ggreif Jan 28, 2022
6efb5f6
mention explicit imports
ggreif Jan 28, 2022
b109d6d
typo
ggreif Jan 28, 2022
4c94e00
teach IR typechecker to reject refutable import patterns
ggreif Jan 28, 2022
1eef8dc
document the new syntax for imports
ggreif Jan 28, 2022
e3ee5f1
show how specific bindings can be imported
ggreif Jan 28, 2022
968cadc
demonstrate renaming on import
ggreif Jan 28, 2022
9c911ca
it is `<pat>` now
ggreif Jan 28, 2022
2c3f4aa
Update doc/modules/language-guide/pages/overview.adoc
ggreif Jan 28, 2022
2503343
DRY
ggreif Jan 28, 2022
0e0b65a
Update src/docs/extract.ml
ggreif Jan 28, 2022
656b48c
Update doc/modules/language-guide/pages/language-manual.adoc
ggreif Jan 28, 2022
9dc287a
Update doc/modules/language-guide/pages/language-manual.adoc
ggreif Jan 28, 2022
c59e85a
Update doc/modules/language-guide/pages/language-manual.adoc
ggreif Jan 28, 2022
a2aeae4
Update src/ir_def/check_ir.ml
ggreif Jan 28, 2022
d0dff31
remove cruft
ggreif Jan 28, 2022
2d9b797
use the coverage checker to reject refutable import patterns
ggreif Jan 28, 2022
37b2838
Update doc/modules/language-guide/pages/language-manual.adoc
ggreif Jan 28, 2022
c763ff8
Update src/ir_def/check_ir.ml
ggreif Jan 28, 2022
feec77b
back
ggreif Jan 28, 2022
988c27f
forgotten
ggreif Jan 28, 2022
c98c798
add example
ggreif Jan 28, 2022
a3ac786
ooops
ggreif Jan 28, 2022
2ae3cbb
Update doc/modules/language-guide/pages/language-manual.adoc
ggreif Jan 28, 2022
3e8226c
Update language-manual.adoc
ggreif Jan 28, 2022
b5ed7d0
reindent
ggreif Jan 28, 2022
1ddcf93
tweak again
ggreif Jan 28, 2022
95a93a8
Merge branch 'master' into gabor/deconstruct-import
ggreif Jan 28, 2022
ca01c14
move an item
ggreif Jan 28, 2022
af6941a
demonstrate that actor methods cannot be imported explicitly
ggreif Jan 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/modules/language-guide/examples/grammar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@

<imp> ::=
'import' <id>? '='? <text>
'import' '{' <list(<pat_field>, ';')> '}' '=' <text>
Copy link
Contributor

@crusso crusso Jan 27, 2022

Choose a reason for hiding this comment

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

why not just <pat> = text?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed the syntax here ought to simply be <pat> (or <pat_nullary>).

Copy link
Contributor Author

@ggreif ggreif Jan 27, 2022

Choose a reason for hiding this comment

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

Doesn't that allow tuple patterns and all the other things (wildcard and literals)?
What would those even mean?
Or should we just delegate those to the type-checker (will error out) and accept _ as a NOP import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

You could insist the pattern is irrefutable, I think, as with shared function args.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, that's what I do. I have also added tests for it.

Copy link
Contributor Author

@ggreif ggreif Jan 28, 2022

Choose a reason for hiding this comment

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

@crusso the pattern matches arising from imports have this form

  (LetD (ObjP (cons (VarP cons)) (nil (VarP nil))) (VarE file$lib/ListM.mo))

but we have little control about the RHS (VarE file$lib/ListM.mo). What we could do is, when in check_ir, detect ids like file$* and run check (Ir_utils.is_irrefutable p) on the LHS pattern. Is this what you mean?

Well, we know a bit about the RHS, after all. It gets built in desugar.ml/transform_import and we can anticipate names. Would it be possible to have an IrrefutableLetD and insert that in transform_import?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@crusso what about 4c94e00?

Copy link
Contributor Author

@ggreif ggreif Jan 28, 2022

Choose a reason for hiding this comment

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

Okay now I reuse the coverage check from typing.ml but make it an error when its is happening in an error. PTAL! 2d9b797

Copy link
Contributor Author

Choose a reason for hiding this comment

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

JFTR, <pat> gives

      menhir mo_frontend/parser.{ml,mli} (exit 1)
(cd _build/default && /nix/store/ih98div9nccad7gjvksxwzhyl0jag1m7-ocaml4.12.0-menhir-20211012/bin/menhir --table --inspection -v --strict mo_frontend/parser.mly --base mo_frontend/parser)
Error: one state has shift/reduce conflicts.
Warning: one shift/reduce conflict was arbitrarily resolved.


<prog> ::=
<list(<imp>, ';')> <list(<dec>, ';')>
Expand Down
2 changes: 1 addition & 1 deletion src/docs/extract.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let un_prog prog =
let imports =
List.map
(fun i ->
let alias, path, _ = i.it in
let Syntax.Surface alias, path, _ = i.it in
(alias.it, path))
imports
in
Expand Down
6 changes: 4 additions & 2 deletions src/lowering/desugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ let link_declarations imports (cu, flavor) =


let transform_import (i : S.import) : import_declaration =
let (id, f, ir) = i.it in
let (mid, f, ir) = i.it in
let t = i.note in
assert (t <> T.Pre);
let rhs = match !ir with
Expand All @@ -869,7 +869,9 @@ let transform_import (i : S.import) : import_declaration =
varE (var (id_of_full_path "@prim") t)
| S.IDLPath (fp, canister_id) ->
primE (I.ActorOfIdBlob t) [blobE canister_id]
in [ letD (var id.it t) rhs ]
in [ match mid with
| Surface id -> letD (var id.it t) rhs
| Bulk id -> letD (var id.it t) (dotE rhs id.it T.Any(*FIXME*))]

let transform_unit_body (u : S.comp_unit_body) : Ir.comp_unit =
match u.it with
Expand Down
17 changes: 14 additions & 3 deletions src/mo_def/compUnit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ let comp_unit_of_prog as_lib (prog : prog) : comp_unit =
match ds with
(* imports *)
| {it = LetD ({it = VarP n; _}, ({it = ImportE (url, ri); _} as e)); _} :: ds' ->
let i : import = { it = (n, url, ri); note = e.note.note_typ; at = e.at } in
let i : import = { it = (Surface n, url, ri); note = e.note.note_typ; at = e.at } in
go (imports @ [i]) ds'
| {it = LetD ({it = ObjP []; _}, _); _} :: ds' ->
go imports ds'
| ({it = LetD (({it = ObjP (f :: fs); _} as letd), ({it = ImportE (url, ri); _} as es)); _} as imp) :: ds' ->
(*let e = { it = DotE (es, f.label); at = es.at; note = e.note.note_typ } in*)
let m, tfs = Type.as_obj es.note.note_typ in
assert (m = Type.Module);
let field_id = f.it.id in
let i : import = { it = (Bulk field_id, url, ri); note = Type.lookup_val_field field_id.it tfs; at = es.at } in
go (imports @ [i]) ({imp with it = LetD ({letd with it = ObjP fs}, es)} :: ds')

(* terminal expressions *)
| [{it = ExpD ({it = ObjBlockE ({it = Type.Module; _}, fields); _} as e); _}] when as_lib ->
Expand Down Expand Up @@ -88,9 +97,11 @@ let obj_decs obj_sort at note id_opt fields =
let decs_of_lib (cu : comp_unit) =
let open Source in
let { imports; body = cub; _ } = cu.it in
let import_decs = List.map (fun { it = (id, fp, ri); at; note} ->
let import_decs = List.map (fun { it = (mid, fp, ri); at; note} ->
{ it = LetD (
{ it = VarP id; at; note; },
{ it = (match mid with
| Surface id -> VarP id
| Bulk id -> ObjP [{it = { id; pat = { it = VarP id; at; note }}; at; note = ()}]); at; note },
{ it = ImportE (fp, ri);
at;
note = { note_typ = note; note_eff = Type.Triv} });
Expand Down
3 changes: 2 additions & 1 deletion src/mo_def/syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ and stab_sig' = (dec list * typ_field list) (* type declarations & stable a
(* Compilation units *)

type import = (import', Type.typ) Source.annotated_phrase
and import' = id * string * resolved_import ref
and import' = access * string * resolved_import ref
and access = Surface of id | Bulk of id

type comp_unit_body = (comp_unit_body', typ_note) Source.annotated_phrase
and comp_unit_body' =
Expand Down
2 changes: 2 additions & 0 deletions src/mo_frontend/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,8 @@ imp :
| IMPORT xf=id_opt EQ? f=TEXT
{ let _, x = xf "import" $sloc in
let_or_exp true x (ImportE (f, ref Unresolved)) (at $sloc) }
| IMPORT LCURLY fps=seplist(pat_field, semicolon) RCURLY EQ f=TEXT
{ LetD(ObjP(fps) @! at $sloc, ImportE (f, ref Unresolved) @? at $sloc) @? at $sloc }

start : (* dummy non-terminal to satisfy ErrorReporting.ml, that requires a non-empty parse stack *)
| (* empty *) { () }
Expand Down
5 changes: 5 additions & 0 deletions test/run/import-module-explicit.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { cons; nil } = "lib/ListM";

//type stack = List<Int>;
let s = cons(1, nil());
let u = cons<Int>(2, nil<Int>());
5 changes: 5 additions & 0 deletions test/run/ok/import-module-explicit.comp.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Ill-typed intermediate code after Desugaring (use -v to see dumped IR):
(unknown location): IR type error [M0000], subtype violation:
module {type List<T> = ?(T, List<T>); cons : <T>(T, List<T>) -> List<T>; nil : <T>() -> List<T>}
<T>(T, List<T>) -> List<T>

1 change: 1 addition & 0 deletions test/run/ok/import-module-explicit.comp.ret.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return code 1
8 changes: 8 additions & 0 deletions test/run/ok/import-module-explicit.diff-ir.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--- import-module-explicit.run
+++ import-module-explicit.run-ir
@@ -0,0 +1,5 @@
+Ill-typed intermediate code after Desugaring (use -v to see dumped IR):
+(unknown location): IR type error [M0000], subtype violation:
+ module {type List<T> = ?(T, List<T>); cons : <T>(T, List<T>) -> List<T>; nil : <T>() -> List<T>}
+ <T>(T, List<T>) -> List<T>
+
8 changes: 8 additions & 0 deletions test/run/ok/import-module-explicit.diff-low.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--- import-module-explicit.run
+++ import-module-explicit.run-low
@@ -0,0 +1,5 @@
+Ill-typed intermediate code after Desugaring (use -v to see dumped IR):
+(unknown location): IR type error [M0000], subtype violation:
+ module {type List<T> = ?(T, List<T>); cons : <T>(T, List<T>) -> List<T>; nil : <T>() -> List<T>}
+ <T>(T, List<T>) -> List<T>
+
5 changes: 5 additions & 0 deletions test/run/ok/import-module-explicit.run-ir.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Ill-typed intermediate code after Desugaring (use -v to see dumped IR):
(unknown location): IR type error [M0000], subtype violation:
module {type List<T> = ?(T, List<T>); cons : <T>(T, List<T>) -> List<T>; nil : <T>() -> List<T>}
<T>(T, List<T>) -> List<T>

1 change: 1 addition & 0 deletions test/run/ok/import-module-explicit.run-ir.ret.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return code 1
5 changes: 5 additions & 0 deletions test/run/ok/import-module-explicit.run-low.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Ill-typed intermediate code after Desugaring (use -v to see dumped IR):
(unknown location): IR type error [M0000], subtype violation:
module {type List<T> = ?(T, List<T>); cons : <T>(T, List<T>) -> List<T>; nil : <T>() -> List<T>}
<T>(T, List<T>) -> List<T>

1 change: 1 addition & 0 deletions test/run/ok/import-module-explicit.run-low.ret.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Return code 1