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

Combining pure and impure fragments #2

Open
mpickering opened this issue Aug 22, 2018 · 0 comments
Open

Combining pure and impure fragments #2

mpickering opened this issue Aug 22, 2018 · 0 comments

Comments

@mpickering
Copy link
Owner

If we consider the staged power function, we observe a fundamental difficulty of writing programs polymorphic in their stage.

staticPower_s :: Syntax r => Int -> r Int -> r Int                              
staticPower_s n k = if n == 0                                                   
                                     then pure 1                                               
                                     else ([ k * (staticPower_s (n - 1) k) ])  

Notice that the conditional and scrutinee are both statically evaluated using the non-overloaded versions of if and ap.

However, we could also use the overloaded variants and then instantiate them to Identity. The hope being that this would provide a more uniform treatment.

staticPower_s :: Syntax r => Int -> r Int -> r Int                              
staticPower_s n k = runIdentity $ _if (pure (==) <*> pure n <*> pure 0)                                                   
                                         (pure (pure 1)                                               
                                         (pure ([ k * (staticPower_s (n - 1) k) ])) 

The type of the _if :: Identity Bool -> Identity (r Int) -> Identity (r Int) -> Identity (r Int)

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

1 participant