├── .github └── FUNDING.yml ├── README.md ├── SwiftHaskell ├── .gitignore ├── BuildSettings.xcconfig ├── SwiftHaskell.xcodeproj │ └── project.pbxproj ├── SwiftHaskell │ ├── Assets.xcassets │ │ ├── AccentColor.colorset │ │ │ └── Contents.json │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── Contents.json │ ├── ContentView.swift │ ├── SwiftHaskell.entitlements │ └── SwiftHaskellApp.swift ├── haskell-framework │ ├── .gitignore │ ├── CHANGELOG.md │ ├── LICENSE │ ├── README.md │ ├── cbits │ │ ├── MyForeignLibRts.c │ │ └── MyForeignLibRts.h │ ├── flib │ │ └── MyForeignLib.hs │ ├── haskell-framework.cabal │ ├── scripts │ │ ├── gen-dynamic-settings.sh │ │ └── test-haskell-foreign-lib.sh │ └── src │ │ └── MyLib.hs └── module.modulemap └── experiments ├── Makefile ├── Memory.hs └── Rect.swift /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: alt-romes 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is the beginning of a repository in which every commit matches a step of an 2 | in-depth guide into developing a native applications for Apple platforms (macOS, 3 | iOS, etc.) using Haskell with Swift and SwiftUI. 4 | 5 | The [first of the series of blog posts](https://alt-romes.github.io/posts/2023-11-10-creating-a-macos-app-with-haskell-and-swift.html) covers the set-up required to call 6 | Haskell functions from Swift in an XCode project using SwiftUI. 7 | 8 | In future installements of the series, I intend to at least discuss calling 9 | functions with idiomatic Haskell types with Swift ones (both with and without 10 | marshaling), SwiftUI observation, and iOS development which requires GHC to 11 | produce code for the iOS compilation target. 12 | 13 | This work is being partially sponsored by [Well-Typed](https://well-typed.com/), and is otherwise carried out in my own free time. 14 | -------------------------------------------------------------------------------- /SwiftHaskell/.gitignore: -------------------------------------------------------------------------------- 1 | SwiftHaskell.xcodeproj/project.xcworkspace/ 2 | SwiftHaskell/Preview\ Content/ 3 | DynamicBuildSettings.xcconfig 4 | -------------------------------------------------------------------------------- /SwiftHaskell/BuildSettings.xcconfig: -------------------------------------------------------------------------------- 1 | SWIFT_INCLUDE_PATHS=$(PROJECT_DIR) 2 | OTHER_LDFLAGS=-lhaskell-foreign-framework 3 | #include "DynamicBuildSettings.xcconfig" 4 | -------------------------------------------------------------------------------- /SwiftHaskell/SwiftHaskell.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 56; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | C356F9F22AFD970F00285AD5 /* SwiftHaskellApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = C356F9F12AFD970F00285AD5 /* SwiftHaskellApp.swift */; }; 11 | C356F9F42AFD970F00285AD5 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C356F9F32AFD970F00285AD5 /* ContentView.swift */; }; 12 | C356F9F62AFD971000285AD5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C356F9F52AFD971000285AD5 /* Assets.xcassets */; }; 13 | C356F9F92AFD971000285AD5 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C356F9F82AFD971000285AD5 /* Preview Assets.xcassets */; }; 14 | C356FA092AFDA14C00285AD5 /* BuildSettings.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = C356FA082AFDA14C00285AD5 /* BuildSettings.xcconfig */; }; 15 | C356FA832AFE487A00285AD5 /* libhaskell-foreign-framework.dylib in CopyFiles */ = {isa = PBXBuildFile; fileRef = C356FA822AFE487A00285AD5 /* libhaskell-foreign-framework.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXCopyFilesBuildPhase section */ 19 | C356FA812AFE481100285AD5 /* CopyFiles */ = { 20 | isa = PBXCopyFilesBuildPhase; 21 | buildActionMask = 2147483647; 22 | dstPath = ""; 23 | dstSubfolderSpec = 10; 24 | files = ( 25 | C356FA832AFE487A00285AD5 /* libhaskell-foreign-framework.dylib in CopyFiles */, 26 | ); 27 | runOnlyForDeploymentPostprocessing = 0; 28 | }; 29 | /* End PBXCopyFilesBuildPhase section */ 30 | 31 | /* Begin PBXFileReference section */ 32 | C356F9EE2AFD970F00285AD5 /* SwiftHaskell.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftHaskell.app; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | C356F9F12AFD970F00285AD5 /* SwiftHaskellApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftHaskellApp.swift; sourceTree = ""; }; 34 | C356F9F32AFD970F00285AD5 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 35 | C356F9F52AFD971000285AD5 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 36 | C356F9F82AFD971000285AD5 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 37 | C356F9FA2AFD971000285AD5 /* SwiftHaskell.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SwiftHaskell.entitlements; sourceTree = ""; }; 38 | C356FA082AFDA14C00285AD5 /* BuildSettings.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = BuildSettings.xcconfig; sourceTree = SOURCE_ROOT; }; 39 | C356FA822AFE487A00285AD5 /* libhaskell-foreign-framework.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libhaskell-foreign-framework.dylib"; path = "haskell-framework/dist-newstyle/build/aarch64-osx/ghc-9.8.1/haskell-framework-0.1.0.0/f/haskell-foreign-framework/build/haskell-foreign-framework/libhaskell-foreign-framework.dylib"; sourceTree = ""; }; 40 | /* End PBXFileReference section */ 41 | 42 | /* Begin PBXFrameworksBuildPhase section */ 43 | C356F9EB2AFD970F00285AD5 /* Frameworks */ = { 44 | isa = PBXFrameworksBuildPhase; 45 | buildActionMask = 2147483647; 46 | files = ( 47 | ); 48 | runOnlyForDeploymentPostprocessing = 0; 49 | }; 50 | /* End PBXFrameworksBuildPhase section */ 51 | 52 | /* Begin PBXGroup section */ 53 | C356F9E52AFD970F00285AD5 = { 54 | isa = PBXGroup; 55 | children = ( 56 | C356FA822AFE487A00285AD5 /* libhaskell-foreign-framework.dylib */, 57 | C356FA082AFDA14C00285AD5 /* BuildSettings.xcconfig */, 58 | C356F9F02AFD970F00285AD5 /* SwiftHaskell */, 59 | C356F9EF2AFD970F00285AD5 /* Products */, 60 | ); 61 | sourceTree = ""; 62 | }; 63 | C356F9EF2AFD970F00285AD5 /* Products */ = { 64 | isa = PBXGroup; 65 | children = ( 66 | C356F9EE2AFD970F00285AD5 /* SwiftHaskell.app */, 67 | ); 68 | name = Products; 69 | sourceTree = ""; 70 | }; 71 | C356F9F02AFD970F00285AD5 /* SwiftHaskell */ = { 72 | isa = PBXGroup; 73 | children = ( 74 | C356F9F12AFD970F00285AD5 /* SwiftHaskellApp.swift */, 75 | C356F9F32AFD970F00285AD5 /* ContentView.swift */, 76 | C356F9F52AFD971000285AD5 /* Assets.xcassets */, 77 | C356F9FA2AFD971000285AD5 /* SwiftHaskell.entitlements */, 78 | C356F9F72AFD971000285AD5 /* Preview Content */, 79 | ); 80 | path = SwiftHaskell; 81 | sourceTree = ""; 82 | }; 83 | C356F9F72AFD971000285AD5 /* Preview Content */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | C356F9F82AFD971000285AD5 /* Preview Assets.xcassets */, 87 | ); 88 | path = "Preview Content"; 89 | sourceTree = ""; 90 | }; 91 | /* End PBXGroup section */ 92 | 93 | /* Begin PBXNativeTarget section */ 94 | C356F9ED2AFD970F00285AD5 /* SwiftHaskell */ = { 95 | isa = PBXNativeTarget; 96 | buildConfigurationList = C356F9FD2AFD971000285AD5 /* Build configuration list for PBXNativeTarget "SwiftHaskell" */; 97 | buildPhases = ( 98 | C356F9EA2AFD970F00285AD5 /* Sources */, 99 | C356FA812AFE481100285AD5 /* CopyFiles */, 100 | C356F9EB2AFD970F00285AD5 /* Frameworks */, 101 | C356F9EC2AFD970F00285AD5 /* Resources */, 102 | ); 103 | buildRules = ( 104 | ); 105 | dependencies = ( 106 | ); 107 | name = SwiftHaskell; 108 | productName = SwiftHaskell; 109 | productReference = C356F9EE2AFD970F00285AD5 /* SwiftHaskell.app */; 110 | productType = "com.apple.product-type.application"; 111 | }; 112 | /* End PBXNativeTarget section */ 113 | 114 | /* Begin PBXProject section */ 115 | C356F9E62AFD970F00285AD5 /* Project object */ = { 116 | isa = PBXProject; 117 | attributes = { 118 | BuildIndependentTargetsInParallel = 1; 119 | LastSwiftUpdateCheck = 1500; 120 | LastUpgradeCheck = 1500; 121 | TargetAttributes = { 122 | C356F9ED2AFD970F00285AD5 = { 123 | CreatedOnToolsVersion = 15.0.1; 124 | }; 125 | }; 126 | }; 127 | buildConfigurationList = C356F9E92AFD970F00285AD5 /* Build configuration list for PBXProject "SwiftHaskell" */; 128 | compatibilityVersion = "Xcode 14.0"; 129 | developmentRegion = en; 130 | hasScannedForEncodings = 0; 131 | knownRegions = ( 132 | en, 133 | Base, 134 | ); 135 | mainGroup = C356F9E52AFD970F00285AD5; 136 | productRefGroup = C356F9EF2AFD970F00285AD5 /* Products */; 137 | projectDirPath = ""; 138 | projectRoot = ""; 139 | targets = ( 140 | C356F9ED2AFD970F00285AD5 /* SwiftHaskell */, 141 | ); 142 | }; 143 | /* End PBXProject section */ 144 | 145 | /* Begin PBXResourcesBuildPhase section */ 146 | C356F9EC2AFD970F00285AD5 /* Resources */ = { 147 | isa = PBXResourcesBuildPhase; 148 | buildActionMask = 2147483647; 149 | files = ( 150 | C356FA092AFDA14C00285AD5 /* BuildSettings.xcconfig in Resources */, 151 | C356F9F92AFD971000285AD5 /* Preview Assets.xcassets in Resources */, 152 | C356F9F62AFD971000285AD5 /* Assets.xcassets in Resources */, 153 | ); 154 | runOnlyForDeploymentPostprocessing = 0; 155 | }; 156 | /* End PBXResourcesBuildPhase section */ 157 | 158 | /* Begin PBXSourcesBuildPhase section */ 159 | C356F9EA2AFD970F00285AD5 /* Sources */ = { 160 | isa = PBXSourcesBuildPhase; 161 | buildActionMask = 2147483647; 162 | files = ( 163 | C356F9F42AFD970F00285AD5 /* ContentView.swift in Sources */, 164 | C356F9F22AFD970F00285AD5 /* SwiftHaskellApp.swift in Sources */, 165 | ); 166 | runOnlyForDeploymentPostprocessing = 0; 167 | }; 168 | /* End PBXSourcesBuildPhase section */ 169 | 170 | /* Begin XCBuildConfiguration section */ 171 | C356F9FB2AFD971000285AD5 /* Debug */ = { 172 | isa = XCBuildConfiguration; 173 | baseConfigurationReference = C356FA082AFDA14C00285AD5 /* BuildSettings.xcconfig */; 174 | buildSettings = { 175 | ALWAYS_SEARCH_USER_PATHS = NO; 176 | ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; 177 | CLANG_ANALYZER_NONNULL = YES; 178 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 179 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 180 | CLANG_ENABLE_MODULES = YES; 181 | CLANG_ENABLE_OBJC_ARC = YES; 182 | CLANG_ENABLE_OBJC_WEAK = YES; 183 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 184 | CLANG_WARN_BOOL_CONVERSION = YES; 185 | CLANG_WARN_COMMA = YES; 186 | CLANG_WARN_CONSTANT_CONVERSION = YES; 187 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 188 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 189 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 190 | CLANG_WARN_EMPTY_BODY = YES; 191 | CLANG_WARN_ENUM_CONVERSION = YES; 192 | CLANG_WARN_INFINITE_RECURSION = YES; 193 | CLANG_WARN_INT_CONVERSION = YES; 194 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 195 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 196 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 197 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 198 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 199 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 200 | CLANG_WARN_STRICT_PROTOTYPES = YES; 201 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 202 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 203 | CLANG_WARN_UNREACHABLE_CODE = YES; 204 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 205 | COPY_PHASE_STRIP = NO; 206 | DEBUG_INFORMATION_FORMAT = dwarf; 207 | ENABLE_STRICT_OBJC_MSGSEND = YES; 208 | ENABLE_TESTABILITY = YES; 209 | ENABLE_USER_SCRIPT_SANDBOXING = YES; 210 | GCC_C_LANGUAGE_STANDARD = gnu17; 211 | GCC_DYNAMIC_NO_PIC = NO; 212 | GCC_NO_COMMON_BLOCKS = YES; 213 | GCC_OPTIMIZATION_LEVEL = 0; 214 | GCC_PREPROCESSOR_DEFINITIONS = ( 215 | "DEBUG=1", 216 | "$(inherited)", 217 | ); 218 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 219 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 220 | GCC_WARN_UNDECLARED_SELECTOR = YES; 221 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 222 | GCC_WARN_UNUSED_FUNCTION = YES; 223 | GCC_WARN_UNUSED_VARIABLE = YES; 224 | LOCALIZATION_PREFERS_STRING_CATALOGS = YES; 225 | MACOSX_DEPLOYMENT_TARGET = 14.0; 226 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 227 | MTL_FAST_MATH = YES; 228 | ONLY_ACTIVE_ARCH = YES; 229 | SDKROOT = macosx; 230 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; 231 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 232 | }; 233 | name = Debug; 234 | }; 235 | C356F9FC2AFD971000285AD5 /* Release */ = { 236 | isa = XCBuildConfiguration; 237 | baseConfigurationReference = C356FA082AFDA14C00285AD5 /* BuildSettings.xcconfig */; 238 | buildSettings = { 239 | ALWAYS_SEARCH_USER_PATHS = NO; 240 | ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; 241 | CLANG_ANALYZER_NONNULL = YES; 242 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 243 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 244 | CLANG_ENABLE_MODULES = YES; 245 | CLANG_ENABLE_OBJC_ARC = YES; 246 | CLANG_ENABLE_OBJC_WEAK = YES; 247 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 248 | CLANG_WARN_BOOL_CONVERSION = YES; 249 | CLANG_WARN_COMMA = YES; 250 | CLANG_WARN_CONSTANT_CONVERSION = YES; 251 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 252 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 253 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 254 | CLANG_WARN_EMPTY_BODY = YES; 255 | CLANG_WARN_ENUM_CONVERSION = YES; 256 | CLANG_WARN_INFINITE_RECURSION = YES; 257 | CLANG_WARN_INT_CONVERSION = YES; 258 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 259 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 260 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 261 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 262 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 263 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 264 | CLANG_WARN_STRICT_PROTOTYPES = YES; 265 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 266 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 267 | CLANG_WARN_UNREACHABLE_CODE = YES; 268 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 269 | COPY_PHASE_STRIP = NO; 270 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 271 | ENABLE_NS_ASSERTIONS = NO; 272 | ENABLE_STRICT_OBJC_MSGSEND = YES; 273 | ENABLE_USER_SCRIPT_SANDBOXING = YES; 274 | GCC_C_LANGUAGE_STANDARD = gnu17; 275 | GCC_NO_COMMON_BLOCKS = YES; 276 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 277 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 278 | GCC_WARN_UNDECLARED_SELECTOR = YES; 279 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 280 | GCC_WARN_UNUSED_FUNCTION = YES; 281 | GCC_WARN_UNUSED_VARIABLE = YES; 282 | LOCALIZATION_PREFERS_STRING_CATALOGS = YES; 283 | MACOSX_DEPLOYMENT_TARGET = 14.0; 284 | MTL_ENABLE_DEBUG_INFO = NO; 285 | MTL_FAST_MATH = YES; 286 | SDKROOT = macosx; 287 | SWIFT_COMPILATION_MODE = wholemodule; 288 | }; 289 | name = Release; 290 | }; 291 | C356F9FE2AFD971000285AD5 /* Debug */ = { 292 | isa = XCBuildConfiguration; 293 | buildSettings = { 294 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 295 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 296 | CODE_SIGN_ENTITLEMENTS = SwiftHaskell/SwiftHaskell.entitlements; 297 | CODE_SIGN_STYLE = Automatic; 298 | COMBINE_HIDPI_IMAGES = YES; 299 | CURRENT_PROJECT_VERSION = 1; 300 | DEVELOPMENT_ASSET_PATHS = "\"SwiftHaskell/Preview Content\""; 301 | DEVELOPMENT_TEAM = 6DK27X5SAW; 302 | ENABLE_HARDENED_RUNTIME = YES; 303 | ENABLE_PREVIEWS = YES; 304 | GENERATE_INFOPLIST_FILE = YES; 305 | INFOPLIST_KEY_NSHumanReadableCopyright = ""; 306 | LD_RUNPATH_SEARCH_PATHS = ( 307 | "$(inherited)", 308 | "@executable_path/../Frameworks", 309 | ); 310 | MARKETING_VERSION = 1.0; 311 | PRODUCT_BUNDLE_IDENTIFIER = romes.SwiftHaskell; 312 | PRODUCT_NAME = "$(TARGET_NAME)"; 313 | SWIFT_EMIT_LOC_STRINGS = YES; 314 | SWIFT_VERSION = 5.0; 315 | }; 316 | name = Debug; 317 | }; 318 | C356F9FF2AFD971000285AD5 /* Release */ = { 319 | isa = XCBuildConfiguration; 320 | buildSettings = { 321 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 322 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 323 | CODE_SIGN_ENTITLEMENTS = SwiftHaskell/SwiftHaskell.entitlements; 324 | CODE_SIGN_STYLE = Automatic; 325 | COMBINE_HIDPI_IMAGES = YES; 326 | CURRENT_PROJECT_VERSION = 1; 327 | DEVELOPMENT_ASSET_PATHS = "\"SwiftHaskell/Preview Content\""; 328 | DEVELOPMENT_TEAM = 6DK27X5SAW; 329 | ENABLE_HARDENED_RUNTIME = YES; 330 | ENABLE_PREVIEWS = YES; 331 | GENERATE_INFOPLIST_FILE = YES; 332 | INFOPLIST_KEY_NSHumanReadableCopyright = ""; 333 | LD_RUNPATH_SEARCH_PATHS = ( 334 | "$(inherited)", 335 | "@executable_path/../Frameworks", 336 | ); 337 | MARKETING_VERSION = 1.0; 338 | PRODUCT_BUNDLE_IDENTIFIER = romes.SwiftHaskell; 339 | PRODUCT_NAME = "$(TARGET_NAME)"; 340 | SWIFT_EMIT_LOC_STRINGS = YES; 341 | SWIFT_VERSION = 5.0; 342 | }; 343 | name = Release; 344 | }; 345 | /* End XCBuildConfiguration section */ 346 | 347 | /* Begin XCConfigurationList section */ 348 | C356F9E92AFD970F00285AD5 /* Build configuration list for PBXProject "SwiftHaskell" */ = { 349 | isa = XCConfigurationList; 350 | buildConfigurations = ( 351 | C356F9FB2AFD971000285AD5 /* Debug */, 352 | C356F9FC2AFD971000285AD5 /* Release */, 353 | ); 354 | defaultConfigurationIsVisible = 0; 355 | defaultConfigurationName = Release; 356 | }; 357 | C356F9FD2AFD971000285AD5 /* Build configuration list for PBXNativeTarget "SwiftHaskell" */ = { 358 | isa = XCConfigurationList; 359 | buildConfigurations = ( 360 | C356F9FE2AFD971000285AD5 /* Debug */, 361 | C356F9FF2AFD971000285AD5 /* Release */, 362 | ); 363 | defaultConfigurationIsVisible = 0; 364 | defaultConfigurationName = Release; 365 | }; 366 | /* End XCConfigurationList section */ 367 | }; 368 | rootObject = C356F9E62AFD970F00285AD5 /* Project object */; 369 | } 370 | -------------------------------------------------------------------------------- /SwiftHaskell/SwiftHaskell/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /SwiftHaskell/SwiftHaskell/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "mac", 5 | "scale" : "1x", 6 | "size" : "16x16" 7 | }, 8 | { 9 | "idiom" : "mac", 10 | "scale" : "2x", 11 | "size" : "16x16" 12 | }, 13 | { 14 | "idiom" : "mac", 15 | "scale" : "1x", 16 | "size" : "32x32" 17 | }, 18 | { 19 | "idiom" : "mac", 20 | "scale" : "2x", 21 | "size" : "32x32" 22 | }, 23 | { 24 | "idiom" : "mac", 25 | "scale" : "1x", 26 | "size" : "128x128" 27 | }, 28 | { 29 | "idiom" : "mac", 30 | "scale" : "2x", 31 | "size" : "128x128" 32 | }, 33 | { 34 | "idiom" : "mac", 35 | "scale" : "1x", 36 | "size" : "256x256" 37 | }, 38 | { 39 | "idiom" : "mac", 40 | "scale" : "2x", 41 | "size" : "256x256" 42 | }, 43 | { 44 | "idiom" : "mac", 45 | "scale" : "1x", 46 | "size" : "512x512" 47 | }, 48 | { 49 | "idiom" : "mac", 50 | "scale" : "2x", 51 | "size" : "512x512" 52 | } 53 | ], 54 | "info" : { 55 | "author" : "xcode", 56 | "version" : 1 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /SwiftHaskell/SwiftHaskell/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /SwiftHaskell/SwiftHaskell/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // SwiftHaskell 4 | // 5 | // Created by Rodrigo Mesquita on 09/11/2023. 6 | // 7 | 8 | import SwiftUI 9 | import HaskellFramework.MyForeignLib_stub 10 | 11 | struct User: Codable { 12 | let name: String 13 | let age: Int 14 | } 15 | 16 | struct Rect { 17 | let width: Int 18 | let height: Int 19 | } 20 | 21 | func wrap_give_rect() -> Rect { 22 | let y = give_rect() 23 | return y!.load(as: Rect.self) 24 | } 25 | 26 | // birthday(user: User(name: "Anton", age: 33)) = User(name: "Anton", age: 34) 27 | func birthday (user : User) -> User { 28 | let enc = JSONEncoder() 29 | let dec = JSONDecoder() 30 | do { 31 | var data : Data = try enc.encode(user) 32 | let data_len = Int64(data.count) 33 | return try data.withUnsafeMutableBytes { (rawPtr:UnsafeMutableRawBufferPointer) in 34 | 35 | // Allocate buffer for result 36 | let buf_size = 1024000 37 | 38 | return try withUnsafeTemporaryAllocation(of: Int.self, capacity: 1) { size_ptr in 39 | size_ptr.baseAddress?.pointee = buf_size 40 | 41 | do { 42 | return try withUnsafeTemporaryAllocation(byteCount: buf_size, alignment: 1) { 43 | res_ptr in 44 | 45 | c_birthday(rawPtr.baseAddress, data_len, res_ptr.baseAddress, size_ptr.baseAddress) 46 | 47 | if let required_size = size_ptr.baseAddress?.pointee { 48 | if required_size > buf_size { 49 | throw HsFFIError.requiredSizeIs(required_size) 50 | } 51 | } 52 | return try dec.decode(User.self, from: Data(bytesNoCopy: res_ptr.baseAddress!, count: size_ptr.baseAddress?.pointee ?? 0, deallocator: .none)) 53 | } 54 | } catch HsFFIError.requiredSizeIs(let required_size) { 55 | print("Retrying with required size: \(required_size)") 56 | return try withUnsafeTemporaryAllocation(byteCount: required_size, alignment: 57 | 1) { res_ptr in 58 | size_ptr.baseAddress?.pointee = required_size 59 | 60 | c_birthday(rawPtr.baseAddress, data_len, res_ptr.baseAddress, size_ptr.baseAddress) 61 | 62 | return try dec.decode(User.self, from: Data(bytesNoCopy: res_ptr.baseAddress!, count: size_ptr.baseAddress?.pointee ?? 0, deallocator: .none)) 63 | } 64 | } 65 | } 66 | } 67 | } catch { 68 | print("Error decoding JSON probably: \(error)") 69 | return User(name: "", age: 0) 70 | } 71 | } 72 | 73 | enum HsFFIError: Error { 74 | case requiredSizeIs(Int) 75 | } 76 | 77 | struct ContentView: View { 78 | var body: some View { 79 | VStack { 80 | let user = birthday(user: User(name: "Ellie", age: 24)) 81 | let rect = wrap_give_rect() 82 | Text("Post-birthday, \(user.name) is: \(user.age)!") 83 | Text("myrect: width is \(rect.width) and height is \(rect.height)!") 84 | } 85 | .padding() 86 | } 87 | } 88 | 89 | #Preview { 90 | ContentView() 91 | } 92 | -------------------------------------------------------------------------------- /SwiftHaskell/SwiftHaskell/SwiftHaskell.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.security.app-sandbox 6 | 7 | com.apple.security.files.user-selected.read-only 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /SwiftHaskell/SwiftHaskell/SwiftHaskellApp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SwiftHaskellApp.swift 3 | // SwiftHaskell 4 | // 5 | // Created by Rodrigo Mesquita on 09/11/2023. 6 | // 7 | 8 | import SwiftUI 9 | import HaskellFramework.RTSManage 10 | 11 | @main 12 | struct SwiftHaskellApp: App { 13 | init() { 14 | flib_init() 15 | 16 | NotificationCenter.default.addObserver(forName: NSApplication.willTerminateNotification, object: nil, queue: .main) { _ in 17 | // terminating 18 | flib_end() 19 | } 20 | } 21 | var body: some Scene { 22 | WindowGroup { 23 | ContentView() 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/.gitignore: -------------------------------------------------------------------------------- 1 | haskell-framework-include/ 2 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Revision history for haskell-framework 2 | 3 | ## 0.1.0.0 -- YYYY-mm-dd 4 | 5 | * First version. Released on an unsuspecting world. 6 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2023, Rodrigo Mesquita 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following 13 | disclaimer in the documentation and/or other materials provided 14 | with the distribution. 15 | 16 | * Neither the name of Rodrigo Mesquita nor the names of other 17 | contributors may be used to endorse or promote products derived 18 | from this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/README.md: -------------------------------------------------------------------------------- 1 | A step-by-step Haskell x Swift project where the commits match the steps 2 | described in the accompanying blog post (TODO). 3 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/cbits/MyForeignLibRts.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | HsBool flib_init() { 6 | 7 | printf("Initialising flib\n"); 8 | 9 | // Initialise Haskell runtime 10 | hs_init(NULL, NULL); 11 | 12 | // Do other library initialisations here 13 | 14 | return HS_BOOL_TRUE; 15 | } 16 | 17 | void flib_end() { 18 | printf("Terminating flib\n"); 19 | hs_exit(); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/cbits/MyForeignLibRts.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | HsBool flib_init(); 4 | void flib_end(); 5 | 6 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/flib/MyForeignLib.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE ForeignFunctionInterface, MagicHash, UnboxedTuples #-} 2 | module MyForeignLib where 3 | import GHC.Exts 4 | import Foreign.C 5 | import Foreign.Ptr 6 | import Foreign.Storable 7 | import Foreign.Marshal 8 | import Data.Aeson 9 | import Data.Bits 10 | import Data.ByteString 11 | import Data.ByteString.Unsafe 12 | import MyLib (hs_factorial, birthday) 13 | 14 | import Unsafe.Coerce 15 | import GHC.IO 16 | 17 | foreign export ccall hs_factorial :: CInt -> CInt 18 | 19 | c_birthday :: Ptr CChar -> Int -> Ptr CChar -> Ptr Int -> IO () 20 | c_birthday cstr clen result size_ptr = do 21 | -- (1) Decode C string 22 | Just user <- decodeStrict <$> unsafePackCStringLen (cstr, clen) 23 | -- (2) Apply `birthday` 24 | let user_new = birthday user 25 | -- (3) Encode result 26 | unsafeUseAsCStringLen (toStrict $ encode user_new) $ \(ptr,len) -> do 27 | 28 | -- (3.2) What is the size of the result buffer? 29 | size_avail <- peek size_ptr 30 | 31 | -- (3.3) Write actual size to the int ptr. 32 | poke size_ptr len 33 | 34 | -- (3.4) If sufficient, we copy the result bytes to the given result buffer 35 | if size_avail < len 36 | then do 37 | -- We need @len@ bytes available 38 | -- The caller has to retry 39 | return () 40 | else do 41 | moveBytes result ptr len 42 | 43 | foreign export ccall c_birthday :: Ptr CChar -> Int -> Ptr CChar -> Ptr Int -> IO () 44 | 45 | data Rect 46 | = Rect 47 | { width :: {-# UNPACK #-} !Int 48 | , height :: {-# UNPACK #-} !Int 49 | } 50 | 51 | myrect :: Rect 52 | myrect = Rect 12 24 53 | 54 | rectToAddr :: Rect -> Ptr () 55 | rectToAddr x = unsafePerformIO $ IO $ \rw -> 56 | case anyToAddr# x rw of 57 | (# rw, addr #) -> (# rw, Ptr addr #) 58 | 59 | give_rect :: Ptr () 60 | give_rect = 61 | let 62 | -- Step 1 63 | tagged_ptr = rectToAddr myrect :: Ptr () 64 | -- Step 1.5 65 | untagged_ptr = wordPtrToPtr (complement 7 .&. ptrToWordPtr tagged_ptr) 66 | -- Step 2 67 | ptr_final = untagged_ptr `plusPtr` 8 :: Ptr () -- 8 bytes 68 | in 69 | ptr_final 70 | 71 | foreign export ccall give_rect :: Ptr () 72 | 73 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/haskell-framework.cabal: -------------------------------------------------------------------------------- 1 | cabal-version: 3.4 2 | name: haskell-framework 3 | version: 0.1.0.0 4 | -- synopsis: 5 | -- description: 6 | license: BSD-3-Clause 7 | license-file: LICENSE 8 | author: Rodrigo Mesquita 9 | maintainer: rodrigo.m.mesquita@gmail.com 10 | -- copyright: 11 | build-type: Simple 12 | extra-doc-files: CHANGELOG.md 13 | -- extra-source-files: 14 | 15 | common warnings 16 | ghc-options: -Wall 17 | 18 | library 19 | import: warnings 20 | exposed-modules: MyLib 21 | -- other-modules: 22 | -- other-extensions: 23 | build-depends: base ^>=4.19.0.0, aeson, bytestring 24 | hs-source-dirs: src 25 | default-language: GHC2021 26 | 27 | foreign-library haskell-foreign-framework 28 | type: native-shared 29 | 30 | -- This should work on Mac, despite being undefined behaviour 31 | -- See https://www.hobson.space/posts/haskell-foreign-library/ (great read) 32 | options: standalone 33 | 34 | -- We copy the C stub headers to a folder in the root. 35 | -- If you have foreign-export declarations in the library 36 | -- be sure to add this flag there too (so all stubs get added 37 | -- to the `haskell-framework-include` folder) 38 | ghc-options: -stubdir=haskell-framework-include -ddump-cmm 39 | 40 | other-modules: MyForeignLib 41 | build-depends: base, haskell-framework, bytestring, aeson 42 | hs-source-dirs: flib 43 | 44 | include-dirs: cbits 45 | c-sources: cbits/MyForeignLibRts.c 46 | install-includes: MyForeignLibRts.h 47 | 48 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/scripts/gen-dynamic-settings.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | if ! test -f "haskell-framework/haskell-framework.cabal"; then 5 | echo "Run this script from the root of your XCode project!" 6 | exit 1 7 | fi 8 | 9 | pushd . > /dev/null 10 | cd haskell-framework 11 | FLIB_PATH=$(cabal list-bin haskell-foreign-framework) 12 | popd > /dev/null 13 | 14 | echo " 15 | HEADER_SEARCH_PATHS=\$(inherit) $(ghc-pkg field rts include-dirs --simple-output | tr ' ' '\n' | tail -n1) 16 | LIBRARY_SEARCH_PATHS=\$(inherit) $(dirname $FLIB_PATH) 17 | " > DynamicBuildSettings.xcconfig 18 | 19 | echo "Created DynamicBuildSettings.xcconfig!" 20 | 21 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/scripts/test-haskell-foreign-lib.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -e 4 | 5 | if ! test -f "haskell-framework.cabal"; then 6 | echo "Run this script from the root of your project!" 7 | exit 1 8 | fi 9 | 10 | # Alternatively, run `cabal list-bin haskell-foreign-framework` 11 | HS_FLIB=$(find . -name libhaskell-foreign-framework.dylib) 12 | 13 | if test -z $HS_FLIB; then 14 | echo "Shared library not found! Did you run 'cabal build'?" 15 | exit 1 16 | fi 17 | 18 | HS_FLIB_PATH=$(dirname $HS_FLIB) 19 | HS_HEADERS_PATH=haskell-framework-include 20 | 21 | echo " 22 | #include 23 | #include 24 | #include 25 | int main(void) { 26 | hs_init(NULL, NULL); 27 | printf(\"%d\n\", hs_factorial(5)); 28 | hs_exit(); 29 | return 0; 30 | } 31 | " > conftestmain.c 32 | 33 | # We use `ghc` instead of `gcc` because otherwise we also need to provide the 34 | # include and lib path of the runtime system (Rts) 35 | ghc -no-hs-main -o conftest conftestmain.c \ 36 | -lhaskell-foreign-framework \ 37 | -I"$HS_HEADERS_PATH" \ 38 | -L"$HS_FLIB_PATH" \ 39 | -optl-Wl,-rpath,"$HS_FLIB_PATH" 40 | 41 | RESULT=$(./conftest) 42 | 43 | if [ 120 -eq $RESULT ]; then 44 | echo "Foreign library successfully called!" 45 | else 46 | echo "Bad bad foreign library!" 47 | exit 1 48 | fi 49 | 50 | rm -f conftest* 51 | 52 | -------------------------------------------------------------------------------- /SwiftHaskell/haskell-framework/src/MyLib.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE DerivingStrategies, DeriveAnyClass, OverloadedRecordDot #-} 2 | module MyLib where 3 | 4 | import Foreign.C 5 | import Data.Aeson 6 | import GHC.Generics 7 | 8 | hs_factorial :: CInt -> CInt 9 | hs_factorial x = product [1..x] 10 | 11 | data User = User 12 | { name :: String 13 | , age :: Int 14 | } 15 | deriving stock Generic 16 | deriving anyclass (ToJSON, FromJSON) 17 | 18 | birthday :: User -> User 19 | birthday user = user{age = user.age + 1} 20 | -------------------------------------------------------------------------------- /SwiftHaskell/module.modulemap: -------------------------------------------------------------------------------- 1 | // 2 | // module.modulemap 3 | // SwiftHaskell 4 | // 5 | // Created by Rodrigo Mesquita on 09/11/2023. 6 | // 7 | 8 | module HaskellFramework { 9 | umbrella "haskell-framework/haskell-framework-include" 10 | 11 | explicit module * { 12 | export * 13 | } 14 | 15 | explicit module RTSManage { 16 | header "haskell-framework/cbits/MyForeignLibRts.h" 17 | } 18 | 19 | link "haskell-foreign-framework" 20 | } 21 | -------------------------------------------------------------------------------- /experiments/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all Memory.cmm Rect.S 2 | 3 | all: Memory.cmm Rect.S 4 | 5 | Memory.cmm: Memory.hs 6 | ghc -O -fforce-recomp -ddump-cmm $< > $@ 7 | 8 | Rect.S: Rect.swift 9 | swiftc $< -emit-ir -o - | swift demangle > $@ 10 | -------------------------------------------------------------------------------- /experiments/Memory.hs: -------------------------------------------------------------------------------- 1 | module Memory where 2 | 3 | data Rect 4 | = Rect { width :: {-# UNPACK #-} !Int 5 | , height :: {-# UNPACK #-} !Int 6 | } 7 | 8 | double :: Rect -> Rect 9 | double Rect{width=x, height=y} = Rect{width=x*2, height=y*2} 10 | 11 | myrect = Rect{width=12, height=24} 12 | 13 | area = putStrLn ("Area: " ++ show (width myrect * height myrect)) 14 | 15 | giveMyRect :: () -> Rect 16 | giveMyRect () = myrect 17 | -------------------------------------------------------------------------------- /experiments/Rect.swift: -------------------------------------------------------------------------------- 1 | 2 | struct Rect { 3 | let width: Int 4 | let height: Int 5 | } 6 | 7 | func makeRect(w: Int, h: Int) -> Rect { 8 | return Rect(width: 533, height:6464) 9 | } 10 | --------------------------------------------------------------------------------