33open GraphBLAS.FSharp .Backend .Objects
44
55module ArithmeticOperations =
6- let inline mkUnaryOp zero unaryOp =
6+ // unary
7+ let inline optionUnOp zero unaryOp =
78 <@ fun x ->
89 let mutable res = zero
910
@@ -13,120 +14,104 @@ module ArithmeticOperations =
1314
1415 if res = zero then None else Some res @>
1516
16- let inline mkNumericSum zero =
17- <@ fun ( x : 't option ) ( y : 't option ) ->
18- let mutable res = zero
19-
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 -> ()
17+ let inline addLeftConst zero constant =
18+ optionUnOp zero <@ fun x -> constant + x @>
2519
26- if res = zero then None else Some res @>
20+ let inline addRightConst zero constant =
21+ optionUnOp zero <@ fun x -> x + constant @>
2722
28- let inline mkNumericSumAtLeastOne zero =
29- <@ fun ( values : AtLeastOne < 't , 't >) ->
30- let mutable res = zero
23+ let inline mulLeftConst zero constant =
24+ optionUnOp zero <@ fun x -> constant * x @>
3125
32- match values with
33- | Both ( f, s) -> res <- f + s
34- | Left f -> res <- f
35- | Right s -> res <- s
26+ let inline mulRightConst zero constant =
27+ optionUnOp zero <@ fun x -> x * constant @>
3628
37- if res = zero then None else Some res @>
29+ // binary
3830
39- let inline mkNumericMul zero =
40- <@ fun ( x : 't option ) ( y : 't option ) ->
31+ let inline optionBinOpQ zero binOp =
32+ <@ fun ( x : 'a option ) ( y : 'a option ) ->
4133 let mutable res = zero
4234
4335 match x, y with
44- | Some f, Some s -> res <- f * s
45- | _ -> ()
36+ | Some f, Some s -> res <- (% binOp) f s
37+ | Some f, None -> res <- f
38+ | None, Some s -> res <- s
39+ | None, None -> ()
4640
4741 if res = zero then None else Some res @>
4842
49- let inline mkNumericMulAtLeastOne zero =
50- <@ fun ( values : AtLeastOne < 't , 't > ) ->
43+ let inline optionBinOp zero binOp =
44+ fun ( x : 'a option ) ( y : 'a option ) ->
5145 let mutable res = zero
5246
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-
6347 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
6451 | None, None -> ()
65- | _ -> res <- true
6652
67- if res then Some true else None @>
53+ if res = zero then None else Some res
6854
69- let inline addLeftConst zero constant =
70- mkUnaryOp zero <@ fun x -> constant + x @>
55+ let createOptionPair zero opQ op =
56+ optionBinOpQ zero opQ , optionBinOp zero op
7157
72- let inline addRightConst zero constant =
73- mkUnaryOp zero <@ fun x -> x + constant @>
58+ let inline createOptionSumPair zero = createOptionPair zero <@ (+) @> (+)
7459
75- let intSumOption = mkNumericSum 0
76- let byteSumOption = mkNumericSum 0 uy
77- let floatSumOption = mkNumericSum 0.0
78- let float32SumOption = mkNumericSum 0 f
60+ let intSumOption = createOptionSumPair 0
61+ let byteSumOption = createOptionSumPair 0 uy
62+ let floatSumOption = createOptionSumPair 0.0
63+ let float32SumOption = createOptionSumPair 0 f
7964
80- let boolSumAtLeastOne =
81- <@ fun ( _ : AtLeastOne < bool , bool >) -> Some true @>
65+ let boolSumOption = createOptionPair false <@ (||) @> (||)
8266
83- let intSumAtLeastOne = mkNumericSumAtLeastOne 0
84- let byteSumAtLeastOne = mkNumericSumAtLeastOne 0 uy
85- let floatSumAtLeastOne = mkNumericSumAtLeastOne 0.0
86- let float32SumAtLeastOne = mkNumericSumAtLeastOne 0 f
67+ let inline createOptionMulPair zero = createOptionPair zero <@ (*) @> (*)
8768
88- let boolMulOption =
89- <@ fun ( x : bool option ) ( y : bool option ) ->
90- let mutable res = false
69+ let intMulOption = createOptionMulPair 0
70+ let byteMulOption = createOptionMulPair 0 uy
71+ let floatMulOption = createOptionMulPair 0.0
72+ let float32MulOption = createOptionMulPair 0 f
9173
92- match x, y with
93- | Some _, Some _ -> res <- true
94- | _ -> ()
74+ let boolMulOption = createOptionPair true <@ (&&) @> (&&)
9575
96- if res then Some true else None @>
76+ let inline atLeastOneBinOpQ zero binOp =
77+ Convert.optionToAtLeastOne <| optionBinOpQ zero binOp
9778
98- let inline mulLeftConst zero constant =
99- mkUnaryOp zero <@ fun x -> constant * x @>
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)
10086
101- let inline mulRightConst zero constant =
102- mkUnaryOp zero <@ fun x -> x * constant @>
87+ let inline createAtLeastOnePair zero opQ op =
88+ atLeastOneBinOpQ zero opQ, atLeastOneBinOp zero op
89+
90+ let inline createAtLeastOneSumPair zero = createAtLeastOnePair zero <@ (+) @> (+)
10391
104- let intMulOption = mkNumericMul 0
105- let byteMulOption = mkNumericMul 0 uy
106- let floatMulOption = mkNumericMul 0.0
107- let float32MulOption = mkNumericMul 0 f
92+ let intSumAtLeastOne = createAtLeastOneSumPair 0
93+ let byteSumAtLeastOne = createAtLeastOneSumPair 0 uy
94+ let floatSumAtLeastOne = createAtLeastOneSumPair 0.0
95+ let float32SumAtLeastOne = createAtLeastOneSumPair 0 f
10896
109- let boolMulAtLeastOne =
110- <@ fun ( values : AtLeastOne < bool , bool >) ->
111- let mutable res = false
97+ let boolSumAtLeastOne = createAtLeastOnePair false <@ (||) @> (||)
11298
113- match values with
114- | Both _ -> res <- true
115- | _ -> ()
99+ let inline createAtLeastOneMulPair zero = createAtLeastOnePair zero <@ (*) @> (*)
116100
117- if res then Some true else None @>
101+ let intMulAtLeastOne = createAtLeastOneMulPair 0
102+ let byteMulAtLeastOne = createAtLeastOneMulPair 0 uy
103+ let floatMulAtLeastOne = createAtLeastOneMulPair 0.0
104+ let float32MulAtLeastOne = createAtLeastOneMulPair 0 f
118105
119- let intMulAtLeastOne = mkNumericMulAtLeastOne 0
120- let byteMulAtLeastOne = mkNumericMulAtLeastOne 0 uy
121- let floatMulAtLeastOne = mkNumericMulAtLeastOne 0.0
122- let float32MulAtLeastOne = mkNumericMulAtLeastOne 0 f
106+ let boolMulAtLeastOne = createAtLeastOnePair true <@ (&&) @> (&&)
123107
124108 let notOption =
125109 <@ fun x ->
126110 match x with
127111 | Some true -> None
128112 | _ -> Some true @>
129113
114+ // unwrapped operands
130115 let inline private binOpQ zero op =
131116 <@ fun ( left : 'a ) ( right : 'a ) ->
132117 let result = (% op) left right
0 commit comments