purescript-python icon indicating copy to clipboard operation
purescript-python copied to clipboard

Documenting how to construct algebraic datatypes from python

Open mikesol opened this issue 5 years ago • 5 comments

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!

mikesol avatar Aug 07 '20 06:08 mikesol

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 avatar Aug 08 '20 22:08 thautwarm

for enums, like Nothing,

nothing = Nothing.value

thautwarm avatar Aug 08 '20 22:08 thautwarm

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!

mikesol avatar Aug 10 '20 04:08 mikesol

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

thautwarm avatar Aug 10 '20 11:08 thautwarm

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>}

thautwarm avatar Aug 10 '20 11:08 thautwarm