Copyright 2026 Chris Liu, Dustin Tucker, James B. Wilson. All rights reserved. Not permitted for AI training nor public posting.
You've used a Machine Learning ML algorithm to train a function classify that takes audio recordings of diesel engines and returns a code that identifies 457 engine failure categories.
Your present classify to your mechanics and they inform you that they only fix or replace 25 major components, even if the problem is something smaller inside one of the parts. The mechanics have agreed to answer which of the 25 major components goes with which of the 457 failures.
Machine Learning ML algorithm classify identifies 457 engine failure categories.
Mechanics only fix or replace 25 major components, mechanics can tell uswhich of the 25 major components goes with which of the 457 failures.
You need to cut-down the classifier function to match the mechanic roles.
After discussion everyone agrees the right option is to place some new code between the classify output and the mechanics use. But your engineering team is split on possible options.
component that composes with classify and outputs the correct component.component that composes with classify and outputs the correct component.Eugene the engineer didn't wait. Here's his prototype in Swift.
struct FailureCode: Equatable {
let value: Int
init?(_ value: Int) {
guard (1...457).contains(value) else { return nil }
self.value = value
}
func printFailure() {
print("Failure Code: \(self.value)")
}
static func == (lhs: FailureCode, rhs: FailureCode) -> Bool {
// TODO: replace with mechanic codes
if (lhs.value < 250 && rhs.value < 250)
|| (lhs.value >= 250 && rhs.value >= 250) {
return true
}
return false
}
}
func classify<AudioInput>(audioFile: AudioInput) -> FailureCode {
// ML algorithm processes audio file and returns failure code (1-457)
// Placeholder implementation
return FailureCode(Int.random(in: 1...457))!
}
3 Diagnose if this is a problem and explain how it happened.
Text
When we make partial functions (e.g. left inverses) we need to accept that some inputs have no outputs. Programs have many options include Option[A], Maybe a, throws IllegalArgumentException, but Swift makes it very natural.
func doSomething(input) -> Output? Here ? means the output may fail.! when you can prove the output is always there, no errors.guard lets you take an input like Int which takes a huge range of values and insists they fit a restricted range. Avoids errors later.https://swiftfiddle.com/
Eugene the engineer didn't wait. Here's his prototype in Swift.
You notice equality can be true or false, but even when true you get different behavior from x and y.
let noise1 = "noise1"
let noise2 = "noise2"
let x = classify(audioFile: noise1)
let y = classify(audioFile: noise2)
print("1> x = classify(noise1)")
print("2> y = classify(noise2)")
print("3> x == y")
print(x == y) // True about half the time
print("4> x.printFailure()")
x.printFailure()
print("5> y.printFailure()")
y.printFailure() // Not generally the same as x!
3 Diagnose if this is a problem and explain how it happened.
Text
When we make partial functions (e.g. left inverses) we need to accept that some inputs have no outputs. Programs have many options include Option[A], Maybe a, throws IllegalArgumentException, but Swift makes it very natural.
func doSomething(input) -> Output? Here ? means the output may fail.! when you can prove the output is always there, no errors.guard lets you take an input like Int which takes a huge range of values and insists they fit a restricted range. Avoids errors later.After this equality shortcomings, Eugene the Engineer prototypes parts.
struct FailureCode: Equatable {
let value: Int
// TODO get actual partition
static let part1: ClosedRange<Int> = 1...10
static let part2: ClosedRange<Int> = 11...457
init?(_ value: Int) {
guard (1...457).contains(value) else { return nil }
self.value = value
}
func printFailure() {
if FailureCode.part1.contains(value) {
print("part 1")
} else {
print("part 2")
}
}
static func == (lhs: FailureCode, rhs: FailureCode) -> Bool {
let lhsInPart1 = FailureCode.part1.contains(lhs.value)
let rhsInPart1 = FailureCode.part1.contains(rhs.value)
let lhsInPart2 = FailureCode.part2.contains(lhs.value)
let rhsInPart2 = FailureCode.part2.contains(rhs.value)
return (lhsInPart1 && rhsInPart1) || (lhsInPart2 && rhsInPart2)
}
}
- Predict what will be necessary to continue in this way when there are 25 parts.
After this equality shortcomings, Eugene the Engineer prototypes parts.
- Predict what will be necessary to continue in this way when there are 25 parts.
Poor Eugene the Engineer is asked to start over with the function method.
- Design a solution that uses a surjective function and solves the problems Eugene has faced so far.
struct FailureCode: Equatable {
let value: Int
init?(_ value: Int) {
guard (1...457).contains(value) else { return nil }
self.value = value
}
}
struct MechanicCode: Equatable {
let value: String
init?(_ value: String) {
guard !value.isEmpty else { return nil }
self.value = value
}
}
func classify<AudioInput>(audioFile: AudioInput) -> FailureCode {
// ML algorithm processes audio file and returns failure code (1-457)
// Placeholder implementation
return FailureCode(Int.random(in: 1...457))!
}
func reclassify(rawCode :FailureCode) -> MechanicCode {
if rawCode.value < 250 {
return MechanicCode("part 1")!
} else {
return MechanicCode("part 2")!
}
}
let noise1 = "noise1"
let noise2 = "noise2"
let x = classify(audioFile: noise1)
let y = classify(audioFile: noise2)
let x_mechanic = reclassify(rawCode: x)
let y_mechanic = reclassify(rawCode: y)
print(x_mechanic == y_mechanic)
print(x_mechanic)
print(y_mechanic)