Skip to content

Commit 6a4887d

Browse files
committed
wip
1 parent b3f821c commit 6a4887d

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/discrimination_tree.ml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,38 @@ module type DiscriminationTree = sig
4747
val retrieve_unifiables : 'a t -> keylist -> 'a list
4848
end
4949

50+
let arity_bits = 4
51+
let arity_mask = (1 lsl arity_bits) - 1
52+
let encode c a = (c lsl arity_bits) lor a
53+
let mask_low n = n land arity_mask
54+
5055
type cell =
51-
| Constant of int * int (* constant , arity *)
56+
| Constant of int (* (constant << arity_bits) lor arity *)
5257
| Primitive of int (*Elpi_util.Util.CData.t hash *)
5358
| Variable
5459
| Other
5560
[@@deriving show]
5661

62+
let mkConstant c a = Constant (encode c a)
63+
let mkVariable = Variable
64+
let mkOther = Other
65+
let mkPrimitive c = Primitive (Elpi_util.Util.CData.hash c)
66+
67+
let arity_of = function
68+
| Constant n -> mask_low n
69+
| Variable | Other | Primitive _ -> 0
70+
71+
5772
type path = cell list [@@deriving show]
5873

5974
let compare x y =
6075
match (x, y) with
61-
| Constant (x, _), Constant (y, _) -> x - y
76+
| Constant x, Constant y -> x - y
6277
| Variable, Variable -> 0
6378
| Other, Other -> 0
6479
| Primitive x, Primitive y -> x - y
6580
| _, _ -> compare x y
6681

67-
let arity_of = function
68-
| Constant (_, a) -> a
69-
| Variable | Other | Primitive _ -> 0
7082

7183
let skip (path : path) : path =
7284
let rec aux arity path =

src/runtime.ml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,26 +2460,26 @@ and arg_to_trie_path ~depth t path_depth : Discrimination_tree.path =
24602460
else
24612461
let path_depth = path_depth - 1 in
24622462
match deref_head ~depth t with
2463-
| Const k when k == Global_symbols.uvarc -> [Variable]
2464-
| Const k -> [Constant (k, 0)]
2465-
| CData d -> [Primitive (CData.hash d)]
2466-
| App (k,_,_) when k == Global_symbols.uvarc -> [Variable]
2463+
| Const k when k == Global_symbols.uvarc -> [mkVariable]
2464+
| Const k -> [mkConstant k 0]
2465+
| CData d -> [mkPrimitive d]
2466+
| App (k,_,_) when k == Global_symbols.uvarc -> [mkVariable]
24672467
| App (k,a,_) when k == Global_symbols.asc -> arg_to_trie_path ~depth a (path_depth+1)
2468-
| Nil -> [Constant(Global_symbols.nilc,0)]
2469-
| Lam _ -> [Other] (* loose indexing to enable eta *)
2470-
| Arg _ | UVar _ | AppArg _ | AppUVar _ | Discard -> [Other]
2468+
| Nil -> [mkConstant Global_symbols.nilc 0]
2469+
| Lam _ -> [mkOther] (* loose indexing to enable eta *)
2470+
| Arg _ | UVar _ | AppArg _ | AppUVar _ | Discard -> [mkOther]
24712471
| Builtin (k,tl) ->
24722472
let path = arg_to_trie_path_aux ~depth tl path_depth in
2473-
Constant (k, if path_depth = 0 then 0 else List.length tl) :: path
2473+
mkConstant k (if path_depth = 0 then 0 else List.length tl) :: path
24742474
| App (k, x, xs) ->
24752475
let arg_length = if path_depth = 0 then 0 else List.length xs + 1 in
24762476
let hd_path = arg_to_trie_path ~depth x path_depth in
24772477
let tl_path = arg_to_trie_path_aux ~depth xs path_depth in
2478-
Constant (k, arg_length) :: hd_path @ tl_path
2478+
mkConstant k arg_length :: hd_path @ tl_path
24792479
| Cons (x,xs) ->
24802480
let hd_path = arg_to_trie_path ~depth x path_depth in
24812481
let tl_path = arg_to_trie_path ~depth xs path_depth in
2482-
Constant (Global_symbols.consc, 2) :: hd_path @ tl_path
2482+
mkConstant Global_symbols.consc 2 :: hd_path @ tl_path
24832483

24842484
(**
24852485
[arg_to_trie_path ~path_depth ~depth t]

0 commit comments

Comments
 (0)