Skip to content

Commit

Permalink
fix: Separate variable/discard patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
vain0x committed Sep 23, 2019
1 parent f96ed9b commit 0258cf2
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 35 deletions.
1 change: 1 addition & 0 deletions boot/MiloneLang/ClosureConversion.fs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ let declosurePat (pat, ctx) =
match pat with
| HPat.Lit _
| HPat.Nil _
| HPat.Discard _
| HPat.Nav _ ->
pat, ctx
| HPat.Ref (serial, _, _) ->
Expand Down
5 changes: 5 additions & 0 deletions boot/MiloneLang/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ let rec patExtract (pat: HPat): Ty * Loc =
litToTy lit, a
| HPat.Nil (itemTy, a) ->
tyList itemTy, a
| HPat.Discard (ty, a) ->
ty, a
| HPat.Ref (_, ty, a) ->
ty, a
| HPat.Nav (_, _, ty, a) ->
Expand All @@ -558,6 +560,8 @@ let patMap (f: Ty -> Ty) (g: Loc -> Loc) (pat: HPat): HPat =
HPat.Lit (lit, g a)
| HPat.Nil (itemTy, a) ->
HPat.Nil (f itemTy, g a)
| HPat.Discard (ty, a) ->
HPat.Discard (f ty, g a)
| HPat.Ref (serial, ty, a) ->
HPat.Ref (serial, f ty, g a)
| HPat.Nav (pat, ident, ty, a) ->
Expand All @@ -582,6 +586,7 @@ let patNormalize pat =
let rec go pat =
match pat with
| HPat.Lit _
| HPat.Discard _
| HPat.Ref _
| HPat.Nil _ ->
[pat]
Expand Down
7 changes: 5 additions & 2 deletions boot/MiloneLang/Mir.fs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ let mirifyPatAs ctx endLabel pat serial expr loc =
/// Determines if the pattern covers the whole.
let mirifyPat ctx (endLabel: string) (pat: HPat) (expr: MExpr): bool * MirCtx =
match pat with
| HPat.Ref ("_", _, _, _) ->
// Discard result.
| HPat.Discard _ ->
// Discard the result, which we know is pure.
// FIXME: This should be done in optimization?
true, ctx

| HPat.Lit (lit, loc) ->
mirifyPatLit ctx endLabel lit expr loc
| HPat.Nil (itemTy, loc) ->
Expand Down Expand Up @@ -270,6 +272,7 @@ let mirifyBlock ctx expr =
let patsIsCovering pats =
let rec go pat =
match pat with
| HPat.Discard _
| HPat.Ref _ ->
// FIXME: unit-like variant patterns may not be covering
true
Expand Down
7 changes: 3 additions & 4 deletions boot/MiloneLang/NameRes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ let collectDecls (expr, ctx) =
let rec goPat (pat, ctx) =
match pat with
| HPat.Lit _
| HPat.Discard _
| HPat.Nav _
| HPat.Nil _ ->
pat, ctx
Expand Down Expand Up @@ -551,15 +552,13 @@ let primFromIdent ident =
let onPat (pat: HPat, ctx: ScopeCtx) =
match pat with
| HPat.Lit _
| HPat.Discard _
| HPat.Nil _ ->
pat, ctx

| HPat.Ref (varSerial, ty, loc)
when ctx |> scopeCtxGetIdent varSerial = "_" ->
// Handle discard pattern.
let varDef = VarDef.Var ("_", ty, loc)
let ctx = ctx |> scopeCtxDefineVar varSerial varDef
HPat.Ref (varSerial, ty, loc), ctx
HPat.Discard (ty, loc), ctx

| HPat.Ref (varSerial, ty, loc) ->
let ident = ctx |> scopeCtxGetIdent varSerial
Expand Down
5 changes: 4 additions & 1 deletion boot/MiloneLang/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,10 @@ type HPat =
/// `[]`
| Nil
of itemTy:Ty * Loc
/// Variable reference pattern or `_`.
/// `_`
| Discard
of Ty * Loc
/// Variable pattern.
| Ref
of VarSerial * Ty * Loc
| Nav
Expand Down
2 changes: 2 additions & 0 deletions boot/MiloneLang/Typing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ let inferPat ctx pat ty =
let itemTy, ctx = ctx |> ctxFreshPatTy pat
let ctx = unifyTy ctx loc ty (tyList itemTy)
HPat.Nil (itemTy, loc), ctx
| HPat.Discard (_, loc) ->
HPat.Discard (ty, loc), ctx
| HPat.Ref (varSerial, _, loc) ->
inferPatRef ctx varSerial loc ty
| HPat.Nav (l, r, _, loc) ->
Expand Down
4 changes: 2 additions & 2 deletions boot/tests/errors/ty_errors/ty_errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ end_match_4:;
#error 7:24 While unifying 'Con (Tuple,[Con (Tuple,[Con (Int,[]); Con (Int,[])]); Con (Int,[])])' and 'Con (Tuple,[Con (Tuple,[Con (Int,[]); Con (Str,[])]); Con (Int,[])])', failed to unify 'Con (Int,[])' and 'Con (Str,[])'.

