Compile F# to Morphir IR
Acme Car Rental keeps its daily rates in F#. In five minutes you turn that rule into Morphir IR, and then into C# that another team can call.
graph LR
A[Rates.fs] -->|dotnet-morphir compile| B[morphir-ir.json]
B -->|dotnet-morphir generate| C[C# or F# code]1. Install the tool
You need the .NET SDK 10.0 or later.
dotnet tool install --global Morphir.Tool --prerelease
dotnet-morphir --version
2. Write the rule
mkdir -p acme/src && cd acme
Save this as src/Rates.fs:
namespace Acme.CarRental
module Rates =
type VehicleClass =
| Economy
| Compact
| Suv
let dailyRate (vehicle: VehicleClass) : int =
match vehicle with
| Economy -> 4000
| Compact -> 5000
| Suv -> 8000
3. Compile it
dotnet-morphir compile --language fsharp --input src/Rates.fs --package-name acme/car-rental
compile succeeded: 1 module(s), IR version 4, /work/acme/.morphir/out/compile.dest/morphir-ir.json; no diagnostics
/work/acme/.morphir/out/compile.dest/morphir-ir.json
The IR is in .morphir/out/compile.dest/morphir-ir.json. Every later command can find it there.
4. Generate code
dotnet-morphir generate --target csharp --output generated/csharp
Morphir.Polyfill.g.cs
p_Acme.CarRental/m_RatesModule.g.cs
Morphir.Polyfill.g.cs has the runtime types that closed and union need below .NET 11. generate reads the IR of the most recent compile, so it needs no --input. Use
--target fsharp for F#.
Do both steps in one run:
dotnet-morphir compile --language fsharp --input src/Rates.fs --package-name acme/car-rental \
--target csharp --generate-output generated/csharp
Compile without .NET installed
To compile F# on a machine with no .NET, use morphirup: it installs the Morphir CLI with its own .NET runtime and the
F# frontend. Follow Your first model with morphirup. To build
the self-contained F# frontend from a checkout instead, see
Build the self-contained F# frontend.
Next steps
- Tutorial: car rental pricing from F# to C#: a project file, three modules, a refused construct, a C# app and a CI check.
- Tutorial: compile F# with the native executable:
the AOT
morphirand the F# frontend extension. - What F# can you write?
compilereference
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.