<p>I'll use this answer to log my journey as I try things out.</p>
<h2>Tentative Solution/Answer</h2>
<p>Looks like <code>P.show</code> can't be used in a validator based on <a href="https://discord.com/channels/826816523368005654/826829805387120690/892870961353879592" rel="nofollow noreferrer">https://discord.com/channels/826816523368005654/826829805387120690/892870961353879592</a></p>
<p>I'll wait for more confirmation before accepting this. I hope that's not the case.</p>
<h2>Attempt 1</h2>
<p>I've also tried using <code>trace</code> with</p>
<pre><code>mkValidator dat _ ctx = do
    trace $ printf &quot;deadline %s&quot; (P.show $ deadline dat)
    False
but that also results in an error:

• Couldn't match expected type ‘Bool’ with actual type ‘a0 -&gt; b0’
• In a stmt of a 'do' block:
    trace $ printf &quot;deadline %s&quot; (P.show $ deadline dat)
  In the expression:
    do trace $ printf &quot;deadline %s&quot; (P.show $ deadline dat)
       False
  In an equation for ‘mkValidator’:
      mkValidator dat _ ctx
        = do trace $ printf &quot;deadline %s&quot; (P.show $ deadline dat)
             False
</code></pre>
<h2>Attempt 2</h2>
<p>Looks like using <code>do</code> in a validator is a fools errand. Now I'm trying to add debug info to the trace message.</p>
<pre><code>mkValidator :: VestingDatum -&gt; () -&gt; ScriptContext -&gt; Bool
mkValidator dat _ ctx  = traceIfFalse (P.show $ deadline dat) False
</code></pre>
<p>but it turns out that <code>P.show</code> doesn't return the correct type (it returns <code>[P.Char]</code> when a <code>BuiltinString</code> is required). Trying to coerce <code>[P.Char]</code> to a <code>BuiltinString</code> using <code>stringToBuiltinString</code> from <a href="https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Builtins-Class.html#v:stringToBuiltinString" rel="nofollow noreferrer">https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Builtins-Class.html#v:stringToBuiltinString</a> results in a very large error that looks like linker problems:</p>
<blockquote>
<p><code>GHC Core to PLC plugin: E043:Error: Reference to a name which is not a local, a builtin, or an external INLINABLE function: Variable GHC.CString.unpackAppendCString# No unfolding Context: Compiling expr: GHC.CString.unpackAppendCString# Context: Compiling expr: GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 Context: Compiling expr: GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 (GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime3 (case GHC.Show.$w$cshowsPrec4 0# (x `cast` (Plutus.V1.Ledger.Time.N:POSIXTime[0] :: GHC.Types.Coercible Plutus.V1.Ledger.Time.POSIXTime GHC.Integer.Type.Integer)) Plutus.V1.Ledger.Time.$fShowPOSIXTime5 of { (# ww3 [Occ=Once], ww4 [Occ=Once] #) -&gt; GHC.Types.: @ GHC.Types.Char ww3 ww4 })) Context: Compiling expr: \ (x [Occ=Once] :: Plutus.V1.Ledger.Time.POSIXTime) -&gt; GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 (GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime3 (case GHC.Show.$w$cshowsPrec4 0# (x `cast` (Plutus.V1.Ledger.Time.N:POSIXTime[0] :: GHC.Types.Coercible Plutus.V1.Ledger.Time.POSIXTime GHC.Integer.Type.Integer)) Plutus.V1.Ledger.Time.$fShowPOSIXTime5 of { (# ww3 [Occ=Once], ww4 [Occ=Once] #) -&gt; GHC.Types.: @ GHC.Types.Char ww3 ww4 })) Context: Compiling definition of: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow Context: Compiling expr: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow Context: Compiling expr: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow (case dat of { Main.VestingDatum _ [Occ=Dead] _ [Occ=Dead] ds_dLa0 [Occ=Once] -&gt; ds_dLa0 }) Context: Compiling expr:</code></p>
</blockquote>

