Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
swift source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
x86-64 swiftc 3.1.1
x86-64 swiftc 4.0.2
x86-64 swiftc 4.0.3
x86-64 swiftc 4.1
x86-64 swiftc 4.1.1
x86-64 swiftc 4.1.2
x86-64 swiftc 4.2
x86-64 swiftc 5.0
x86-64 swiftc 5.1
x86-64 swiftc 5.10
x86-64 swiftc 5.2
x86-64 swiftc 5.3
x86-64 swiftc 5.4
x86-64 swiftc 5.5
x86-64 swiftc 5.6
x86-64 swiftc 5.7
x86-64 swiftc 5.8
x86-64 swiftc 5.9
x86-64 swiftc nightly
Options
Source code
protocol Nat { static var value: Int { get } } struct Zero: Nat { static var value: Int { 0 } } struct Succ<Pred: Nat>: Nat { static var value: Int { 1 + Pred.value } } /* print(Succ<Succ<Succ<Zero>>>.value) */ // ---- protocol Str { static var value: String { get } } struct EmptyStr: Str { static var value: String { "" } } struct Char<Unicode: Nat, Prefix: Str>: Str { static var value: String { var result = Prefix.value result.append(Character(UnicodeScalar(Unicode.value)!)) return result } } /* typealias A = Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Succ<Zero>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> typealias H = Succ<Succ<Succ<Succ<Succ<Succ<Succ<A>>>>>>> typealias I = Succ<H> print(Char<I, Char<H, EmptyStr>>.value) */ // ---- protocol MemoryTape {} struct ToInfinityAndBeyond: MemoryTape {} struct Square<Value: Nat, Tail: MemoryTape>: MemoryTape { var value: Value var tail: Tail } // ---- protocol Instr {} protocol SimpleInstr: Instr {} struct Exited: Instr {} struct Inc: SimpleInstr {} struct Dec: SimpleInstr {} struct Next: SimpleInstr {} struct Prev: SimpleInstr {} struct CharOut: SimpleInstr {} struct LoopStart: Instr {} struct LoopEnd: Instr {} // ---- protocol ProgramTape {} struct End: ProgramTape {} struct Instruction<I: Instr, Rest: ProgramTape>: ProgramTape { var instruction: I var rest: ProgramTape } // ---- protocol MachineState {} struct Step: MachineState {} struct ScanForward: MachineState {} struct ScanBackward: MachineState {} protocol StateStack {} struct EmptyStack: StateStack {} struct Pushed<State: MachineState, Rest: StateStack>: StateStack {} @dynamicMemberLookup struct Machine< TapeLeft: MemoryTape, TapeCurrent: Nat, TapeRight: MemoryTape, ProgramLeft: ProgramTape, ProgramCurrent: Instr, ProgramRight: ProgramTape, Stack: StateStack, Output: Str > { // increment subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, Succ<TapeCurrent>, TapeRight, Instruction<Inc, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == Inc { fatalError() } // decrement subscript<OutputString, StateTail, Pred, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, Pred, TapeRight, Instruction<Dec, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeCurrent == Succ<Pred>, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == Dec { fatalError() } // next, existing tape subscript<OutputString, StateTail, TapeRightFirst, TapeRightRest, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< Square<TapeCurrent, TapeLeft>, TapeRightFirst, TapeRightRest, Instruction<Next, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeRight == Square<TapeRightFirst, TapeRightRest>, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == Next { fatalError() } // next, creating tape subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< Square<TapeCurrent, TapeLeft>, Zero, ToInfinityAndBeyond, Instruction<Next, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeRight == ToInfinityAndBeyond, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == Next { fatalError() } // prev, existing tape subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< ToInfinityAndBeyond, Zero, Square<TapeCurrent, TapeRight>, Instruction<Prev, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeLeft == ToInfinityAndBeyond, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == Prev { fatalError() } // prev, creating tape subscript<OutputString, StateTail, TapeLeftFirst, TapeLeftRest, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeftRest, TapeLeftFirst, Square<TapeCurrent, TapeRight>, Instruction<Prev, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeLeft == Square<TapeLeftFirst, TapeLeftRest>, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == Prev { fatalError() } // charout subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Instruction<CharOut, ProgramLeft>, NextInstr, Rest, Stack, Char<TapeCurrent, Output> >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == CharOut { fatalError() } // loopstart, nonzero subscript<OutputString, StateTail, Pred, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Instruction<LoopStart, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeCurrent == Succ<Pred>, ProgramCurrent == LoopStart, ProgramRight == Instruction<NextInstr, Rest> { fatalError() } // loopstart, zero subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Instruction<LoopStart, ProgramLeft>, NextInstr, Rest, Pushed<ScanForward, Stack>, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeCurrent == Zero, ProgramCurrent == LoopStart, ProgramRight == Instruction<NextInstr, Rest> { fatalError() } // loopend, zero subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Instruction<LoopEnd, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeCurrent == Zero, ProgramCurrent == LoopEnd, ProgramRight == Instruction<NextInstr, Rest> { fatalError() } // loopend, nonzero subscript<OutputString, StateTail, Pred, PrevInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Rest, PrevInstr, Instruction<LoopEnd, ProgramRight>, Pushed<ScanBackward, Stack>, Output >, OutputString >) -> OutputString where Stack == Pushed<Step, StateTail>, TapeCurrent == Succ<Pred>, ProgramLeft == Instruction<PrevInstr, Rest>, ProgramCurrent == LoopEnd { fatalError() } // scan forward over simple instruction subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Instruction<ProgramCurrent, ProgramLeft>, NextInstr, Rest, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<ScanForward, StateTail>, ProgramCurrent: SimpleInstr, ProgramRight == Instruction<NextInstr, Rest> { fatalError() } // scan forward over loopstart subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Instruction<LoopStart, ProgramLeft>, NextInstr, Rest, Pushed<ScanForward, Stack>, Output >, OutputString >) -> OutputString where Stack == Pushed<ScanForward, StateTail>, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == LoopStart { fatalError() } // scan forward over loopend subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Instruction<LoopEnd, ProgramLeft>, NextInstr, Rest, StateTail, Output >, OutputString >) -> OutputString where Stack == Pushed<ScanForward, StateTail>, ProgramRight == Instruction<NextInstr, Rest>, ProgramCurrent == LoopEnd { fatalError() } // scan backward over simple instruction subscript<OutputString, StateTail, PrevInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Rest, PrevInstr, Instruction<ProgramCurrent, ProgramRight>, Stack, Output >, OutputString >) -> OutputString where Stack == Pushed<ScanBackward, StateTail>, ProgramCurrent: SimpleInstr, ProgramLeft == Instruction<PrevInstr, Rest> { fatalError() } // scan backward over loopend subscript<OutputString, StateTail, PrevInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Rest, PrevInstr, Instruction<LoopEnd, ProgramRight>, Pushed<ScanBackward, Stack>, Output >, OutputString >) -> OutputString where Stack == Pushed<ScanBackward, StateTail>, ProgramCurrent == LoopEnd, ProgramLeft == Instruction<PrevInstr, Rest> { fatalError() } // scan backward over loopstart, nested subscript<OutputString, StateTail, PrevInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Rest, PrevInstr, Instruction<LoopStart, ProgramRight>, Pushed<ScanBackward, StateTail>, Output >, OutputString >) -> OutputString where Stack == Pushed<ScanBackward, Pushed<ScanBackward, StateTail>>, ProgramCurrent == LoopStart, ProgramLeft == Instruction<PrevInstr, Rest> { fatalError() } // scan backward to instruction after loopstart, nonnested subscript<OutputString, StateTail, NextInstr, Rest>(dynamicMember keyPath: KeyPath< Machine< TapeLeft, TapeCurrent, TapeRight, Instruction<LoopStart, ProgramLeft>, NextInstr, Rest, Pushed<Step, StateTail>, Output >, OutputString >) -> OutputString where Stack == Pushed<ScanBackward, Pushed<Step, StateTail>>, ProgramCurrent == LoopStart, ProgramRight == Instruction<NextInstr, Rest> { fatalError() } } extension Machine where ProgramCurrent == Exited { var output: Output { fatalError() } } /* // ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++. typealias PrintHi = Machine< ToInfinityAndBeyond, Zero, ToInfinityAndBeyond, Instruction<Exited, End>, Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<LoopStart, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<LoopStart, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Next, Instruction<Inc, Instruction<Prev, Instruction<Prev, Instruction<Prev, Instruction<Prev, Instruction<Dec, Instruction<LoopEnd, Instruction<Next, Instruction<Inc, Instruction<Next, Instruction<Inc, Instruction<Next, Instruction<Dec, Instruction<Next, Instruction<Next, Instruction<Inc, Instruction<LoopStart, Instruction<Prev, Instruction<LoopEnd, Instruction<Prev, Instruction<Dec, Instruction<LoopEnd, Instruction<Next, Instruction<Next, Instruction<CharOut, Instruction<Next, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<CharOut, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<CharOut, Instruction<CharOut, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<CharOut, Instruction<Next, Instruction<Next, Instruction<CharOut, Instruction<Prev, Instruction<Dec, Instruction<CharOut, Instruction<Prev, Instruction<CharOut, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<CharOut, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<CharOut, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<CharOut, Instruction<Next, Instruction<Next, Instruction<Inc, Instruction<CharOut, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<CharOut, Instruction<Exited, End>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>, Pushed<Step, EmptyStack>, EmptyStr > print(PrintHi().output.debugDescription) */ // +++[->+++<]>[->++++>++++++++>+++++++++<<<]>>.+.<----.>>++.++++.<.---.>---. typealias PrintHi = Machine< ToInfinityAndBeyond, Zero, ToInfinityAndBeyond, Instruction<Exited, End>, Inc, Instruction<Inc, Instruction<Inc, Instruction<LoopStart, Instruction<Dec, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Prev, Instruction<LoopEnd, Instruction<Next, Instruction<LoopStart, Instruction<Dec, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Prev, Instruction<Prev, Instruction<Prev, Instruction<LoopEnd, Instruction<Next, Instruction<Next, Instruction<CharOut, Instruction<Inc, Instruction<CharOut, Instruction<Prev, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<CharOut, Instruction<Next, Instruction<Next, Instruction<Inc, Instruction<Inc, Instruction<CharOut, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<Inc, Instruction<CharOut, Instruction<Prev, Instruction<CharOut, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<CharOut, Instruction<Next, Instruction<Dec, Instruction<Dec, Instruction<Dec, Instruction<CharOut, Instruction<Exited, End>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>, Pushed<Step, EmptyStack>, EmptyStr > protocol Infer { associatedtype InferredType static var inferFrom: InferredType { get } } struct PrintHiOutput: Infer { static let inferFrom = PrintHi().output } print(PrintHiOutput.InferredType.self) print(PrintHiOutput.InferredType.value)
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree