Currying

From mojo_puzzler
Jump to navigation Jump to search
Return to: Chialisp

Example 1

  • cdv clsp curry -i include io_puz.clvm -a 0x7cac37d6245033e764e02b27b4898645df9dbd5ea5d2d8ef030cfa600ca33090 -a 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 -a ff01ffff33ffa0656214f18f80a85a30f97cafa0fdfd29578d5e8030316b21e36552f9172c49d1ff018080 -x

Example 2

  • cdv clsp curry -i include io_puz.clvm -a 0x7cac37d6245033e764e02b27b4898645df9dbd5ea5d2d8ef030cfa600ca33090 -a 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 -a ff01ffff33ffa0656214f18f80a85a30f97cafa0fdfd29578d5e8030316b21e36552f9172c49d1ff018080 --treehash

Example 3

Take for instance the password protected coin tutorial example which has a puzzle that looks like this


(mod (password new_puzhash amount)
   (defconstant CREATE_COIN 51)
   (if (= (sha256 password) (q . 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824))
       (list (list CREATE_COIN new_puzhash amount))
       (x)
   )
)

which yields clvm that looks like this (puzzle_hash)

(a (q 2 (i (= (sha256 5) (q . 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824)) (q 4 (c 2 (c 11 (c 23 ()))) ()) (q 8)) 1) (c (q . 51) 1))

and a solution that looks like this

(q . (hello 0xe57f6cc4a9f35ee60b95889a8c8f2e5e7a273a9fcf716276ee1f4c6f932b7ea6 1))


We can accomplish the same thing by currying... Curried version:

(mod (HASHED_PASSWORD password new_puzhash amount)
   (defconstant CREATE_COIN 51)
   (if (= (sha256 password) HASHED_PASSWORD)
       (list (list CREATE_COIN new_puzhash amount))
       (x)
   )
)

The convention is to ALL_CAPS values that will be curried in. We can actually curry in the same hashed password from our tutorial example by doing

cdv clsp curry -a 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 curry_ppc.clvm

which yields puzzle_hash

(a (q 2 (q 2 (i (= (sha256 11) 5) (q 4 (c 2 (c 23 (c 47 ()))) ()) (q 8)) 1) (c (q . 51) 1)) (c (q . 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824) 1))

The resulting clvm code is slightly different from non-curried versus curried, yet they both spend to the same solution so it seems currying is just technique preference.

My understanding is that currying in values like this makes the base Chialisp code more modular and reusable. In the case of password protected coin tutorial you could expect a changed password frequently.