Skip to content

Commit 31b1fa7

Browse files
committed
compiler: cleanup
1 parent 3ef2a4c commit 31b1fa7

File tree

7 files changed

+148
-118
lines changed

7 files changed

+148
-118
lines changed

src/compiler/compiler.ml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -747,11 +747,11 @@ module CustomFunctorCompilation = struct
747747

748748
let scope_singlequote ~loc state x =
749749
match State.get singlequote state with
750-
| None -> ScopedTerm.(App(ScopedTerm.mk_global_const ~name:x ~loc,[]))
750+
| None -> ScopedTerm.(App(ScopedTerm.mk_global_const x ~loc,[]))
751751
| Some (language,f) -> ScopedTerm.unlock @@ ScopedTerm.of_simple_term_loc @@ f ~language state loc (F.show x)
752752
let scope_backtick ~loc state x =
753753
match State.get backtick state with
754-
| None -> ScopedTerm.(App(ScopedTerm.mk_global_const ~name:x ~loc,[]))
754+
| None -> ScopedTerm.(App(ScopedTerm.mk_global_const x ~loc,[]))
755755
| Some (language,f) -> ScopedTerm.unlock @@ ScopedTerm.of_simple_term_loc @@ f ~language state loc (F.show x)
756756
end
757757

@@ -887,11 +887,11 @@ end = struct
887887
scope_term_macro ~loc ~state c []
888888
| Const c when F.Set.mem c ctx -> ScopedTerm.(App(ScopedTerm.mk_bound_const ~lang:elpi_language c ~loc,[]))
889889
| Const c ->
890-
if is_uvar_name c then ScopedTerm.Var(ScopedTerm.mk_bound_const ~lang:elpi_var c ~loc,[])
890+
if is_uvar_name c then ScopedTerm.UVar(ScopedTerm.mk_uvar c ~loc,[])
891891
else if CustomFunctorCompilation.is_singlequote c then CustomFunctorCompilation.scope_singlequote ~loc state c
892892
else if CustomFunctorCompilation.is_backtick c then CustomFunctorCompilation.scope_backtick ~loc state c
893-
else if is_global c then ScopedTerm.(App(mk_const ~scope:(Scope.mkGlobal ~escape_ns:true ()) (of_global c) ~loc,[]))
894-
else ScopedTerm.(App(mk_const ~scope:(Scope.mkGlobal ()) c ~loc,[]))
893+
else if is_global c then ScopedTerm.(App(mk_global_const ~escape_ns:true (of_global c) ~loc,[]))
894+
else ScopedTerm.(App(mk_global_const ~loc c,[]))
895895
| App ({ it = App (f,l1) },l2) -> scope_term ~state ctx ~loc (App(f, l1 @ l2))
896896
| App ({ it = Parens f },l) -> scope_term ~state ctx ~loc (App(f, l))
897897
| App({ it = Const c }, [x]) when F.equal c F.spillf ->
@@ -913,9 +913,9 @@ end = struct
913913
else
914914
let bound = F.Set.mem c ctx in
915915
if bound then ScopedTerm.App(ScopedTerm.mk_bound_const ~lang:elpi_language c ~loc:cloc, xs)
916-
else if is_uvar_name c then ScopedTerm.Var(ScopedTerm.mk_bound_const ~lang:elpi_var c ~loc:cloc,xs)
917-
else if is_global c then ScopedTerm.App(ScopedTerm.mk_const ~scope:(Scope.mkGlobal ~escape_ns:true ()) (of_global c) ~loc:cloc,xs)
918-
else ScopedTerm.App(ScopedTerm.mk_const ~scope:(Scope.mkGlobal ()) c ~loc:cloc, xs)
916+
else if is_uvar_name c then ScopedTerm.UVar(ScopedTerm.mk_uvar c ~loc:cloc,xs)
917+
else if is_global c then ScopedTerm.App(ScopedTerm.mk_global_const ~escape_ns:true (of_global c) ~loc:cloc,xs)
918+
else ScopedTerm.App(ScopedTerm.mk_global_const c ~loc:cloc, xs)
919919
| Cast (t,ty) ->
920920
let t = scope_loc_term ~state ctx t in
921921
let ty = scope_loc_tye F.Set.empty (RecoverStructure.structure_type_expression ty.Ast.TypeExpression.tloc Ast.Structured.Relation valid_functional ty) in
@@ -926,7 +926,7 @@ end = struct
926926
| Lam (c,cloc,ty,b) ->
927927
if has_dot c then error ~loc "Bound variables cannot contain the namespaec separator '.'";
928928
let ty = ty |> Option.map (fun ty -> scope_loc_tye F.Set.empty (RecoverStructure.structure_type_expression ty.Ast.TypeExpression.tloc Ast.Structured.Relation valid_functional ty)) in
929-
let name = Some (ScopedTerm.mk_const ~scope:elpi_language c ~loc:cloc) in
929+
let name = Some (ScopedTerm.mk_binder ~lang:elpi_language c ~loc:cloc) in
930930
ScopedTerm.Lam (name,ty,scope_loc_term ~state (F.Set.add c ctx) b)
931931
| CData c -> ScopedTerm.CData c (* CData.hcons *)
932932
| App ({ it = Lam _},_) ->
@@ -1173,9 +1173,9 @@ module Flatten : sig
11731173
let b' = aux_loc b in
11741174
let ty' = option_smart_map (ScopedTypeExpression.smart_map_scoped_loc_ty tyf) ty in
11751175
if b == b' && ty' == ty then it else Lam(n,ty',b')
1176-
| Var(c,l) ->
1176+
| UVar(c,l) ->
11771177
let l' = smart_map aux_loc l in
1178-
if l == l' then it else Var(c,l')
1178+
if l == l' then it else UVar(c,l')
11791179
| Cast(t,ty) ->
11801180
let t' = aux_loc t in
11811181
let ty' = ScopedTypeExpression.smart_map_scoped_loc_ty tyf ty in
@@ -1642,7 +1642,7 @@ end = struct
16421642
let allocate_global_symbol = allocate_global_symbol types symb state in
16431643
let push_bound (n,ctx) c = (n+1,Scope.Map.add c n ctx) in
16441644
let push_unnamed_bound (n,ctx) = (n+1,ctx) in
1645-
let push ctx : string ScopedTerm.const option -> 'a = function
1645+
let push ctx : ScopedTerm.binder option -> 'a = function
16461646
| None -> push_unnamed_bound ctx
16471647
| Some { scope = l; name = x } -> push_bound ctx (x,l) in
16481648
let open ScopedTerm in
@@ -1690,7 +1690,7 @@ end = struct
16901690
let xs = List.map (todbl ctx) xs in
16911691
D.mkApp c x xs
16921692
(* holes *)
1693-
| Var({ name = c },xs) ->
1693+
| UVar({ name = c },xs) ->
16941694
let xs = List.map (todbl ctx) xs in
16951695
R.mkAppArg (allocate_arg c) 0 xs
16961696
| Discard -> D.mkDiscard
@@ -2560,7 +2560,7 @@ let info_of_scoped_term ~types t =
25602560
let rec aux loc ty = function
25612561
| Impl(_,locs,l,r) -> log_bsymb locs Global_symbols.impl; log_ty loc ty; aux_loc l; aux_loc r
25622562
| Discard -> log_ty loc ty
2563-
| Var({ scope = s; ty = tys; loc = locs},args) -> if args <> [] then log_ty loc ty; log_symb locs s (TypeAssignment.deref_opt tys); List.iter aux_loc args
2563+
| UVar({ scope = s; ty = tys; loc = locs},args) -> if args <> [] then log_ty loc ty; log_symb locs (Scope.Bound elpi_var) (TypeAssignment.deref_opt tys); List.iter aux_loc args
25642564
| App({ scope = s; ty = tys; loc = locs},args) -> if args <> [] then log_ty loc ty; log_symb locs s (TypeAssignment.deref_opt tys); List.iter aux_loc args
25652565
| CData _ -> log_ty loc ty
25662566
| Spill(t,_) -> log_ty loc ty; aux_loc t

src/compiler/compiler_data.ml

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -909,17 +909,21 @@ module ScopedTerm = struct
909909

910910
end
911911

912-
type 'scope const = { scope: 'scope; name: F.t; ty: TypeAssignment.t MutableOnce.t; loc : Loc.t }
913-
[@@ deriving show]
914-
915-
let mk_const ?(ty = MutableOnce.make F.dummyname) ~scope name ~loc : 'a const = { scope; name; ty; loc }
916-
let mk_bound_const ?ty ~lang name ~loc = mk_const ?ty ~scope:(Scope.Bound lang) name ~loc
917-
918-
let bind_const (n : string const) : Scope.t const = { n with scope = Scope.Bound n.scope }
919-
920-
let mk_global_const ~name ~loc : 'a const = mk_const ~scope:(Scope.mkGlobal ()) name ~loc
921-
let const_of_symb ~types symb ~loc : 'a const = mk_const ~scope:(Scope.mkResolvedGlobal types symb) (Symbol.get_func symb) ~loc
922-
let clone_const ?(clone_scope = Fun.id) {scope;name; loc } = mk_const ~scope:(clone_scope scope) name ~loc
912+
type 'scope w_ty_loc = { scope: 'scope; name: F.t; ty: TypeAssignment.t MutableOnce.t; loc : Loc.t } [@@ deriving show]
913+
type const = Scope.t w_ty_loc [@@ deriving show]
914+
type uvar = unit w_ty_loc [@@ deriving show]
915+
type binder = string w_ty_loc [@@ deriving show]
916+
917+
let mk_w_ty_loc ?(ty = MutableOnce.make F.dummyname) ~scope name ~loc : 'a w_ty_loc =
918+
{ scope; name; ty; loc }
919+
let mk_global_const ?escape_ns ~loc name : const = mk_w_ty_loc ~scope:(Scope.mkGlobal ?escape_ns ()) name ~loc
920+
let mk_bound_const ?ty ~lang name ~loc = mk_w_ty_loc ?ty ~scope:(Scope.Bound lang) name ~loc
921+
let mk_uvar ?ty name ~loc = mk_w_ty_loc ?ty ~scope:() name ~loc
922+
let mk_binder ?ty ~lang name ~loc = mk_w_ty_loc ?ty ~scope:lang ~loc name
923+
let bind_const (n : binder) : const = { n with scope = Scope.Bound n.scope }
924+
925+
let const_of_symb ~types symb ~loc : const = mk_w_ty_loc ~scope:(Scope.mkResolvedGlobal types symb) (Symbol.get_func symb) ~loc
926+
let clone_const ?(clone_scope = Fun.id) {scope;name; loc } = mk_w_ty_loc ~scope:(clone_scope scope) name ~loc
923927