#line 13
#error 13:17 Couldn't unify 'Meta (40,(12, 12))' and 'Con
(Fun,[Con (Fun,[Meta (40,(12, 12)); Meta (39,(12, 16))]); Meta (39,(12, 16))])' due to self recursion.
#error 13:17 Couldn't unify 'Meta (39,(12, 12))' and 'Con
(Fun,[Con (Fun,[Meta (39,(12, 12)); Meta (38,(12, 16))]); Meta (38,(12, 16))])' due to self recursion.

#line 15
#error 15:3 While unifying 'Con (Int,[])' and 'Con (Tuple,[])', failed to unify 'Con (Int,[])' and 'Con (Tuple,[])'.
40 changes: 20 additions & 20 deletions boot/tests/examples/MatchChecker/MatchChecker.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ struct IntSpace_Space_Tuple3;

struct Space_ fun_12(void* env_3, int arg_5, struct Space_Space_Tuple2 arg_6);

struct Space_ fun_28(struct Space_List* seconds_, struct Space_List* firsts_, struct String tag_2, struct Space_ spaceEmpty_, struct Space_ spaceFull_, int i_1, struct Space_ __14);
struct Space_ fun_28(struct Space_List* seconds_, struct Space_List* firsts_, struct String tag_2, struct Space_ spaceEmpty_, struct Space_ spaceFull_, int i_1, struct Space_ arg_51);

struct Space_ListSpace_ListStringSpace_Space_Tuple5;

Expand Down Expand Up @@ -220,7 +220,7 @@ struct Space_ fun_18(void* env_9, struct Ty_ arg_29);

struct Space_ fun_19(void* env_10, int arg_31);

struct Space_ thunk_2(struct Space_ itemSpace_, int arg_51);
struct Space_ thunk_2(struct Space_ itemSpace_, int arg_52);

struct Space_ fun_20(void* env_11, int arg_33);

Expand All @@ -238,27 +238,27 @@ struct Space_ fun_22(void* env_13, struct Pat_ arg_38);

struct Space_ patsToSpace_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, struct Pat_List* pats_);

struct Space_ fun_6(struct Space_ spaceFull_, int arg_52);
struct Space_ fun_6(struct Space_ spaceFull_, int arg_53);

struct Space_ fun_23(void* env_14, int arg_41);

int testSpaceIsEmpty_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, int arg_53);
int testSpaceIsEmpty_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, int arg_54);

int fun_24(void* env_15, int arg_43);

int fun_7(struct StringSpace_Tuple2 arg_54);
int fun_7(struct StringSpace_Tuple2 arg_55);

int fun_25(void* env_16, struct StringSpace_Tuple2 arg_44);

int testSpaceToString_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, int arg_55);
int testSpaceToString_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, int arg_56);

int fun_26(void* env_17, int arg_45);

struct BoolStringTuple2;

struct Covering_Covering_Tuple2;

int fun_8(struct Space_ spaceEmpty_, struct Space_ spaceFull_, struct StringTy_Pat_ListCovering_Tuple4 arg_56);
int fun_8(struct Space_ spaceEmpty_, struct Space_ spaceFull_, struct StringTy_Pat_ListCovering_Tuple4 arg_57);

int fun_27(void* env_18, struct StringTy_Pat_ListCovering_Tuple4 arg_46);

Expand Down Expand Up @@ -1236,7 +1236,7 @@ struct Space_ fun_12(void* env_3, int arg_5, struct Space_Space_Tuple2 arg_6) {
return call_67;
}

struct Space_ fun_28(struct Space_List* seconds_, struct Space_List* firsts_, struct String tag_2, struct Space_ spaceEmpty_, struct Space_ spaceFull_, int i_1, struct Space_ __14) {
struct Space_ fun_28(struct Space_List* seconds_, struct Space_List* firsts_, struct String tag_2, struct Space_ spaceEmpty_, struct Space_ spaceFull_, int i_1, struct Space_ arg_51) {
struct IntSpace_Space_Tuple3 tuple_4;
tuple_4.t0 = i_1;
tuple_4.t1 = spaceEmpty_;
Expand Down Expand Up @@ -1540,7 +1540,7 @@ struct Space_ fun_19(void* env_10, int arg_31) {
return call_113;
}

struct Space_ thunk_2(struct Space_ itemSpace_, int arg_51) {
struct Space_ thunk_2(struct Space_ itemSpace_, int arg_52) {
struct Space_ call_114 = spaceCtor_((struct String){.str = "nil", .len = 3}, NULL);
struct Space_Tuple1 tuple_10;
tuple_10.t0 = itemSpace_;
Expand Down Expand Up @@ -1698,7 +1698,7 @@ struct Space_ patsToSpace_(struct Space_ spaceEmpty_, struct Space_ spaceFull_,
return call_135;
}

struct Space_ fun_6(struct Space_ spaceFull_, int arg_52) {
struct Space_ fun_6(struct Space_ spaceFull_, int arg_53) {
return spaceFull_;
}

Expand All @@ -1708,7 +1708,7 @@ struct Space_ fun_23(void* env_14, int arg_41) {
return call_136;
}

int testSpaceIsEmpty_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, int arg_53) {
int testSpaceIsEmpty_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, int arg_54) {
int call_137 = spaceIsEmpty_(spaceEmpty_);
milone_assert(call_137, 455, 2);
int call_138 = 0;
Expand Down Expand Up @@ -1791,9 +1791,9 @@ int fun_24(void* env_15, int arg_43) {
return call_163;
}

int fun_7(struct StringSpace_Tuple2 arg_54) {
struct String expected_ = arg_54.t0;
struct Space_ space_10 = arg_54.t1;
int fun_7(struct StringSpace_Tuple2 arg_55) {
struct String expected_ = arg_55.t0;
struct Space_ space_10 = arg_55.t1;
struct String call_164 = spaceToString_(space_10);
struct String actual_ = call_164;
int match_33;
Expand All @@ -1817,7 +1817,7 @@ int fun_25(void* env_16, struct StringSpace_Tuple2 arg_44) {
return call_166;
}

int testSpaceToString_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, int arg_55) {
int testSpaceToString_(struct Space_ spaceEmpty_, struct Space_ spaceFull_, int arg_56) {
struct StringSpace_Tuple2 tuple_16;
tuple_16.t0 = (struct String){.str = "empty", .len = 5};
tuple_16.t1 = spaceEmpty_;
Expand Down Expand Up @@ -1913,11 +1913,11 @@ struct Covering_Covering_Tuple2 {
struct Covering_ t1;
};

int fun_8(struct Space_ spaceEmpty_, struct Space_ spaceFull_, struct StringTy_Pat_ListCovering_Tuple4 arg_56) {
struct String name_ = arg_56.t0;
struct Ty_ ty_2 = arg_56.t1;
struct Pat_List* pats_1 = arg_56.t2;
struct Covering_ covering_ = arg_56.t3;
int fun_8(struct Space_ spaceEmpty_, struct Space_ spaceFull_, struct StringTy_Pat_ListCovering_Tuple4 arg_57) {
struct String name_ = arg_57.t0;
struct Ty_ ty_2 = arg_57.t1;
struct Pat_List* pats_1 = arg_57.t2;
struct Covering_ covering_ = arg_57.t3;
struct Space_ call_177 = tyToSpace_(spaceFull_, ty_2);
struct Space_ tySpace_ = call_177;
struct Space_ call_178 = patsToSpace_(spaceEmpty_, spaceFull_, pats_1);
Expand Down
4 changes: 2 additions & 2 deletions boot/tests/features/union_complex/union_complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct First_Second_Tuple2;

struct First_Second_Tuple2List;

struct First_ListSecond_ListTuple2 f_(struct First_Second_Tuple2List* __2);
struct First_ListSecond_ListTuple2 f_(struct First_Second_Tuple2List* arg_2);

struct First_ListSecond_ListTuple2 fun_(void* env_, struct First_Second_Tuple2List* arg_);

Expand Down Expand Up @@ -86,7 +86,7 @@ struct First_Second_Tuple2List {
struct First_Second_Tuple2List* tail;
};

struct First_ListSecond_ListTuple2 f_(struct First_Second_Tuple2List* __2) {
struct First_ListSecond_ListTuple2 f_(struct First_Second_Tuple2List* arg_2) {
struct First_ListSecond_ListTuple2 tuple_;
tuple_.t0 = NULL;
tuple_.t1 = NULL;
Expand Down
8 changes: 4 additions & 4 deletions boot/tests/functions/str_esc/str_esc.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "milone.h"

int f_(char __);
int f_(char arg_);

int g_(struct String __1);
int g_(struct String arg_1);

int main();

int f_(char __) {
int f_(char arg_) {
return 0;
}

int g_(struct String __1) {
int g_(struct String arg_1) {
return 0;
}

Expand Down

0 comments on commit 0258cf2

Please sign in to comment.