CC BY-SA 4.0
2021-09-30T13:06:21.687Z
P.show will always fail inside a validator, because you can't use functions defined in Haskell Prelude, you need to use instead PlutusTx.Prelude
Mateus
CC BY-SA 4.0
2021-10-01T16:24:56.567Z
Does PlutusTx.Prelude include a `show` function? I couldn't find one but maybe I wasn't looking in the right place.
Paymahn Moghadasian
CC BY-SA 4.0
2021-10-13T16:50:49.020Z
There isn't, since `show` transforms anything into a `String` and PlutusTx.Prelude only works with `BuiltinString` or `BuiltinByteString`
Mateus
CC BY-SA 4.0
2022-12-09T08:04:13.353Z
Same here. Get this error every time I there is a variable in the validator.
Adam
CC BY-SA 4.0
2021-09-29T20:23:52.630Z
2021-09-29T20:34:53.417Z
2021-09-29T20:34:53.417Z
Paymahn Moghadasian
<p>You're running into trouble because <code>logInfo</code> is meant to be used inside the Contract monad. The Wallet code uses the Contract monad but the validator code doesn't.</p>
<p>If you're not familiar with how monads and <code>do</code> blocks work, I'd check out <a href="https://www.youtube.com/watch?v=g4lvA14I-Jg" rel="noreferrer">Lecture #4</a></p>
<p>As for logging inside of validators, I'd look at using <code>trace</code>, <code>traceIfTrue</code>, or <a href="https://github.com/input-output-hk/plutus/blob/30b293767d61a556586d16634c66bd0ce2b816da/plutus-use-cases/src/Plutus/Contracts/Uniswap/OnChain.hs#L48" rel="noreferrer"><code>traceIfFalse</code></a>. I've only used <code>traceIfFalse</code> though. Remember, validators are compiled into Plutus and run on the chain, so they aren't going to log like normal code. Whereas the wallet code is run natively on your machine.</p>
<p>Hope that helps.</p>

CC BY-SA 4.0
2021-09-30T13:08:20.613Z
Is it possible to use that combined with an arbitrary variable? For example, if I want to dynamically display the number of tokens that are missing for the script to validate.
Mateus
CC BY-SA 4.0
2021-10-01T14:57:41.817Z
Thanks for the great answer! I stumbled on that myself but it's good to know what I found myself is accurate. @Mateus I don't think you can log arbitrary variables within `trace` or it's derivatives because the Plutus Prelude doesn't offer a `show` function.
Paymahn Moghadasian
CC BY-SA 4.0
2021-09-29T23:24:04.280Z
2021-09-29T23:24:04.280Z
Mitchell Turner
<p>Validators are compiled into Plutus Core language which is human-unreadable. It's not possible to compile something that will be only available at run-time because it's literally not there to be compiled. You can log variables that are available at compiler.</p>
<blockquote>
<p>GHC Core to PLC plugin: E043:Error: Reference to a name which is not a local, a builtin, or an external INLINABLE function</p>
</blockquote>
<p>This error means that you are trying to compile something that will be available at run-time.</p>

CC BY-SA 4.0
2022-01-25T17:16:57.610Z
2022-01-25T17:16:57.610Z
Tuvshintsenguun
<p>I'm working through the Plutus Pioneer program and I have a bug in my implementation of the validator for homework one. I'd like to add some logging to the validator to get more insight into what I'm doing wrong.</p>
<p>I've seen that the <code>logInfo</code> function is used in the wallet code and I've tried adding that to the validator like:</p>
<pre><code>mkValidator dat _ ctx = do
    logInfo @P.String $ printf &quot;deadline %s&quot; (P.show $ deadline dat)
    False -- dummy value to figure out how to get logging to work
</code></pre>
<p>but when I do that I get the following error:</p>
<pre><code>• Couldn't match expected type ‘Bool’
              with actual type ‘Contract w0 s0 e0 b0’
• In a stmt of a 'do' block:
    logInfo @P.String $ printf &quot;deadline %s&quot; (P.show $ deadline dat)
  In the expression:
    do logInfo @P.String $ printf &quot;deadline %s&quot; (P.show $ deadline dat)
       False
  In an equation for ‘mkValidator’:
      mkValidator dat _ ctx
        = do logInfo @P.String
               $ printf &quot;deadline %s&quot; (P.show $ deadline dat)
             False
</code></pre>
<p>Upon checking the type of <code>logInfo</code> I see that it has the type <code>logInfo :: forall a w (s :: Row *) e. ToJSON a =&gt; a -&gt; Contract w s e ()</code> which seems to match up with the error that's given.</p>
<p>How come I can't use this logging function if I'm later returning <code>False</code>? I suspect I'm misunderstanding something fairly basic about how haskell works.</p>

<plutus><haskell>
Logging in validator
# Logging in validator

(Asked by: username_0 on 2021-09-29)

I'm working through the Plutus Pioneer program and I have a bug in my implementation of the validator for homework one. I'd like to add some logging to the validator to get more insight into what I'm doing wrong.
I've seen that the logInfo function is used in the wallet code and I've tried adding that to the validator like:
mkValidator dat _ ctx = do
    logInfo @P.String $ printf "deadline %s" (P.show $ deadline dat)
    False -- dummy value to figure out how to get logging to work

but when I do that I get the following error:
• Couldn't match expected type ‘Bool’
              with actual type ‘Contract w0 s0 e0 b0’
• In a stmt of a 'do' block:
    logInfo @P.String $ printf "deadline %s" (P.show $ deadline dat)
  In the expression:
    do logInfo @P.String $ printf "deadline %s" (P.show $ deadline dat)
       False
  In an equation for ‘mkValidator’:
      mkValidator dat _ ctx
        = do logInfo @P.String
               $ printf "deadline %s" (P.show $ deadline dat)
             False

Upon checking the type of logInfo I see that it has the type logInfo :: forall a w (s :: Row *) e. ToJSON a => a -> Contract w s e () which seems to match up with the error that's given.
How come I can't use this logging function if I'm later returning False? I suspect I'm misunderstanding something fairly basic about how haskell works.

**Tags:** plutus, haskell

---

## 3 answers

### Answer by username_0 on 2021-09-29. Score: 1 | Accepted answer

I'll use this answer to log my journey as I try things out.
Tentative Solution/Answer
Looks like P.show can't be used in a validator based on https://discord.com/channels/826816523368005654/826829805387120690/892870961353879592
I'll wait for more confirmation before accepting this. I hope that's not the case.
Attempt 1
I've also tried using trace with
mkValidator dat _ ctx = do
    trace $ printf "deadline %s" (P.show $ deadline dat)
    False
but that also results in an error:

• Couldn't match expected type ‘Bool’ with actual type ‘a0 -> b0’
• In a stmt of a 'do' block:
    trace $ printf "deadline %s" (P.show $ deadline dat)
  In the expression:
    do trace $ printf "deadline %s" (P.show $ deadline dat)
       False
  In an equation for ‘mkValidator’:
      mkValidator dat _ ctx
        = do trace $ printf "deadline %s" (P.show $ deadline dat)
             False

Attempt 2
Looks like using do in a validator is a fools errand. Now I'm trying to add debug info to the trace message.
mkValidator :: VestingDatum -> () -> ScriptContext -> Bool
mkValidator dat _ ctx  = traceIfFalse (P.show $ deadline dat) False

but it turns out that P.show doesn't return the correct type (it returns [P.Char] when a BuiltinString is required). Trying to coerce [P.Char] to a BuiltinString using stringToBuiltinString from https://playground.plutus.iohkdev.io/doc/haddock/plutus-tx/html/PlutusTx-Builtins-Class.html#v:stringToBuiltinString results in a very large error that looks like linker problems:

