|
7 | 7 | // |
8 | 8 |
|
9 | 9 | /// If R is a Ring and M is an abelian group, a Left Module defines a binary operation R *<> M -> M. |
10 | | -protocol LeftModule { |
11 | | - associatedtype R : Semiring |
12 | | - associatedtype A : Additive |
13 | | - func *<>(_ : R, _ : A) -> A |
| 10 | +struct LeftModule<R: Semiring, A: Additive> { |
| 11 | + let multiply: (R, A) -> A |
| 12 | +} |
| 13 | +enum LeftModules { |
| 14 | + static let int = LeftModule<Int, Int>(multiply: *) |
| 15 | + static let int8 = LeftModule<Int, Int8>(multiply: { Int8($0) * $1 }) |
| 16 | + static let int16 = LeftModule<Int, Int16>(multiply: { Int16($0) * $1 }) |
| 17 | + static let int32 = LeftModule<Int, Int32>(multiply: { Int32($0) * $1 }) |
| 18 | + static let int64 = LeftModule<Int, Int64>(multiply: { Int64($0) * $1 }) |
14 | 19 | } |
15 | 20 |
|
16 | 21 | /// If R is a Ring and M is an abelian group, a Right Module defines a binary operation M <>* R -> M. |
17 | | -protocol RightModule { |
18 | | - associatedtype R : Semiring |
19 | | - associatedtype M : Additive |
20 | | - func <>* (_ : M, _ : R) -> M |
| 22 | +struct RightModule<A: Additive, R: Semiring> { |
| 23 | + let multiply: (A, R) -> A |
| 24 | +} |
| 25 | +enum RightModules { |
| 26 | + static let int = RightModule<Int, Int>(multiply: *) |
| 27 | + static let int8 = RightModule<Int8, Int>(multiply: { $0 * Int8($1) }) |
| 28 | + static let int16 = RightModule<Int16, Int>(multiply: { $0 * Int16($1) }) |
| 29 | + static let int32 = RightModule<Int32, Int>(multiply: { $0 * Int32($1) }) |
| 30 | + static let int64 = RightModule<Int64, Int>(multiply: { $0 * Int64($1) }) |
21 | 31 | } |
22 | 32 |
|
23 | 33 | /// A bimodule is a module with compatible left and right operations. |
24 | | -protocol Bimodule : LeftModule, RightModule {} |
25 | | - |
26 | | -extension Int : LeftModule { |
27 | | - typealias R = Int |
28 | | - typealias A = Int |
29 | | -} |
30 | | -public func *<>(l : Int, r : Int) -> Int { return l * r } |
31 | | -extension Int8 : LeftModule { |
32 | | - typealias R = Int |
33 | | - typealias A = Int8 |
| 34 | +struct Bimodule<R: Semiring, A: Additive> { |
| 35 | + let left: LeftModule<R, A> |
| 36 | + let right: RightModule<A, R> |
34 | 37 | } |
35 | | -public func *<>(l : Int, r : Int8) -> Int8 { return Int8(l) * r } |
36 | | -extension Int16 : LeftModule { |
37 | | - typealias R = Int |
38 | | - typealias A = Int16 |
| 38 | +enum Bimodules { |
| 39 | + static let int = Bimodule(left: LeftModules.int, right: RightModules.int) |
| 40 | + static let int8 = Bimodule(left: LeftModules.int8, right: RightModules.int8) |
| 41 | + static let int16 = Bimodule(left: LeftModules.int16, right: RightModules.int16) |
| 42 | + static let int32 = Bimodule(left: LeftModules.int32, right: RightModules.int32) |
| 43 | + static let int64 = Bimodule(left: LeftModules.int64, right: RightModules.int64) |
39 | 44 | } |
40 | | -public func *<>(l : Int, r : Int16) -> Int16 { return Int16(l) * r } |
41 | | -extension Int32 : LeftModule { |
42 | | - typealias R = Int |
43 | | - typealias A = Int32 |
44 | | -} |
45 | | -public func *<>(l : Int, r : Int32) -> Int32 { return Int32(l) * r } |
46 | | -extension Int64 : LeftModule { |
47 | | - typealias R = Int |
48 | | - typealias A = Int64 |
49 | | -} |
50 | | -public func *<>(l : Int, r : Int64) -> Int64 { return Int64(l) * r } |
51 | | - |
52 | | - |
53 | | -extension Int : RightModule { } |
54 | | -public func <>*(l : Int, r : Int) -> Int { return l * r } |
55 | | - |
56 | | -extension Int8 : RightModule { } |
57 | | -public func <>*(l : Int8, r : Int) -> Int8 { return l * Int8(r) } |
58 | | - |
59 | | -extension Int16 : RightModule { } |
60 | | -public func <>*(l : Int16, r : Int) -> Int16 { return l * Int16(r) } |
61 | | - |
62 | | -extension Int32 : RightModule { } |
63 | | -public func <>*(l : Int32, r : Int) -> Int32 { return l * Int32(r) } |
64 | | - |
65 | | -extension Int64 : RightModule { } |
66 | | -public func <>*(l : Int64, r : Int) -> Int64 { return l * Int64(r) } |
67 | | - |
68 | | - |
69 | | -extension Int : Bimodule { } |
70 | | -extension Int8 : Bimodule { } |
71 | | -extension Int16 : Bimodule { } |
72 | | -extension Int32 : Bimodule { } |
73 | | -extension Int64 : Bimodule { } |
0 commit comments