Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documenting how to construct algebraic datatypes from python #35

Open
mikesol opened this issue Aug 7, 2020 · 5 comments
Open

Documenting how to construct algebraic datatypes from python #35

mikesol opened this issue Aug 7, 2020 · 5 comments

Comments

@mikesol
Copy link

mikesol commented Aug 7, 2020

For interop, I'm having trouble calling a purescript function from python. If I have:

just :: Int -> Maybe Int
just = Just

addMaybe :: Maybe Int -> Maybe Int -> Maybe Int
addMaybe a b = (+) <$> a <*> b

Then this works:

import foo.Main.pure as Main
print(Main.addMaybe(Main.just(1))(Main.just(2)))

This produces the wrong output (it yields nothing):

import foo.Main.pure as Main
import foo.Data.Maybe.pure as Maybe
Just = Maybe.Just
print(Main.addMaybe({'value0': 3, '.t': Just})({'value0': 3, '.t': Just}))

and this crashes.

import foo.Main.pure as Main
import foo.Data.Maybe.pure as Maybe
Just = Maybe.Just
print(Main.addMaybe(Just(1))(Just(2)))

The first solution is of course fine, but it requires more boilerplate, and I'm assuming it must be possible to use the Just constructor or the Nothing constructor directly somehow.

Thanks!

@thautwarm
Copy link
Member

for

data C = Ctor Int Int

to create Ctor:

def new(t): return {'.t': t}
Ctor(1, new(Ctor))
Ctor.create(1)(2)

Sorry for late reply!
I'm now working for some technique meetup, will add documemtations after Aug 15

@thautwarm
Copy link
Member

for enums, like Nothing,

nothing = Nothing.value

@mikesol
Copy link
Author

mikesol commented Aug 10, 2020

Thanks!

I tried this, but it looks like the problem still persists:

-- snippet from Main.purs
addM :: Maybe Int -> Maybe Int -> Maybe Int
addM a b = (+) <$> a <*> b
# testMain.py
import pspy.Main.pure as Main
import pspy.Data.Maybe.pure as Maybe

print(Maybe.Just.create(1))
print(Main.addM(Maybe.Just.create(1))(Maybe.Just.create(2)))

yields

{'value0': 1, '.t': <function ps_Just at 0x7f19302ea710>}
{'.t': <function ps_Nothing at 0x7f1930349b90>}

Thanks in advance for your help!

thautwarm added a commit to purescript-python/purescripto that referenced this issue Aug 10, 2020
@thautwarm
Copy link
Member

Sorry, purescripto 0.8.9 didn't release. Now pip install -U purescripto fixes all.

@thautwarm
Copy link
Member

In [9]: import purescript_test.Main.pure as Main 
   ...: import purescript_test.Data.Maybe.pure as Maybe 
   ...:  
   ...: print(Maybe.Just.create(1)) 
   ...: print(Main.addM(Maybe.Just.create(1))(Maybe.Just.create(2)))                                                                   
{'value0': 1, '.t': <function ps_Just at 0x7f8ec3bf0c10>}
{'value0': 3, '.t': <function ps_Just at 0x7f8ec3bf0c10>}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants