-
Notifications
You must be signed in to change notification settings - Fork 38
/
hashcode-staged.fs
203 lines (165 loc) · 6.29 KB
/
hashcode-staged.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
module HashCode_Staged
open FSharp.Quotations
open FSharp.Quotations.Evaluator
open Swensen.Unquote
open TypeShape.Core
open TypeShape.Core.StagingExtensions
// Staged hashcode generation
let mkStagedHasher<'T> (self : StagedGenerator1) (expr : Expr<'T>) : Expr<int> =
let unwrap () = unbox<Expr<'a>> expr
let genHash () = Expr.lam (fun e -> self.Generate<'a, int> e)
let combineHash (h1 : Expr<int>) (h2 : Expr<int>) =
<@ let h1 = %h1 in let h2 = %h2 in ((h1 <<< 5) + h1) ||| h2 @>
let stageMemberHash (shape : IShapeReadOnlyMember<'DeclaringType>) (dExpr : Expr<'DeclaringType>) =
shape.Accept { new IReadOnlyMemberVisitor<'DeclaringType, Expr<int>> with
member _.Visit (shape : ReadOnlyMember<'DeclaringType, 'FieldType>) =
let fExpr = shape.GetExpr dExpr
self.Generate fExpr }
match shapeof<'T> with
| Shape.Unit -> <@ 0 @>
| Shape.Bool -> <@ if (% unwrap()) then 1 else 0 @>
| Shape.Int32 -> <@ (% unwrap()) @>
| Shape.Double -> <@ hash<double> (% unwrap()) @>
| Shape.String -> <@ hash<string> (% unwrap()) @>
| Shape.Array s when s.Rank = 1 ->
s.Element.Accept { new ITypeVisitor<Expr<int>> with
member _.Visit<'t> () =
<@
match (% unwrap()) with
| null -> 0
| ts ->
let mutable agg = 0
for t in ts do
let th = (% genHash() ) t
agg <- (% Expr.lam2 combineHash) agg th
agg
@> }
| Shape.FSharpOption s ->
s.Element.Accept { new ITypeVisitor<Expr<int>> with
member _.Visit<'t> () =
<@
match (% (unwrap() : Expr<'t option>)) with
| None -> 0
| Some t ->
let th = (% genHash() ) t
(% Expr.lam2 combineHash) 1 th
@> }
| Shape.FSharpList s ->
s.Element.Accept { new ITypeVisitor<Expr<int>> with
member _.Visit<'t> () =
<@
let mutable agg = 0
let mutable ts = (% (unwrap() : Expr<'t list>))
while not(List.isEmpty ts) do
let th = (% genHash() ) (List.head ts)
agg <- (% Expr.lam2 combineHash) agg th
ts <- List.tail ts
agg
@> }
| Shape.Tuple (:? ShapeTuple<'T> as shape) ->
let mkElementHasher tuple =
shape.Elements
|> Array.map (fun e -> stageMemberHash e tuple)
|> Array.map (fun eh agg -> combineHash eh agg)
|> Expr.update ("agg", <@ 0 @>)
<@
let tuple = %expr
(% Expr.lam mkElementHasher) tuple
@>
| Shape.FSharpRecord (:? ShapeFSharpRecord<'T> as shape) ->
let mkFieldHasher record =
shape.Fields
|> Array.map (fun e -> stageMemberHash e record)
|> Array.map (fun eh agg -> combineHash eh agg)
|> Expr.update ("agg", <@ 0 @>)
<@
let record = %expr
(% Expr.lam mkFieldHasher) record
@>
| Shape.FSharpUnion (:? ShapeFSharpUnion<'T> as shape) ->
let stageUnionCaseHasher (union : Expr<'T>) (tag : Expr<int>)
(case : ShapeFSharpUnionCase<'T>) =
case.Fields
|> Array.map (fun c -> stageMemberHash c union)
|> Array.map (fun fh agg -> combineHash fh agg)
|> Expr.update ("agg", tag)
let stageUnionCaseHashers (u : Expr<'T>) (tag : Expr<int>) =
shape.UnionCases
|> Array.map (stageUnionCaseHasher u tag)
|> Expr.switch tag
<@
let union = %expr
let tag = (% Expr.lam shape.GetTagExpr) union
(% Expr.lam2 stageUnionCaseHashers) union tag
@>
| _ -> failwithf "Unsupported shape %O" typeof<'T>
// Compilation code
let mkHashCodeExpr<'T> () =
let F self =
{ new StagedGenerator1 with
member _.Generate<'T,'R> (e:Expr<'T>) : Expr<'R> =
mkStagedHasher self e |> unbox }
let gen = Expr.Y1 F
gen.Generate<'T, int>
|> Expr.lam
|> Expr.cleanup
let mkHasher<'T> () = mkHashCodeExpr<'T>() |> QuotationEvaluator.Evaluate
let decompileHasher<'T> () = mkHashCodeExpr<'T>() |> decompile
// examples
let hasher1 = mkHasher<int list * string option>()
hasher1 ([1 .. 100], Some "42")
decompileHasher<int * (string * bool)>()
//fun t ->
// let mutable agg = 0
// agg <- let h1 = t.m_Item1 in (h1 <<< 5) + h1 ||| agg
// agg <-
// let h1 =
// let tuple = t.m_Item2
// let mutable agg = 0
// agg <- let h1 = hash tuple.m_Item1 in (h1 <<< 5) + h1 ||| agg
// agg <- let h1 = if tuple.m_Item2 then 1 else 0 in (h1 <<< 5) + h1 ||| agg
// agg
// (h1 <<< 5) + h1 ||| agg
// agg
type Foo = { A : int ; B : string }
type Bar =
| UA
| UB of foo:string
| UC of Foo
let hasher2 = mkHasher<Bar list>()
hasher2 [UA ; UC { A = 12 ; B = "test" }; UB "string" ]
decompileHasher<Bar>()
//fun t ->
// let tag = t.Tag
// if tag = 0 then tag
// elif tag = 1 then
// let mutable agg = tag
// agg <- let h1 = hash t._foo in (h1 <<< 5) + h1 ||| agg
// agg
// elif tag = 2 then
// let mutable agg = tag
// agg <-
// let h1 =
// let record = t.item
// let mutable agg = 0
// agg <- let h1 = record.A@ in (h1 <<< 5) + h1 ||| agg
// agg <- let h1 = record.B@.GetHashCode() in (h1 <<< 5) + h1 ||| agg
// agg
// (h1 <<< 5) + h1 ||| agg
// agg
// else invalidOp "invalid tag"
type P = Z | S of P
let hasher3 = mkHasher<P>()
hasher3 (S (S (S (S (S Z)))))
decompileHasher<P> ()
//fun t ->
// let rec func t =
// let tag = t.Tag
// if tag = 0 then tag
// elif tag = 1 then
// let mutable agg = tag
// agg <- let h1 = func t.item in (h1 <<< 5) + h1 ||| agg
// agg
// else invalidOp "invalid tag"
//
// func t