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

Add Data.Maybe #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Data/Maybe.ard
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
\import Logic
\import Paths
\import Set

-- | Maybe A
-- Maybe represents an optional value. It might also be used to represent failure.
\data Maybe (A : \Type) | nothing | just A
\where {
\func bind {A B : \Type} (a : Maybe A)(f : A -> Maybe B) : Maybe B \elim a
| nothing => nothing
| just a => f a

\lemma justA/=nothing {A : \Type} (a : A) (p : just a = nothing) : Empty =>
IsJust.absurd (transport IsJust p itIsJust)
}

-- | IsJust is a proof that the value is actually a just
\data IsJust {A : \Type} (m : Maybe A) : \Prop \with
| just _ => itIsJust
\where {
\func absurd {A B : \Type} (s : IsJust {A} nothing) : B

\lemma notNothing {A : \Type} (m : Maybe A) (ev : IsJust m) : Not (m = nothing) \elim m, ev
| just a, itIsJust => Maybe.justA/=nothing a
}

\func isJust {A : \Type} (m : Maybe A) : \Type
| nothing => Empty
| just a => IsJust (just a)

\func isItJust {A : \Type} (m : Maybe A) : Dec (IsJust m) \elim m
| nothing => no (IsJust.absurd {A} {Empty})
| just a => yes itIsJust