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

Error Node And Maybe Compilation Wrapper #129

Merged
merged 5 commits into from
May 30, 2023
Merged

Error Node And Maybe Compilation Wrapper #129

merged 5 commits into from
May 30, 2023

Conversation

agureev
Copy link
Collaborator

@agureev agureev commented May 29, 2023

Adds an error node to the STLC class with possibility to be of an arbitrary type specified by the user. Appropriate helper functions added, such as errorp. Given that a term fed into the compiler now has an error subterm, it will be compiled thorough a Maybe monad as specified by to-cat-err otherwise compiling as usual without waste of computational resources.

Adds test cases for casing compilation to Geb to showcase how the interpreter can help with evaluating errors and where they appear in the STLC code.

This solves issue #90 but further improvements need to be had

@mariari mariari force-pushed the artem/maybe branch 4 times, most recently from 114d6e9 to 68f084d Compare May 30, 2023 07:36
Fixes the compilation of STLC to Geb discarding previous uncurrying
changes in favour of uncurrying only the final result. Further this
commit adds tests to make sure that furhter issues from this problem
does not happen
mariari and others added 3 commits May 30, 2023 16:49
This includes the generic function and all valid instances, and
documentation for how it works
Adds an error node of an arbitrary type as a new STLC
constructor. Appropriatelly changes the compilation procedure to Geb
in case an STLC term has an error subterm and wraps the compilation in
a Maybe monad.
Comment on lines +136 to +230
(labels ((rec (context tterm)
(match-of stlc tterm
((absurd tcod term)
(comp (mcase (->left so1 (maybe-rest tcod))
(init (maybe-comp tcod)))
(rec context term)))
(unit
(comp (->right so1 so1)
(terminal (stlc-ctx-maybe context))))
((left rty term)
(comp (->right so1 (maybe-rest (ttype tterm)))
(comp (->left (maybe-comp (ttype term))
(maybe-comp rty))
(rec context term))))
((right lty term)
(comp (->right so1 (maybe-rest (ttype tterm)))
(comp (->right (maybe-comp lty)
(maybe-comp (ttype term)))
(rec context term))))
((case-on on ltm rtm)
(let ((mcartoon (mcar (ttype on)))
(mcadrtoon (mcadr (ttype on)))
(ctx (stlc-ctx-maybe context)))
(comp (mcase (comp (->left so1 (maybe-rest (ttype tterm)))
(terminal (prod ctx so1)))
(comp (mcase (commutes-left
(rec
(cons mcartoon
context)
ltm))
(commutes-left
(rec
(cons mcadrtoon
context)
rtm)))
(distribute ctx
(maybe-comp mcartoon)
(maybe-comp mcadrtoon))))
(comp (distribute ctx
so1
(maybe-rest (coprod mcartoon
mcadrtoon)))
(geb:pair ctx (rec context on))))))
((pair ltm rtm)
(let ((lty (ttype ltm))
(rty (ttype rtm)))
(comp (->right so1 (prod (maybe-comp lty)
(maybe-comp rty)))
(geb:pair (rec context ltm)
(rec context rtm)))))
((fst term)
(let ((mcarttot (mcar (ttype term))))
(comp (mcase (->left so1 (maybe-rest mcarttot))
(<-left (maybe-comp mcarttot)
(maybe (mcadr (ttype term)))))
(rec context term))))
((snd term)
(let ((mcadrttot (mcadr (ttype term))))
(comp (mcase (->left so1 (maybe-rest mcadrttot))
(<-right (maybe-comp (mcar (ttype term)))
(maybe-comp mcadrttot)))
(rec context term))))
((lamb tdom term)
(comp (->right so1 (maybe-rest (fun-to-hom (ttype tterm))))
(apply-n (length tdom)
#'(lambda (x) (curry (commutes-left x)))
(rec (append
(mapcar #'maybe-comp tdom)
context) term))))
((app fun term)
(let ((tofun (ttype fun)))
(comp
(mcase (->left so1 (maybe-rest tofun))
(commutes-left
(so-eval (maybe-comp (mcar tofun))
(maybe-comp (mcadr tofun)))))
(comp (distribute (reduce #'prod
(mapcar #'maybe-comp term)
:from-end t)
so1
(maybe-rest tofun))
(geb:pair (reduce #'geb:pair
(mapcar #'(lambda (x) (rec context x)) term)
:from-end t)
(rec context
fun))))))
((index pos)
(stlc-ctx-proj (mapcar #'maybe context) pos))
((err ttype)
(comp (->left so1 (maybe-comp ttype))
(terminal (stlc-ctx-maybe context)))))))
(if (not (well-defp context tterm))
(error "not a well-defined ~A in said ~A" tterm context)
(rec context (ann-term1 context tterm)))))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most painful kind of copy and paste.

Let us remove this with a better strategy later, or find a way to abstract it if it has to live here long term

Comment on lines -101 to +289
((left term)
(let ((tott (ttype tterm)))
(comp (->left (mcar tott) (mcadr tott))
(rec context term))))
((right term)
(let ((tott (ttype tterm)))
(comp (->right (mcar tott) (mcadr tott))
(rec context term))))
((left rty term)
(comp (->left (ttype term) rty)
(rec context term)))
((right lty term)
(comp (->right lty (ttype term))
(rec context term)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should do these kinds of changes in a different topic, if they are unrelated to your changes.

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

Successfully merging this pull request may close these issues.

2 participants