@@ -20,7 +20,19 @@ Let's consider the following example Plinth program:
20
20
start="-- BEGIN Plinth"
21
21
end="-- END Plinth" />
22
22
23
- In order to evaluate this code, we need to add the ` plutus-tx:plutus-tx-testlib `
23
+ This code represents a function that expects an ` Integer ` argument
24
+ and returns the ` Integer ` value.
25
+
26
+ To compile it, we can use the ` compile ` function as described earlier in the "[ Compiling Plinth code] ( ./compiling-plinth.md ) " section:
27
+
28
+ <LiteralInclude
29
+ file="Example/Evaluation/Main.hs"
30
+ language="haskell"
31
+ title="Plinth code compiled"
32
+ start="-- BEGIN CompiledCode"
33
+ end="-- END CompiledCode" />
34
+
35
+ In order to evaluate ` compiledCode ` , we need to add the ` plutus-tx:plutus-tx-testlib `
24
36
dependency to our cabal file:
25
37
``` cabal
26
38
build-depends:
@@ -38,9 +50,6 @@ import PlutusTx.Test (
38
50
)
39
51
```
40
52
41
- Our example code represents a function that expects an `Integer ` argument
42
- and returns the `Integer ` value.
43
-
44
53
It is possible to evaluate this compiled code without applying it to any arguments,
45
54
and evaluation will succeed, returning the value of type `Integer -> Integer `:
46
55
@@ -140,17 +149,42 @@ No traces were emitted
140
149
This output reveals that the evaluation was successful, and the resultng UPLC
141
150
value is an (un-applied) lambda abstraction.
142
151
143
- The [ Lifting Values into CompiledCode] ( ./lifting.md ) section contains and example
144
- of applying the ` CompiledCode ` to an argument.
152
+ To apply the compiled lambda abstraction to an argument we need to have a compiled argument,
153
+ and there are several ways of obtaining it from a Haskell value known at compile time:
154
+ 1 . "lift" it into ` CompiledCode ` . See the [ Lifting Values into CompiledCode] ( ./lifting.md ) section for more details.
155
+ <LiteralInclude
156
+ file="Example/Evaluation/Main.hs"
157
+ language="haskell"
158
+ title="Lift a constant value into CompiledCode"
159
+ start="-- BEGIN LiftedArgument"
160
+ end="-- END LiftedArgument" />
161
+
162
+ 2 . ` $(compile [|| ... ||]) ` it the same way we compiled the function itself.
163
+ <LiteralInclude
164
+ file="Example/Evaluation/Main.hs"
165
+ language="haskell"
166
+ title="Compile a constant value into CompiledCode"
167
+ start="-- BEGIN CompiledArgument"
168
+ end="-- END CompiledArgument" />
169
+
170
+ Once we have an argument of type ` CompiledCode a ` , we can apply it to the compiled function
171
+ using either the [ ` applyCode ` ] ( https://plutus.cardano.intersectmbo.org/haddock/latest/plutus-tx/PlutusTx-Code.html#v:applyCode ) function
172
+
173
+ <LiteralInclude
174
+ file="Example/Evaluation/Main.hs"
175
+ language="haskell"
176
+ title="Applying compiled function to an argument (type-safe way)"
177
+ start="-- BEGIN SafeApplicationResult"
178
+ end="-- END SafeApplicationResult" />
145
179
146
- Alternatively, we can use the imported ` applyLifted ` function like this:
180
+ or the [ ` unsafeApplyCode ` ] ( https://plutus.cardano.intersectmbo.org/haddock/latest/plutus-tx/PlutusTx-Code.html#v:unsafeApplyCode ) function.
147
181
148
182
<LiteralInclude
149
183
file="Example/Evaluation/Main.hs"
150
184
language="haskell"
151
- title="How to apply compiled function to a lifted argument"
152
- start="-- BEGIN AppliedResult "
153
- end="-- END AppliedResult " />
185
+ title="Applying compiled function to an argument (unsafe way) "
186
+ start="-- BEGIN UnsafeApplicationResult "
187
+ end="-- END UnsafeApplicationResult " />
154
188
155
189
Lets print the result of the evaluation:
156
190
0 commit comments