user2236: 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.

user2236: 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:


user1130: 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
As for logging inside of validators, I'd look at using trace, traceIfTrue, or traceIfFalse. 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.

2236: <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>
 2236: <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>
 1130: <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>