924928
type spill_info =
925929
| NoInfo (* before typing *)
@@ -930,9 +934,9 @@ module ScopedTerm = struct
930934
type t_ =
931935
| Impl of SimpleTerm.impl_kind * Loc.t * t * t (* `Impl(true,t1,t2)` ≡ `t1 => t2` and `Impl(false,t1,t2)` ≡ `t1 :- t2` *)
932936
| Discard
933-
| Var of Scope.t const * t list
934-
| App of Scope.t const * t list
935-
| Lam of (Scope.language const) option * ScopedTypeExpression.e option * t
937+
| UVar of uvar * t list
938+
| App of const * t list
939+
| Lam of binder option * ScopedTypeExpression.e option * t
936940

937941
| CData of CData.t
938942
| Spill of t * spill_info ref
@@ -949,14 +953,14 @@ module ScopedTerm = struct
949953

950954
let lvl_of = function
951955
| App(_, (_::_)) -> app
952-
| Var (_, (_::_)) -> app
956+
| UVar (_, (_::_)) -> app
953957
| Lam _ -> lam
954958
| _ -> 2
955959

956960
let get_lam_name = function None -> F.from_string "_" | Some (n,_) -> n
957961
let mk_empty_lam_type = function
958962
| None -> None
959-
| Some (name, loc, scope) -> Some (mk_const name ~scope ~loc)
963+
| Some (name, loc, scope) -> Some (mk_w_ty_loc name ~scope ~loc)
960964

