33open GraphBLAS.FSharp .Backend .Objects
44
55module ArithmeticOperations =
6- // unary
7- let inline optionUnOp zero unaryOp =
6+ let inline mkUnaryOp zero unaryOp =
87 <@ fun x ->
98 let mutable res = zero
109
@@ -14,104 +13,120 @@ module ArithmeticOperations =
1413
1514 if res = zero then None else Some res @>
1615
17- let inline addLeftConst zero constant =
18- optionUnOp zero <@ fun x -> constant + x @>
16+ let inline mkNumericSum zero =
17+ <@ fun ( x : 't option ) ( y : 't option ) ->
18+ let mutable res = zero
1919
20- let inline addRightConst zero constant =
21- optionUnOp zero <@ fun x -> x + constant @>
20+ match x, y with
21+ | Some f, Some s -> res <- f + s
22+ | Some f, None -> res <- f
23+ | None, Some s -> res <- s
24+ | None, None -> ()
2225
23- let inline mulLeftConst zero constant =
24- optionUnOp zero <@ fun x -> constant * x @>
26+ if res = zero then None else Some res @>
2527
26- let inline mulRightConst zero constant =
27- optionUnOp zero <@ fun x -> x * constant @>
28+ let inline mkNumericSumAtLeastOne zero =
29+ <@ fun ( values : AtLeastOne < 't , 't >) ->
30+ let mutable res = zero
31+
32+ match values with
33+ | Both ( f, s) -> res <- f + s
34+ | Left f -> res <- f
35+ | Right s -> res <- s
2836
29- // binary
37+ if res = zero then None else Some res @>
3038
31- let inline optionBinOpQ zero binOp =
32- <@ fun ( x : 'a option ) ( y : 'a option ) ->
39+ let inline mkNumericMul zero =
40+ <@ fun ( x : 't option ) ( y : 't option ) ->
3341 let mutable res = zero
3442
3543 match x, y with
36- | Some f, Some s -> res <- (% binOp) f s
37- | Some f, None -> res <- f
38- | None, Some s -> res <- s
39- | None, None -> ()
44+ | Some f, Some s -> res <- f * s
45+ | _ -> ()
4046
4147 if res = zero then None else Some res @>
4248
43- let inline optionBinOp zero binOp =
44- fun ( x : 'a option ) ( y : 'a option ) ->
49+ let inline mkNumericMulAtLeastOne zero =
50+ <@ fun ( values : AtLeastOne < 't , 't > ) ->
4551 let mutable res = zero
4652
53+ match values with
54+ | Both ( f, s) -> res <- f * s
55+ | _ -> ()
56+
57+ if res = zero then None else Some res @>
58+
59+ let boolSum =
60+ <@ fun ( x : bool option ) ( y : bool option ) ->
61+ let mutable res = false
62+
4763 match x, y with
48- | Some left, Some right -> res <- binOp left right
49- | Some left, None -> res <- left
50- | None, Some right -> res <- right
5164 | None, None -> ()
65+ | _ -> res <- true
5266
53- if res = zero then None else Some res
67+ if res then Some true else None @>
5468
55- let createOptionPair zero opQ op =
56- optionBinOpQ zero opQ, optionBinOp zero op
57-
58- let inline createOptionSumPair zero = createOptionPair zero <@ (+) @> (+)
69+ let inline addLeftConst zero constant =
70+ mkUnaryOp zero <@ fun x -> constant + x @>
5971
60- let intSumOption = createOptionSumPair 0
61- let byteSumOption = createOptionSumPair 0 uy
62- let floatSumOption = createOptionSumPair 0.0
63- let float32SumOption = createOptionSumPair 0 f
72+ let inline addRightConst zero constant =
73+ mkUnaryOp zero <@ fun x -> x + constant @>
6474
65- let boolSumOption = createOptionPair false <@ (||) @> (||)
75+ let intSumOption = mkNumericSum 0
76+ let byteSumOption = mkNumericSum 0 uy
77+ let floatSumOption = mkNumericSum 0.0
78+ let float32SumOption = mkNumericSum 0 f
6679
67- let inline createOptionMulPair zero = createOptionPair zero <@ (*) @> (*)
80+ let boolSumAtLeastOne =
81+ <@ fun ( _ : AtLeastOne < bool , bool >) -> Some true @>
6882
69- let intMulOption = createOptionMulPair 0
70- let byteMulOption = createOptionMulPair 0 uy
71- let floatMulOption = createOptionMulPair 0.0
72- let float32MulOption = createOptionMulPair 0 f
83+ let intSumAtLeastOne = mkNumericSumAtLeastOne 0
84+ let byteSumAtLeastOne = mkNumericSumAtLeastOne 0 uy
85+ let floatSumAtLeastOne = mkNumericSumAtLeastOne 0.0
86+ let float32SumAtLeastOne = mkNumericSumAtLeastOne 0 f
7387
74- let boolMulOption = createOptionPair true <@ (&&) @> (&&)
88+ let boolMulOption =
89+ <@ fun ( x : bool option ) ( y : bool option ) ->
90+ let mutable res = false
7591
76- let inline atLeastOneBinOpQ zero binOp =
77- Convert.optionToAtLeastOne <| optionBinOpQ zero binOp
92+ match x, y with
93+ | Some _, Some _ -> res <- true
94+ | _ -> ()
7895
79- let inline atLeastOneBinOp zero binOp =
80- let optionOp = optionBinOp zero binOp
81- // convert AtLeastOne -> Option
82- function
83- | Both ( left, right) -> optionOp ( Some left) ( Some right)
84- | Left left -> optionOp ( Some left) None
85- | Right right -> optionOp None ( Some right)
96+ if res then Some true else None @>
8697
87- let inline createAtLeastOnePair zero opQ op =
88- atLeastOneBinOpQ zero opQ , atLeastOneBinOp zero op
98+ let inline mulLeftConst zero constant =
99+ mkUnaryOp zero <@ fun x -> constant * x @>
89100
90- let inline createAtLeastOneSumPair zero = createAtLeastOnePair zero <@ (+) @> (+)
101+ let inline mulRightConst zero constant =
102+ mkUnaryOp zero <@ fun x -> x * constant @>
91103
92- let intSumAtLeastOne = createAtLeastOneSumPair 0
93- let byteSumAtLeastOne = createAtLeastOneSumPair 0 uy
94- let floatSumAtLeastOne = createAtLeastOneSumPair 0.0
95- let float32SumAtLeastOne = createAtLeastOneSumPair 0 f
104+ let intMulOption = mkNumericMul 0
105+ let byteMulOption = mkNumericMul 0 uy
106+ let floatMulOption = mkNumericMul 0.0
107+ let float32MulOption = mkNumericMul 0 f
96108
97- let boolSumAtLeastOne = createAtLeastOnePair false <@ (||) @> (||)
109+ let boolMulAtLeastOne =
110+ <@ fun ( values : AtLeastOne < bool , bool >) ->
111+ let mutable res = false
98112
99- let inline createAtLeastOneMulPair zero = createAtLeastOnePair zero <@ (*) @> (*)
113+ match values with
114+ | Both _ -> res <- true
115+ | _ -> ()
100116
101- let intMulAtLeastOne = createAtLeastOneMulPair 0
102- let byteMulAtLeastOne = createAtLeastOneMulPair 0 uy
103- let floatMulAtLeastOne = createAtLeastOneMulPair 0.0
104- let float32MulAtLeastOne = createAtLeastOneMulPair 0 f
117+ if res then Some true else None @>
105118
106- let boolMulAtLeastOne = createAtLeastOnePair true <@ (&&) @> (&&)
119+ let intMulAtLeastOne = mkNumericMulAtLeastOne 0
120+ let byteMulAtLeastOne = mkNumericMulAtLeastOne 0 uy
121+ let floatMulAtLeastOne = mkNumericMulAtLeastOne 0.0
122+ let float32MulAtLeastOne = mkNumericMulAtLeastOne 0 f
107123
108124 let notOption =
109125 <@ fun x ->
110126 match x with
111127 | Some true -> None
112128 | _ -> Some true @>
113129
114- // unwrapped operands
115130 let inline private binOpQ zero op =
116131 <@ fun ( left : 'a ) ( right : 'a ) ->
117132 let result = (% op) left right
0 commit comments