Skip to content

Example code cannot compile #23

@Eiko-Tokura

Description

@Eiko-Tokura

When trying to run the example code showing in documentation,

{-# LANGUAGE QuasiQuotes, TypeApplications #-}
module Main where

import Python.Inline
import Python.Inline.QQ

main :: IO ()
main = withPython $ do
  let input = [1..10] :: [Int]
      square :: Int -> Py Int 
      square x = pure $ x * x
  result <- runPy $ fromPy' @[Int] =<< [pye| [ square_hs(x) for x in input_hs ] |]
  print result

This example code throws the following compile time error:

app/Main.hs:13:29: error: [GHC-39999]
    • No instance for ‘ToPy (Int -> Py Int)’
        arising from a use of ‘inline-python-0.1:Python.Internal.EvalQQ.basicBindInDict’
        (maybe you haven't applied a function to enough arguments?)
    • In the expression:
        inline-python-0.1:Python.Internal.EvalQQ.basicBindInDict
          "square_hs" square
      In the second argument of ‘mapM_’, namely
        ‘[inline-python-0.1:Python.Internal.EvalQQ.basicBindInDict
            "square_hs" square,
          inline-python-0.1:Python.Internal.EvalQQ.basicBindInDict
            "input_hs" input]’
      In a stmt of a 'do' block:
        mapM_
          ($ p_dict_a2Mb)
          [inline-python-0.1:Python.Internal.EvalQQ.basicBindInDict
             "square_hs" square,
           inline-python-0.1:Python.Internal.EvalQQ.basicBindInDict
             "input_hs" input]
   |
13 |     fromPy' @[Int] =<< [pye| [ square_hs(x) for x in input_hs ] |]
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

However, after reading the documentation about ToPy class, it seems that there is an instance ToPy (a -> IO b), we only need to change square :: Int -> Py Int to square :: Int -> IO Int, which compiles and runs as expected.

I'm using inline-python version 0.1, downloaded via hackage.

Btw this is a great project!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions