Repository navigation
Expand file tree
/
Copy pathMain.hs
More file actions
76 lines (66 loc) · 2.72 KB
/
Copy pathMain.hs
File metadata and controls
76 lines (66 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
module Main where
import System.IO (stdout)
import qualified Control.Exception as E
import Control.Monad (when)
-- All the following modules would normally be imported as Py, but I imported
-- them under unique names for added clarity.
import qualified CPython as Py
import qualified CPython.Types.Module as PyM
import qualified CPython.Types.Exception as PyE
import qualified CPython.Protocols.Object as PyO
import qualified CPython.Protocols.Number as PyN
import qualified CPython.Types as PyT
main :: IO ()
main = do
initCPython True
statefulDemo
-- | Call a stateful function from a Python module.
statefulDemo :: IO ()
statefulDemo = E.handle onException $ do
pymain <- PyM.importModule "pymain"
stateful <- pymain --> "stateful"
let
-- | Call the function "stateful" and evaluate to its return value.
callStateful i = do
Just pyInt <- PyT.toInteger i -- convert i to a Python int
>>= \pyIntArg -> PyO.callArgs stateful [PyO.toObject pyIntArg] -- call
-- callArgs returns a polymorphic type called SomeObject. It needs to
-- be cast to a concrete type so that it can be used. This step is
-- necessary to account for Python's dynamic typing, which would
-- otherwise go unaccounted for in Haskell.
>>= PyO.cast -- cast to type inferred elsewhere or fail with Nothing
PyT.fromInteger pyInt -- this binds the cast above to a Python int and
-- converts that Python int to a Haskell Integer.
printInt int = putStrLn $ "Haskell: " ++ show int
callAndPrint int = callStateful int >>= printInt
callAndPrint 0
callAndPrint 0
callAndPrint 0
callAndPrint 1
callAndPrint 1
callAndPrint 1
-- | Initialize the CPython module and set up import path.
initCPython :: Bool -> IO ()
initCPython verbose = do
-- initialize CPython bindings:
Py.initialize
-- add pwd to the Python path, so that it can find a module located in the
-- current directory:
sys <- PyM.importModule "sys"
path <- sys --> "path"
when verbose $ putStrLn "Current Python path:"
when verbose $ PyO.print path stdout
when verbose $ putStrLn "Adding pwd to Python path..."
pathInsert <- path --> "insert"
newPath <- PyT.toUnicode ""
idx <- PyT.toInteger 0
_ <- PyO.callArgs pathInsert [PyO.toObject idx, PyO.toObject newPath]
when verbose $ PyO.print path stdout
-- Get attribute of object. Python's foo.bar is foo --> bar in our code.
getAttr obj attr = PyO.getAttribute obj =<< PyT.toUnicode attr
(-->) = getAttr -- requires NoMonomorphismRestriction
-- | Print an exception's message.
onException :: PyE.Exception -> IO ()
onException exc = PyO.print (PyE.exceptionValue exc) stdout