Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 10 additions & 6 deletions src/ConETypeErase.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let rec tr_expr (e : S.expr) =
match e with
| EUnitPrf | EBoolPrf | EOptionPrf -> assert false

| ENum _ | ENum64 _ | EStr _ | EChr _ | EVar _ | EExtern _ ->
| ELit _ | EVar _ | EExtern _ ->
let^ v = tr_expr_v e in
T.EValue v

Expand Down Expand Up @@ -77,11 +77,7 @@ let rec tr_expr (e : S.expr) =
and tr_expr_v (e : S.expr) =
match e with
| EUnitPrf | EBoolPrf | EOptionPrf -> assert false

| ENum n -> return (T.VLit (LNum n))
| ENum64 n -> return (T.VLit (LNum64 n))
| EStr s -> return (T.VLit (LStr s))
| EChr c -> return (T.VLit (LNum (Char.code c)))
| ELit l -> tr_expr_lit l
| EVar x -> return (T.VVar x)

| EExtern(name, _) -> return (T.VExtern name)
Expand All @@ -105,6 +101,14 @@ and tr_expr_vs es =
let* vs = tr_expr_vs es in
return (v :: vs)

(** Translate a literal *)
and tr_expr_lit (l : S.literal) =
match l with
| ENum n -> return (T.VLit (LNum n))
| ENum64 n -> return (T.VLit (LNum64 n))
| EStr s -> return (T.VLit (LStr s))
| EChr c -> return (T.VLit (LNum (Char.code c)))

(** Translate a recursive definition *)
and tr_rec_def (rd : S.rec_def) =
(rd.rd_var, tr_expr rd.rd_body)
Expand Down
1 change: 1 addition & 0 deletions src/DblParser/Attributes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type attr_conf = {
let rec make_vis_pattern (pt : Lang.Surface.pattern) =
map_node begin function
| PWildcard -> PWildcard
| PLit l -> PLit l
| PId (_, ident) -> PId (true, ident)
| PAnnot (pt, scheme) -> PAnnot (make_vis_pattern pt, scheme)
| PCtor (pth, xs, ys) ->
Expand Down
16 changes: 8 additions & 8 deletions src/DblParser/Desugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,10 @@ let rec tr_pattern (p : Raw.expr) =
| EWildcard -> make PWildcard
| EUnit | ECtor _ | ESelect _ ->
make (PCtor(tr_ctor_pattern p, [], []))
| ENum _ -> Error.fatal (Error.desugar_error p.pos)
| ENum64 _ -> Error.fatal (Error.desugar_error p.pos)
| EStr _ -> Error.fatal (Error.desugar_error p.pos)
| EChr _ -> Error.fatal (Error.desugar_error p.pos)
| ENum n -> make (PLit (ENum n))
| ENum64 n -> make (PLit (ENum64 n))
| EStr s -> make (PLit (EStr s))
| EChr c -> make (PLit (EChr c))
| EInterp _ -> Error.fatal (Error.desugar_error p.pos)
| EParen p -> make (tr_pattern p).data
| EVar x -> make (PId(false, IdVar x))
Expand Down Expand Up @@ -546,10 +546,10 @@ and tr_expr (e : Raw.expr) =
| EParen e -> make (tr_expr e).data
| EUnit | EVar _ | EImplicit _ | ECtor _ | EMethod _ | EBOpID _ | EUOpID _ ->
make (EPoly(tr_poly_expr e, []))
| ENum n -> make (ENum n)
| ENum64 n -> make (ENum64 n)
| EStr s -> make (EStr s)
| EChr c -> make (EChr c)
| ENum n -> make (ELit(ENum n))
| ENum64 n -> make (ELit(ENum64 n))
| EStr s -> make (ELit(EStr s))
| EChr c -> make (ELit(EChr c))
| EInterp (s, xs) ->
let tr_toString (expr : Raw.expr) (fmt : Raw.expr option) =
let mth = { pos = expr.pos; data = (Raw.EMethod (expr, "toString"))} in
Expand Down
2 changes: 1 addition & 1 deletion src/DblParser/Import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ let define_module_path path =
let open Lang.Surface in
let make data = { pos = Position.nowhere; data } in
make (DLetId(false, IdImplicit "~__modulePath__",
make (PE_Expr (make (EStr path)))))
make (PE_Expr (make (ELit(EStr path))))))

let import_many imported imports =
let mk_mod_def (n, imports, (d : File.def_list)) =
Expand Down
33 changes: 18 additions & 15 deletions src/EffectInference/Expr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,24 @@ and infer_type : type ed.
T.Type.subst sub sch.sch_body,
return_pure eff_req )

| ENum n ->
let tp = T.Type.t_var (T.BuiltinType.tv_int) in
(T.ENum n, tp, return_pure eff_req)

| ENum64 n ->
let tp = T.Type.t_var (T.BuiltinType.tv_int64) in
(T.ENum64 n, tp, return_pure eff_req)

| EStr s ->
let tp = T.Type.t_var (T.BuiltinType.tv_string) in
(T.EStr s, tp, return_pure eff_req)

| EChr c ->
let tp = T.Type.t_var (T.BuiltinType.tv_char) in
(T.EChr c, tp, return_pure eff_req)
| ELit l ->
begin match l with
| ENum n ->
let tp = T.Type.t_var (T.BuiltinType.tv_int) in
(T.ELit(ENum n), tp, return_pure eff_req)

| ENum64 n ->
let tp = T.Type.t_var (T.BuiltinType.tv_int64) in
(T.ELit(ENum64 n), tp, return_pure eff_req)

| EStr s ->
let tp = T.Type.t_var (T.BuiltinType.tv_string) in
(T.ELit(EStr s), tp, return_pure eff_req)

| EChr c ->
let tp = T.Type.t_var (T.BuiltinType.tv_char) in
(T.ELit(EChr c), tp, return_pure eff_req)
end

| EFn(x, sch, body, _) ->
let sch = Type.tr_scheme_expr env sch in
Expand Down
3 changes: 1 addition & 2 deletions src/EffectInference/ExprUtils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ let mk_rec_ctx ~evs ~cs ~targs ~named all_defs =

let rec update_rec_body ~rec_ctx (e : T.expr) : T.expr =
match e with
| EUnitPrf | EBoolPrf | EOptionPrf | ENum _ | ENum64 _ | EStr _ | EChr _
| EExtern _ ->
| EUnitPrf | EBoolPrf | EOptionPrf | ELit _ | EExtern _ ->
e

| EVar x ->
Expand Down
34 changes: 32 additions & 2 deletions src/EffectInference/Pattern.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ end
type t =
| PWildcard
| PAs of t * T.var
| PLit of T.literal
| PCtor of
{ name : string;
idx : int;
Expand Down Expand Up @@ -105,7 +106,7 @@ let open_tvars env targs tvars =

let rec check_type env (pat : S.pattern) tp =
match pat.data with
| PWildcard | PAnnot _ | POr _ ->
| PWildcard | PLit _ | PAnnot _ | POr _ ->
check_scheme env pat (T.Scheme.of_type tp)

| PAs(pat, x) ->
Expand Down Expand Up @@ -186,7 +187,36 @@ and check_scheme env (pat : S.pattern) sch =
match pat.data with
| PWildcard -> (PWildcard, PEnv.empty)

| PAs(pat, x) ->
| PLit l ->
let check_lit_scheme lit_tp =
begin match T.Scheme.to_type sch with
| Some tp ->
begin match T.Type.view tp, T.Type.view lit_tp with
| TVar x, TVar y -> assert (T.TVar.equal x y)
| _, _ -> assert false
end
| None -> assert false
end
in
begin match l with
| ENum n ->
check_lit_scheme (T.Type.t_var T.BuiltinType.tv_int);
(PLit (ENum n), PEnv.empty)

| ENum64 n ->
check_lit_scheme (T.Type.t_var T.BuiltinType.tv_int64);
(PLit (ENum64 n), PEnv.empty)

| EStr s ->
check_lit_scheme (T.Type.t_var T.BuiltinType.tv_string);
(PLit (EStr s), PEnv.empty)

| EChr c ->
check_lit_scheme (T.Type.t_var T.BuiltinType.tv_char);
(PLit (EChr c), PEnv.empty)
end

| PAs(pat, x) ->
let (pat, penv) = check_scheme env pat sch in
(PAs(pat, x), PEnv.add_var penv x sch)

Expand Down
1 change: 1 addition & 0 deletions src/EffectInference/Pattern.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ end
type t =
| PWildcard
| PAs of t * T.var
| PLit of T.literal
| PCtor of
{ name : string;
idx : int;
Expand Down
107 changes: 106 additions & 1 deletion src/EffectInference/PatternMatch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ let drop_wildcard (cl : iclause) =
let rec simplify_head x (cl : iclause) =
match cl.c_patterns with
| [] -> assert false
| PAs(pat, y) :: pats ->
| (PWildcard | PLit _ | PCtor _) :: pats -> cl
| PAs(pat, y) :: pats ->
let cl =
{ cl with
c_patterns = pat :: pats;
Expand Down Expand Up @@ -119,6 +120,8 @@ let simplify_ctor idx (ctor : T.ctor_decl) tvs cl =
let pats2 = List.map (fun _ -> Pattern.PWildcard) ctor.ctor_arg_schemes in
Some { cl with c_patterns = pats1 @ pats2 @ pats }

| PLit _ :: _ -> assert false

| PCtor pc :: pats when pc.idx = idx ->
assert (List.length pc.tvars = List.length tvs);
assert (List.length pc.named = List.length ctor.ctor_named);
Expand Down Expand Up @@ -146,6 +149,10 @@ type column_class =
| CC_Wildcard
(** All patterns wild-cards *)

| CC_Lit of T.literal list
(** There is a literal pattern in the column. It stores the list of all
literals. *)

| CC_ADT of T.expr * T.ctor_decl list
(** There is a constructor pattern in the column. It stores computationally
irrelevant proof of the shape of the constructor and the list of all
Expand All @@ -160,6 +167,21 @@ let rec column_class (cls : iclause list) =
begin match cl.c_patterns with
| [] -> assert false
| PWildcard :: _ -> column_class cls
| PLit lit :: _ ->
let rec collect acc cls =
match cls with
| [] -> acc
| cl :: cls ->
(match cl.c_patterns with
| [] -> assert false
| PWildcard :: _ -> collect acc cls
| PLit l :: _ ->
if List.exists (fun x -> x = l) acc
then collect acc cls
else collect (l :: acc) cls
| (PCtor _ | PAs (_, _)) :: _ -> assert false)
in
CC_Lit (collect [lit] cls)
| PCtor cp :: _ -> CC_ADT(cp.proof, cp.ctors)
| PAs _ :: _ | POr _ :: _ ->
(* As-patterns and or-patterns should be already simplified *)
Expand Down Expand Up @@ -193,16 +215,99 @@ module Make(Ctx : MatchContext) = struct
cl.c_used := true;
make_body cl

<<<<<<< HEAD
<<<<<<< HEAD
| x :: xs, cls ->
let cls = normalize_head_patterns x cls in
=======
| x :: xs, cls ->
=======
| x :: xs, cls ->
>>>>>>> ae0f7b7 (added requested changes)
let cls = simplify_as_patterns x cls in
>>>>>>> c868a1f (Added pattern matching for literals)
begin match column_class cls with
| CC_Wildcard ->
tr_match (refocus ctx) xs (List.map drop_wildcard cls)

| CC_Lit(lits) ->
tr_match_lit ctx x xs cls lits

| CC_ADT(proof, ctors) ->
let match_cls = List.mapi (tr_match_clause ctx xs cls) ctors in
T.EMatch(proof, T.EVar x, match_cls, Ctx.res_tp, Ctx.res_eff)
end

and make_eq_type (lit: T.literal) =
let tp_lit =
match lit with
| ENum n -> T.Type.t_var T.BuiltinType.tv_int
| ENum64 n -> T.Type.t_var T.BuiltinType.tv_int64
| EStr s -> T.Type.t_var T.BuiltinType.tv_string
| EChr c -> T.Type.t_var T.BuiltinType.tv_char
in
let tp_bool = T.Type.t_var T.BuiltinType.tv_bool in
let sch_lit = T.Scheme.of_type tp_lit in
let inner = T.Type.t_arrow sch_lit tp_bool T.Pure
in
T.Type.t_arrow sch_lit inner T.Pure

and make_eq_expr (x : T.expr) (lit : T.literal) : T.expr =
let eq_tp = make_eq_type lit in
match lit with
| ENum n ->
T.EApp(T.EApp(T.EExtern("dbl_eqInt", eq_tp), x), T.ELit(ENum n))
| ENum64 n ->
T.EApp(T.EApp(T.EExtern("dbl_eqInt64", eq_tp), x), T.ELit(ENum64 n))
| EStr s ->
T.EApp(T.EApp(T.EExtern("dbl_eqStr", eq_tp), x), T.ELit(EStr s))
| EChr c ->
T.EApp(T.EApp(T.EExtern("dbl_eqInt", eq_tp), x), T.ELit(EChr c))

and make_eq_match x lit then_e else_e =
let cond = make_eq_expr(T.EVar x) lit in
let cl_true =
{ T.cl_tvars = [];
T.cl_vars = [];
T.cl_body = then_e
} in
let cl_false =
{ T.cl_tvars = [];
T.cl_vars = [];
T.cl_body = else_e
} in
T.EMatch(T.EBoolPrf, cond, [cl_false; cl_true], Ctx.res_tp, Ctx.res_eff)

(** Build a match clause for a literal *)
and tr_match_lit ctx x xs cls lits =
let is_default cl =
match cl.c_patterns with
| (PWildcard | PAs _) :: _ -> true
| _ -> false
in
let drop cl =
match cl.c_patterns with
| _ :: pats -> { cl with c_patterns = pats }
| [] -> assert false
in
let default_cls = List.filter is_default cls in
let default_branch =
match default_cls with
| [] -> Error.fatal (Error.non_exhaustive_match ~pos:Ctx.pos ctx)
| _ -> tr_match (refocus ctx) xs (List.map drop default_cls)
in
List.fold_right (fun lit acc ->
let cls_lit =
List.filter (fun cl ->
match cl.c_patterns with
| PLit l :: _ -> (l = lit)
| PWildcard :: _ -> true
| _ -> false)
cls
in
let branch = tr_match (refocus ctx) xs (List.map drop cls_lit) in
make_eq_match x lit branch acc)
lits default_branch

(** Build a match clause for a single constructor. *)
and tr_match_clause ctx xs cls idx (ctor : T.ctor_decl) =
Expand Down
20 changes: 9 additions & 11 deletions src/Lang/ConE.mli
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,8 @@ type expr =
| EOptionPrf
(** ADT-shape proof for option type *)

| ENum of int
(** Integer literal *)

| ENum64 of int64
(** 64 bit integer literal *)

| EStr of string
(** String literal *)

| EChr of char
(** Character literal *)
| ELit of literal
(** Literal *)

| EVar of var
(** Variable *)
Expand Down Expand Up @@ -239,6 +230,13 @@ and match_clause =
(** Body of the clause *)
}

(** Literals for patterns *)
and literal =
| ENum of int
| ENum64 of int64
| EStr of string
| EChr of char

(** Programs *)
type program = expr

Expand Down
Loading
Loading