GHC Core to PLC plugin: E043:Error: Reference to a name which is not a local, a builtin, or an external INLINABLE function: Variable GHC.CString.unpackAppendCString# No unfolding Context: Compiling expr: GHC.CString.unpackAppendCString# Context: Compiling expr: GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 Context: Compiling expr: GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 (GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime3 (case GHC.Show.$w$cshowsPrec4 0# (x `cast` (Plutus.V1.Ledger.Time.N:POSIXTime[0] :: GHC.Types.Coercible Plutus.V1.Ledger.Time.POSIXTime GHC.Integer.Type.Integer)) Plutus.V1.Ledger.Time.$fShowPOSIXTime5 of { (# ww3 [Occ=Once], ww4 [Occ=Once] #) -> GHC.Types.: @ GHC.Types.Char ww3 ww4 })) Context: Compiling expr: \ (x [Occ=Once] :: Plutus.V1.Ledger.Time.POSIXTime) -> GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime4 (GHC.CString.unpackAppendCString# Plutus.V1.Ledger.Time.$fShowPOSIXTime3 (case GHC.Show.$w$cshowsPrec4 0# (x `cast` (Plutus.V1.Ledger.Time.N:POSIXTime[0] :: GHC.Types.Coercible Plutus.V1.Ledger.Time.POSIXTime GHC.Integer.Type.Integer)) Plutus.V1.Ledger.Time.$fShowPOSIXTime5 of { (# ww3 [Occ=Once], ww4 [Occ=Once] #) -> GHC.Types.: @ GHC.Types.Char ww3 ww4 })) Context: Compiling definition of: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow Context: Compiling expr: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow Context: Compiling expr: Plutus.V1.Ledger.Time.$fShowPOSIXTime_$cshow (case dat of { Main.VestingDatum _ [Occ=Dead] _ [Occ=Dead] ds_dLa0 [Occ=Once] -> ds_dLa0 }) Context: Compiling expr:

> **Comments:**
> **username_1**: P.show will always fail inside a validator, because you can't use functions defined in Haskell Prelude, you need to use instead PlutusTx.Prelude  
> **username_0**: Does PlutusTx.Prelude include a `show` function? I couldn't find one but maybe I wasn't looking in the right place.  
> **username_1**: There isn't, since `show` transforms anything into a `String` and PlutusTx.Prelude only works with `BuiltinString` or `BuiltinByteString`  
> **username_2**: Same here. Get this error every time I there is a variable in the validator.  

---

### Answer by username_3 on 2021-09-29. Score: 5

You're running into trouble because `logInfo` is meant to be used inside the Contract monad. The Wallet code uses the Contract monad but the validator code doesn't.

If you're not familiar with how monads and `do` blocks work, I'd check out [Lecture #4](https://www.youtube.com/watch?v=g4lvA14I-Jg)

As for logging inside of validators, I'd look at using `trace`, `traceIfTrue`, or [`traceIfFalse`](https://github.com/input-output-hk/plutus/blob/30b293767d61a556586d16634c66bd0ce2b816da/plutus-use-cases/src/Plutus/Contracts/Uniswap/OnChain.hs#L48). I've only used `traceIfFalse` though. Remember, validators are compiled into Plutus and run on the chain, so they aren't going to log like normal code. Whereas the wallet code is run natively on your machine.

Hope that helps.

> **Comments:**
> **username_1**: Is it possible to use that combined with an arbitrary variable? For example, if I want to dynamically display the number of tokens that are missing for the script to validate.  
> **username_0**: Thanks for the great answer! I stumbled on that myself but it's good to know what I found myself is accurate. @Mateus I don't think you can log arbitrary variables within `trace` or it's derivatives because the Plutus Prelude doesn't offer a `show` function.  

---

### Answer by username_4 on 2022-01-25. Score: 0

Validators are compiled into Plutus Core language which is human-unreadable. It's not possible to compile something that will be only available at run-time because it's literally not there to be compiled. You can log variables that are available at compiler.

> GHC Core to PLC plugin: E043:Error: Reference to a name which is not a local, a builtin, or an external INLINABLE function

This error means that you are trying to compile something that will be available at run-time.


---

