<p>I am trying to learn the basics of the PlutusTx module. In this module is a function <code>applyCode</code> which maps the signatures <code>CompiledCodeIn uni fun (a -&gt; b) -&gt; CompiledCodeIn uni fun a -&gt; CompiledCodeIn uni fun b</code>. In my understanding this function is there to splice in inputs. So given the following validator</p>
<pre><code>{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE NoImplicitPrelude   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE TypeOperators       #-}

module Modules.Contract where

import           Control.Monad       hiding (fmap)
import           Data.Map            as Map
import           Data.Text           (Text)
import           Data.Void           (Void)
import           Plutus.Contract
import           PlutusTx            as PlutusTx
import           PlutusTx.Prelude    as P hiding (Semigroup(..), unless)
import           Ledger              hiding (singleton)
import           Ledger.Constraints  as Constraints
import qualified Ledger.Scripts      as Scripts
import           Ledger.Ada          as Ada
import           Playground.Contract (printJson, printSchemas, ensureKnownCurrencies, stage)
import           Playground.TH       (mkKnownCurrencies, mkSchemaDefinitions)
import           Playground.Types    (KnownCurrency (..))
import           Prelude             (IO, Semigroup (..), String)
import           Text.Printf         (printf)

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
someList :: [Integer]
someList = [1,2,3]

someListBuiltin :: BuiltinData
someListBuiltin = PlutusTx.toBuiltinData someList

{-# INLINABLE mkValidator #-}
mkValidator :: BuiltinData -&gt; BuiltinData -&gt; BuiltinData -&gt; BuiltinData -&gt; ()
mkValidator someListBuiltin _ r _
        | (someListBuiltin P.!! 1) == r = ()
        | otherwise                     = traceError &quot;wrong redeemer&quot;

</code></pre>
<p>Notice that I use BuiltinData instead of a typed version and I import the <code>Plutus.Prelude</code> as <code>P</code>. This simple validator checks if the redeemer is the first element in the list (so if r = (I 1) it would validate. Now how do I splice this pre determined list in the validator via template haskell? My attempt by using applyCode in infix style</p>
<pre><code>validator :: Validator
validator = mkValidatorScript ($$(PlutusTx.compile [|| mkValidator ||]) `PlutusTx.applyCode` PlutusTx.liftCode someListBuiltin)
</code></pre>
<p>For a typed validator I got this to work.</p>

CC BY-SA 4.0
2022-01-26T14:54:56.767Z
What's the error you get?
Sjoerd Visscher
CC BY-SA 4.0
2022-01-26T15:03:57.537Z
I managed to pin it down to the fact that the P.!! function is not applicable on BuiltinData. If one wants to script with BuiltinData, do you have to convert everything back to Data to use the Plutus.Prelude functions? I got the applyCode working now (it was a wrong module import).
Fermat
CC BY-SA 4.0
2022-01-26T15:47:01.757Z
Ah, it might be a better idea to use `mkTypedValidatorScript` which will do the conversions for you.
Sjoerd Visscher
CC BY-SA 4.0
2022-01-26T16:15:34.763Z
I agree, but everything should be possible in the non-typed version as well right? Lars said in his lecture that the non-typed validator where smaller in size since the conversion is skipped. So thats why I wanted to learn the non-typed style too.
Fermat
CC BY-SA 4.0
2022-01-27T08:42:44.397Z
In that case you'll have to pattern match on the `Data` constructors, there won't be functions like `(!!)` to help you.
Sjoerd Visscher
CC BY-SA 4.0
2022-01-27T09:51:25.460Z
But one can use `(<$>) :: Functor f => (a -> b) -> f a -> f b` to circumvent that right? its no problem that type signatures need checking more. Also I am not scared to use some monadic structures, better to learn real haskell by doing :)
Fermat
CC BY-SA 4.0
2022-01-27T10:02:26.000Z
I don't think so, not sure which Functor you're thinking of here.
Sjoerd Visscher
CC BY-SA 4.0
2022-01-27T10:18:06.507Z
I am not a haskell legend but from the type signature deduced that since Data is just some wrapper around Integer, list, Bytestring etc we should have such a function that tries to apply to the internal type. Thanks for the help Sjoerd!
Fermat
CC BY-SA 4.0
2022-01-28T10:01:12.393Z
That's sadly not how it works, but I like how you're thinking! If you pattern match on `List xs`, you can then do `xs !! 0`.
Sjoerd Visscher
<plutus><plutus-pioneer-program><haskell><validator>
Applycode to untyped validator, template Haskell
# Applycode to untyped validator, template Haskell

(Asked by: username_0 on 2022-01-26)

I am trying to learn the basics of the PlutusTx module. In this module is a function applyCode which maps the signatures CompiledCodeIn uni fun (a -> b) -> CompiledCodeIn uni fun a -> CompiledCodeIn uni fun b. In my understanding this function is there to splice in inputs. So given the following validator
{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE NoImplicitPrelude   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeFamilies        #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE TypeOperators       #-}

module Modules.Contract where

import           Control.Monad       hiding (fmap)
import           Data.Map            as Map
import           Data.Text           (Text)
import           Data.Void           (Void)
import           Plutus.Contract
import           PlutusTx            as PlutusTx
import           PlutusTx.Prelude    as P hiding (Semigroup(..), unless)
import           Ledger              hiding (singleton)
import           Ledger.Constraints  as Constraints
import qualified Ledger.Scripts      as Scripts
import           Ledger.Ada          as Ada
import           Playground.Contract (printJson, printSchemas, ensureKnownCurrencies, stage)
import           Playground.TH       (mkKnownCurrencies, mkSchemaDefinitions)
import           Playground.Types    (KnownCurrency (..))
import           Prelude             (IO, Semigroup (..), String)
import           Text.Printf         (printf)

{-# OPTIONS_GHC -fno-warn-unused-imports #-}
someList :: [Integer]
someList = [1,2,3]

someListBuiltin :: BuiltinData
someListBuiltin = PlutusTx.toBuiltinData someList

{-# INLINABLE mkValidator #-}
mkValidator :: BuiltinData -> BuiltinData -> BuiltinData -> BuiltinData -> ()
mkValidator someListBuiltin _ r _
        | (someListBuiltin P.!! 1) == r = ()
        | otherwise                     = traceError "wrong redeemer"


Notice that I use BuiltinData instead of a typed version and I import the Plutus.Prelude as P. This simple validator checks if the redeemer is the first element in the list (so if r = (I 1) it would validate. Now how do I splice this pre determined list in the validator via template haskell? My attempt by using applyCode in infix style
validator :: Validator
validator = mkValidatorScript ($$(PlutusTx.compile [|| mkValidator ||]) `PlutusTx.applyCode` PlutusTx.liftCode someListBuiltin)

For a typed validator I got this to work.

**Tags:** plutus, plutus-pioneer-program, haskell, validator
> **Comments:**
> **username_1**: What's the error you get?  
> **username_0**: I managed to pin it down to the fact that the P.!! function is not applicable on BuiltinData. If one wants to script with BuiltinData, do you have to convert everything back to Data to use the Plutus.Prelude functions? I got the applyCode working now (it was a wrong module import).  
> **username_1**: Ah, it might be a better idea to use `mkTypedValidatorScript` which will do the conversions for you.  
> **username_0**: I agree, but everything should be possible in the non-typed version as well right? Lars said in his lecture that the non-typed validator where smaller in size since the conversion is skipped. So thats why I wanted to learn the non-typed style too.  
> **username_1**: In that case you'll have to pattern match on the `Data` constructors, there won't be functions like `(!!)` to help you.  
> **username_0**: But one can use `(<$>) :: Functor f => (a -> b) -> f a -> f b` to circumvent that right? its no problem that type signatures need checking more. Also I am not scared to use some monadic structures, better to learn real haskell by doing :)  
> **username_1**: I don't think so, not sure which Functor you're thinking of here.  
> **username_0**: I am not a haskell legend but from the type signature deduced that since Data is just some wrapper around Integer, list, Bytestring etc we should have such a function that tries to apply to the internal type. Thanks for the help Sjoerd!  
> **username_1**: That's sadly not how it works, but I like how you're thinking! If you pattern match on `List xs`, you can then do `xs !! 0`.  

---

## 0 answers

