Skip to main content
Bumped by Community user
Bumped by Community user
added 879 characters in body
Source Link
Avatar33
  • 201
  • 1
  • 5

It seems that only a function with no pattern matching or recursion works here.

    checkTokenName :: TokenName ->  Bool
    checkTokenName tn = lengthOfByteString bs > 3
                        && lengthOfByteString bs < 6
                        && checkInt tokNum tokNumLen
        where
            bs = unTokenName tn
            tokNum = sliceByteString 3 2 bs
            tokNumLen = lengthOfByteString tokNum
            isNum i = Builtins.greaterThanInteger i 47 && Builtins.lessThanInteger i 58
            checkInt :: BuiltinByteString -> Integer -> Bool
            -- No recursion or pattern matching !?!? 
            checkInt bs' len = (len == 1 && isNum (indexByteString bs' 0)) ||
                               (len == 2 && isNum (indexByteString bs' 0) &&
                                            isNum (indexByteString bs' 1))

It seems that only a function with no pattern matching or recursion works here.

    checkTokenName :: TokenName ->  Bool
    checkTokenName tn = lengthOfByteString bs > 3
                        && lengthOfByteString bs < 6
                        && checkInt tokNum tokNumLen
        where
            bs = unTokenName tn
            tokNum = sliceByteString 3 2 bs
            tokNumLen = lengthOfByteString tokNum
            isNum i = Builtins.greaterThanInteger i 47 && Builtins.lessThanInteger i 58
            checkInt :: BuiltinByteString -> Integer -> Bool
            -- No recursion or pattern matching !?!? 
            checkInt bs' len = (len == 1 && isNum (indexByteString bs' 0)) ||
                               (len == 2 && isNum (indexByteString bs' 0) &&
                                            isNum (indexByteString bs' 1))

Added an updated failure when using the suggested solution.
Source Link
Avatar33
  • 201
  • 1
  • 5

Update 3

I tried the advise of Sjoerd Visscher, but unfortunately I get the same type of error if I use any function with recursion or a pattern matching. For example

    checkTokenName :: TokenName ->  Bool
    checkTokenName tn = lengthOfByteString bs > 3
                        && checkInt tokNum tokNumLen
        where
            bs = unTokenName tn
            tokNum = sliceByteString 3 2 bs
            tokNumLen = lengthOfByteString tokNum
            checkInt _ 0 = True
            checkInt bs' i =  True && checkInt bs' (i-1)

Results in:

GHC Core to PLC plugin: E042:Error: Unsupported feature: Use of Haskell Integer equality, possibly via the Haskell Eq typeclass
Context: Compiling expr: GHC.Integer.Type.eqInteger#
Context: Compiling expr: GHC.Integer.Type.eqInteger# ds_d12x1
Context: Compiling expr: GHC.Integer.Type.eqInteger# ds_d12x1 0
Context: Compiling expr: case GHC.Integer.Type.eqInteger# ds_d12x1 0
                         of wild [Occ=Once]
                         { __DEFAULT ->
                         GHC.Prim.tagToEnum# @ GHC.Types.Bool wild
                         }
...

Update 3

I tried the advise of Sjoerd Visscher, but unfortunately I get the same type of error if I use any function with recursion or a pattern matching. For example

    checkTokenName :: TokenName ->  Bool
    checkTokenName tn = lengthOfByteString bs > 3
                        && checkInt tokNum tokNumLen
        where
            bs = unTokenName tn
            tokNum = sliceByteString 3 2 bs
            tokNumLen = lengthOfByteString tokNum
            checkInt _ 0 = True
            checkInt bs' i =  True && checkInt bs' (i-1)

Results in:

GHC Core to PLC plugin: E042:Error: Unsupported feature: Use of Haskell Integer equality, possibly via the Haskell Eq typeclass
Context: Compiling expr: GHC.Integer.Type.eqInteger#
Context: Compiling expr: GHC.Integer.Type.eqInteger# ds_d12x1
Context: Compiling expr: GHC.Integer.Type.eqInteger# ds_d12x1 0
Context: Compiling expr: case GHC.Integer.Type.eqInteger# ds_d12x1 0
                         of wild [Occ=Once]
                         { __DEFAULT ->
                         GHC.Prim.tagToEnum# @ GHC.Types.Bool wild
                         }
...
added 799 characters in body
Source Link
Avatar33
  • 201
  • 1
  • 5

Update 2

I'm inching closer to a solution based on the feedback in the comments and in the Q&A that happened on 15 Feb Here is the updated attempt to use only inlinable functions

    -- Check that TokenName is of the form ABC1 .. ABC99
    checkTokenName :: TokenName ->  Bool
    checkTokenName tn = lengthOfByteString bs > 3
                        && checkInt tokNum
        where
            bs = unTokenName tn
            tokNum = sliceByteString 3 2 bs
            tokNumLen = lengthOfByteString tokNum
            checkInt bs' = all (\i -> Builtins.greaterThanInteger i 47 && Builtins.lessThanInteger i 58)
                            -- Seems this list comprehension causes issues
                            [indexByteString bs' i | i <- [0..tokNumLen]]

Update 2

I'm inching closer to a solution based on the feedback in the comments and in the Q&A that happened on 15 Feb Here is the updated attempt to use only inlinable functions

    -- Check that TokenName is of the form ABC1 .. ABC99
    checkTokenName :: TokenName ->  Bool
    checkTokenName tn = lengthOfByteString bs > 3
                        && checkInt tokNum
        where
            bs = unTokenName tn
            tokNum = sliceByteString 3 2 bs
            tokNumLen = lengthOfByteString tokNum
            checkInt bs' = all (\i -> Builtins.greaterThanInteger i 47 && Builtins.lessThanInteger i 58)
                            -- Seems this list comprehension causes issues
                            [indexByteString bs' i | i <- [0..tokNumLen]]

added 337 characters in body
Source Link
Avatar33
  • 201
  • 1
  • 5
Loading
Source Link
Avatar33
  • 201
  • 1
  • 5
Loading