Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Known pattern match in agda

Tags:

haskell

agda

Roughly, I have

check : UExpr -> Maybe Expr

And I have a test term

testTerm : UExpr

Which I hope will check successfully, after which I want to extract the resulting Expr and manipulate it further. Basically

realTerm : Expr
just realTerm = check testTerm

Such that this definition would fail to typecheck if check testTerm turned out to be nothing. Is this possible?

like image 592
luqui Avatar asked Sep 14 '16 07:09

luqui


2 Answers

The usual deal is to write something like

Just : {X : Set} -> Maybe X -> Set
Just (just x) = One -- or whatever you call the fieldless record type
Just nothing = Zero

justify : {X : Set}(m : Maybe X){p : Just m} -> X
justify (just x) = x
justify nothing {()}

If m computes to a success, the type of p is One and the value is inferred.

like image 112
pigworker Avatar answered Oct 02 '22 02:10

pigworker


Well I found one way to do it, which is sortof bizarre and magical.

testTerm-checks : Σ Expr (\e -> check testTerm ≡ just e)
testTerm-checks = _ , refl

realTerm : Expr
realTerm = proj₁ testTerm-checks

This gives me the heebie jeebies, but not necessarily in a bad way. Still interested in other ways to do it.

like image 44
luqui Avatar answered Oct 02 '22 02:10

luqui