961965
(* The type of the object being constructed is irrelevant since
962966
build_infix_constant is used in the pretty printer of term and the type
@@ -994,8 +998,8 @@ module ScopedTerm = struct
994998
| App({ name = f },x::xs) when F.equal F.pif f || F.equal F.sigmaf f -> fprintf fmt "@[<hov 2>%a@ %a@]" F.pp f (Util.pplist ~pplastelem:(pretty_parens_lam ~lvl:app) (pretty_parens ~lvl:app) " ") (x::xs)
995999
| App({ scope = Global _; name = f } as n,x::xs) when is_infix_constant f -> fprintf fmt "%a" (Util.pplist ~boxed:true (pretty_parens ~lvl:0) " ") (intersperse (build_infix_constant n) (x::xs))
9961000
| App({ name = f },x::xs) -> fprintf fmt "@[<hov 2>%a@ %a@]" F.pp f (Util.pplist ~boxed:true (pretty_parens ~lvl:app) " ") (x::xs)
997-
| Var({ name = f },[]) -> fprintf fmt "@[%a@]" F.pp f
998-
| Var({ name = f },xs) -> fprintf fmt "@[%a@ %a@]" F.pp f (Util.pplist ~boxed:true (pretty_parens ~lvl:app) " ") xs
1001+
| UVar({ name = f },[]) -> fprintf fmt "@[%a@]" F.pp f
1002+
| UVar({ name = f },xs) -> fprintf fmt "@[%a@ %a@]" F.pp f (Util.pplist ~boxed:true (pretty_parens ~lvl:app) " ") xs
9991003
| CData c -> fprintf fmt "%a" CData.pp c
10001004
| Spill (t,{ contents = NoInfo }) -> fprintf fmt "{%a}" pretty t
10011005
| Spill (t,{ contents = Main _ }) -> fprintf fmt "{%a}" pretty t
@@ -1011,7 +1015,7 @@ module ScopedTerm = struct
10111015
let rec eq ctx t1 t2 =
10121016
match t1.it, t2.it with
10131017
| Discard, Discard -> true
1014-
| Var(n1,l1), Var(n2,l2) -> eq_uvar ctx n1.name n2.name && Util.for_all2 (eq ctx) l1 l2
1018+
| UVar(n1,l1), UVar(n2,l2) -> eq_uvar ctx n1.name n2.name && Util.for_all2 (eq ctx) l1 l2
10151019
| App({ scope = Global _ as b1; name = c1},xs), App({ scope = Global _ as b2; name = c2 },ys) -> Scope.equal env b1 b2 && F.equal c1 c2 && Util.for_all2 (eq ctx) xs ys
10161020
| App({ scope = Bound l1; name = c1 },xs), App({ scope = Bound l2; name = c2 },ys) -> l1 = l2 && eq_var ctx l1 c1 c2 && Util.for_all2 (eq ctx) xs ys
10171021
| Lam(None, ty1,b1), Lam (None,ty2, b2) -> eq ctx b1 b2 && (not types || Option.equal (ScopedTypeExpression.eqt env (empty ())) ty1 ty2)
@@ -1041,7 +1045,7 @@ module ScopedTerm = struct
10411045
let rec of_simple_term ~loc = function
10421046
| SimpleTerm.Discard -> Discard
10431047
| Impl(b,loc,t1,t2) -> Impl(b,loc,of_simple_term_loc t1, of_simple_term_loc t2)
1044-
| Const(scope,c) -> App (mk_const ~scope c ~loc,[])
1048+
| Const(scope,c) -> App (mk_w_ty_loc ~scope c ~loc,[])
10451049
| Opaque c -> CData c
10461050
| Cast(t,ty) -> Cast(of_simple_term_loc t, ScopedTypeExpression.of_simple_type_loc ty)
10471051
| Lam(c,ty,t) -> Lam(mk_empty_lam_type c,Option.map ScopedTypeExpression.of_simple_type_loc ty, of_simple_term_loc t)
@@ -1050,8 +1054,8 @@ module ScopedTerm = struct
10501054
| [y] -> Impl(SimpleTerm.func_to_impl_kind c,cloc,of_simple_term_loc x, of_simple_term_loc y)
10511055
| _ -> error ~loc "Use of App for Impl is allowed, but the length of the list in 3rd position must be 1"
10521056
end
1053-
| App(s,c,cloc,x,xs) -> App(mk_const ~scope:s c ~loc, of_simple_term_loc x :: List.map of_simple_term_loc xs)
1054-
| Var(c,cloc,xs) -> Var(mk_bound_const ~lang:elpi_var c ~loc:cloc,List.map of_simple_term_loc xs)
1057+
| App(s,c,cloc,x,xs) -> App(mk_w_ty_loc ~scope:s c ~loc, of_simple_term_loc x :: List.map of_simple_term_loc xs)
1058+
| Var(c,cloc,xs) -> UVar(mk_uvar c ~loc:cloc,List.map of_simple_term_loc xs)
10551059
and of_simple_term_loc { SimpleTerm.it; loc } =
10561060
match it with
10571061
| Opaque c when is_scoped_term c -> out_scoped_term c
@@ -1074,7 +1078,7 @@ module ScopedTerm = struct
10741078
| Lam(v,tya,t) -> Lam(v,tya,rename_loc l c d t)
10751079
| Spill(t,i) -> Spill(rename_loc l c d t,i)
10761080
| Cast(t,ty) -> Cast(rename_loc l c d t,ty)
1077-
| Var(v,xs) -> Var(v,List.map (rename_loc l c d) xs)
1081+
| UVar(v,xs) -> UVar(v,List.map (rename_loc l c d) xs)
10781082
| Discard | CData _ -> t
10791083
and rename_loc l c d { it; ty; loc } = { it = rename l c d it; ty; loc }
10801084

@@ -1083,7 +1087,7 @@ module ScopedTerm = struct
10831087
| Impl (b, loc, l, r) -> Impl(b, loc, clone_loc ~loc l, clone_loc ~loc r)
10841088
| Lam (n,ty,bo) -> Lam(Option.map clone_const n, ty, clone_loc ~loc bo)
10851089
| Discard -> Discard
1086-
| Var (v, xs) -> Var (clone_const v, List.map (clone_loc ~loc) xs)
1090+
| UVar (v, xs) -> UVar (clone_const v, List.map (clone_loc ~loc) xs)
10871091
| App (g, xs) -> App (clone_const g, List.map (clone_loc ~loc) xs)
10881092
| CData _ as t -> t
10891093
| Spill (t, _) -> Spill (clone_loc ~loc t, ref NoInfo)
@@ -1093,7 +1097,7 @@ module ScopedTerm = struct
10931097
let rec fv acc { it } =
10941098
match it with
10951099
| Impl(_,_,a,b) -> List.fold_left fv acc [a;b]
1096-
| Var (_,args) -> List.fold_left fv acc args
1100+
| UVar (_,args) -> List.fold_left fv acc args
10971101
| App({ scope = Bound l; name = c },xs) -> List.fold_left fv (Scope.Set.add (c,l) acc) xs
10981102
| App({ scope = Global _ },xs) -> List.fold_left fv acc xs
10991103
| Lam(None,_,t) -> fv acc t
@@ -1121,7 +1125,7 @@ module ScopedTerm = struct
11211125
let hd = Scope.Map.find (c,l) map in
11221126
unlock @@ app_loc hd (List.map (subst_loc map fv) xs)
11231127
| App(n,xs) -> App(n,List.map (subst_loc map fv) xs)
1124-
| Var(c,xs) -> Var(c,List.map (subst_loc map fv) xs)
1128+
| UVar(c,xs) -> UVar(c,List.map (subst_loc map fv) xs)
11251129
| Spill(t,i) -> Spill(subst_loc map fv t,i)
11261130
| Cast(t,ty) -> Cast(subst_loc map fv t,ty)
11271131
| Discard | CData _ -> t
@@ -1131,7 +1135,7 @@ module ScopedTerm = struct
11311135
if args = [] then t else
11321136
match t with
11331137
| App(n,xs) -> App(n,xs @ args)
1134-
| Var(c,xs) -> Var(c,xs @ args)
1138+
| UVar(c,xs) -> UVar(c,xs @ args)
11351139
| Impl(_,_,_,_) -> error ~loc "cannot apply impl"
11361140
| CData _ -> error ~loc "cannot apply cdata"
11371141
| Spill _ -> error ~loc "cannot apply spill"
@@ -1158,7 +1162,7 @@ module ScopedTerm = struct
11581162
match it with
11591163
| SimpleTerm.Opaque o when is_scoped_term o ->
11601164
begin match out_scoped_term o with
1161-
| { it = Var(f,xs); loc = loc'; ty } -> { SimpleTerm.loc; it = SimpleTerm.Opaque (in_scoped_term @@ { it = Var(f,xs @ l); loc = loc'; ty }) }
1165+
| { it = UVar(f,xs); loc = loc'; ty } -> { SimpleTerm.loc; it = SimpleTerm.Opaque (in_scoped_term @@ { it = UVar(f,xs @ l); loc = loc'; ty }) }
11621166
| { it = App({ scope = Bound g } as n,[]); loc = loc'; ty } when g = elpi_language ->
11631167
{ SimpleTerm.loc; it = SimpleTerm.Opaque (in_scoped_term @@ { it = App(n, l); loc = loc'; ty }) }
11641168
| x -> anomaly ~loc (Format.asprintf "The term is not an elpi varible coming from a quotation: @[%a@]" pretty x)
@@ -1189,7 +1193,7 @@ module ScopedTerm = struct
11891193
| _ -> false
11901194
end
11911195

1192-
let is_var = function Var _ -> true | _ -> false
1196+
let is_var = function UVar _ -> true | _ -> false
11931197
end
11941198

11951199

0 commit comments

Comments
 (0)