Welcome to the PowerShell target for UnifyWeaver! This chapter introduces you to compiling Prolog predicates to PowerShell scripts, explains why you might choose PowerShell over other targets, and walks you through your first compilation.
PowerShell is more than just a shell - it’s a powerful automation framework built on .NET. This makes it an excellent target for UnifyWeaver when you need:
UnifyWeaver supports both Bash and PowerShell targets. Here’s when to choose each:
| Factor | PowerShell | Bash |
|---|---|---|
| Platform | Windows-first, cross-platform | Unix-first, cross-platform |
| Type System | Objects with properties | Text streams |
| Pipeline | Object pipeline | Text pipeline |
| .NET Access | Native | Requires Mono/external tools |
| Windows Admin | Excellent | Limited |
| Unix Admin | Good (pwsh) | Excellent |
| Performance | Good (with caching) | Good |
Choose PowerShell when:
Choose Bash when:
UnifyWeaver offers multiple ways to compile Prolog to PowerShell:
The original approach - generates Bash code wrapped in PowerShell. Uses a compatibility layer.
?- compile_to_powershell(ancestor/2, [powershell_mode(baas)], Code).
Pros: Works immediately, reuses all Bash templates Cons: Requires Bash on the system
Native PowerShell generation without Bash dependency.
?- compile_to_powershell(ancestor/2, [powershell_mode(pure)], Code).
Pros: No Bash required, native object support Cons: Some features still being implemented
Embed C# code directly, compiled at runtime via Add-Type.
:- use_module(library(dotnet_source)).
?- Config = [csharp_inline('... C# code ...')],
dotnet_source:compile_source(my_pred/2, Config, [], PowerShellCode).
Pros: Full .NET library access, high performance with caching Cons: Requires .NET SDK for external compilation
Let’s compile a simple predicate to PowerShell. We’ll use the classic grandparent/2 example.
$ cd /path/to/UnifyWeaver
$ swipl
?- ['education/init'].
true.
?- use_module(unifyweaver(core/powershell_compiler)).
true.
% Define some parent facts
?- assertz(parent(anne, bob)).
?- assertz(parent(bob, charles)).
?- assertz(parent(bob, diana)).
% Define the grandparent rule
?- assertz((grandparent(X, Z) :- parent(X, Y), parent(Y, Z))).
?- compile_to_powershell(grandparent/2, [], Code),
format('~w~n', [Code]).
This will output a PowerShell script that implements the grandparent/2 predicate.
?- compile_to_powershell(grandparent/2, [output_file('grandparent.ps1')], _).
true.
# On Windows
PS> .\grandparent.ps1
# On Linux/macOS with PowerShell 7
$ pwsh grandparent.ps1
The generated PowerShell script includes several key components:
function grandparent {
param(
[Parameter(ValueFromPipeline=$true)]
$InputData
)
# ... implementation
}
PowerShell scripts generated by UnifyWeaver support the pipeline:
# Call directly
grandparent
# Pipe input
"bob" | grandparent
Use -Verbose to see compilation and execution details:
grandparent -Verbose
In the following chapters, you’ll learn:
| Predicate | Description |
|---|---|
compile_to_powershell/2 |
Compile with default options |
compile_to_powershell/3 |
Compile with custom options |
dotnet_source:compile_source/4 |
Compile inline .NET to PowerShell |
| Option | Values | Description |
|---|---|---|
powershell_mode |
pure, baas, auto |
Compilation mode |
output_file |
Path | Write to file |
source_type |
dotnet, csv, etc. |
Data source type |
pre_compile |
true, false |
Enable DLL caching |
| Path | Contents |
|---|---|
src/unifyweaver/core/powershell_compiler.pl |
Main compiler |
src/unifyweaver/sources/dotnet_source.pl |
.NET inline plugin |
playbooks/powershell_inline_dotnet_playbook.md |
Inline .NET guide |
docs/POWERSHELL_TARGET.md |
Full API reference |