Agda record parse error - agda

Agda gives me a parse error when trying to type check this:
record Monad (M : Set → Set) : Set1 where
field return : {A : Set} → A → M A
_>>=_ : {A B : Set} → M A → (A → M B) → M B
What I am doing wrong?
EDIT: I've tried flipping the order of the fields while commenting out the bottom one and it type checks => I'm not using correct record,field syntax i guess
EDIT2:
record Monad (M : Set → Set) : Set1 where
field
bind : {A B : Set} → M A → (A → M B) → M B
return : {A : Set} → A → M A
Okay I fiddled around some more and it started working. Formatted like above. Putting this in an edit since I still don't know what caused my original snippet to not work.

Related

How to prove strict inequality holds for no in Agda

I am currently working on one of the recommended question from the chapter Negation in plfa
Here is the question description:
Using negation, show that strict inequality is irreflexive, that is, n < n holds for no n.
From my understanding, I think in strict inequality, it will take two natural numbers as the argument and output a set right? :
data _<_ : ℕ → ℕ → Set where
z<s : ∀ {n : ℕ}
------------
→ zero < suc n
s<s : ∀ {m n : ℕ}
→ m < n
-------------
→ suc m < suc n
And here is my definition of irreflexive:
<-irreflexive : ∀ {n : ℕ} → n < n → ⊥
<-irreflexive n<n = ?
I know that I need to achieve a "⊥" in the RHS. And since "n<n" is a set, I try to use ¬, but the complier says "n<n" :(n < n) !=< Set. So, what is the type of "n<n" in this case? And am I approaching right in this question? Could anybody give me a hint on that? Thanks in advance !!!
You're on the right track! The trick is now to use the argument n<n for which there can be no real element to produce something of type ⊥. We do this by pattern matching on n<n. Agda can already see that the constructor z<s can't possibly apply (this is morally equivalent of producing something of type ⊥) so all that's left to do is to handle the s<s case:
<-irreflexive : ∀ {n : ℕ} → n < n → ⊥
<-irreflexive (s<s n<n) = ?
However, we can now use recursion to produce the required element of type ⊥
<-irreflexive : ∀ {n : ℕ} → n < n → ⊥
<-irreflexive (s<s n<n) = <-irreflexive n<n
Tada, done!

What would be a simple implementation of a collection with non-repeating elements?

I know that there is an AVL tree in the standard library, but that feels too complex for my purposes. A vanilla function meets most my requirements, except for being able to iterate over the keys.
It is not like I strictly do not know how to make some proposition that for every element in a list there is no duplicate - I did so in Coq once, but I am trying to get a handle on how to bake it into the structure directly and am running into difficulties.
open import Relation.Nullary
open import Relation.Binary.PropositionalEquality
data UniqueList {a} (A : Set a) : Set a
data NotIn {a} {A : Set a} : A → UniqueList A → Set a
data UniqueList A where
[] : UniqueList A
_∷_ : ∀ (x : A) (xs : UniqueList A) → NotIn x xs → UniqueList A
data NotIn {a} {A} where
done : ∀ (x : A) → NotIn x []
dive : ∀ (x y : A) (ys : UniqueList A) (prf : NotIn y ys) → NotIn x ys → ¬ x ≡ y → NotIn x ((y ∷ ys) prf)
I thought I might try something like this, but it feels awkward. Any advice for how to deal with this?

How to import the `∃-syntax`?

open import Data.Product using (_×_; ∃; ∃-syntax)
open import Data.List
Any-∃ : ∀ {A : Set} {P : A → Set} {xs : List A} → ∃[ x ∈ xs ] P x
Could not parse the application ∃[ x ∈ xs ] P x
Operators used in the grammar:
∃[_] (prefix notation, level 20) [∃-syntax (C:\Users\Marko\AppData\Roaming\cabal\x86_64-windows-ghc-8.6.5\Agda-2.6.0\lib\agda-stdlib\src\Data\Product.agda:78,1-9)]
when scope checking ∃[ x ∈ xs ] P x
For some reason it seems like it is not importing the precedence properly from the standard library module. Defining it as...
Any-∃ : ∀ {A : Set} {P : A → Set} {xs : List A} → ∃[ x ] P x
...will make it pass parsing, but I am not sure this is doing the right thing for one of the problems I am trying to solve.
What should I do here?
∃ is precisely for the cases where you can leave out the domain of the function
because it's evident. Otherwise you are supposed to use Σ. And indeed Σ-syntax
does give you the ability to write Σ[ x ∈ A ] B.

Agda: Function Parsing error

I'm new to agda, and following a simple example in the book little MLer. Could anyone help me to figure out why the compiler gives me a parsing error?
Thank you
data Shish (a : Set) : Set where
Bottom : a → Shish a
Onion : Shish a → Shish a
Lamb : Shish a → Shish a
Tomato : Shish a → Shish a
data Rod : Set where
Dagger : Rod
Fork : Rod
Sword : Rod
data Plate : Set where
Gold-plate : Plate
Silver-plate : Plate
Brass-plate : Plate
what_bottom : Shish (a : Set) → Bool
what_bottom (Bottom x) → x
what_bottom (Onion x) → what_bottom x
what_bottom (Lamb x) → what_bottom x
what_bottom (Tomato x) → what_bottom x
/Volumes/Little/mko_io/cat/tmp/mler.agda:54,24-24
/Volumes/Little/mko_io/cat/tmp/mler.agda:54,24: Parse error
:<ERROR>
Set) → Bool
what_bottom (Bott...
The data type definitions are properly defined, but this is not the way one defines functions in Agda. A good starting tutorial is Dependent types at work
Functions are defined with the equal sign.
id : {A : Set} → A → A
id a = a
Moreover, the dependent type must be declared before like so either implicitly or explicitly.
what_bottom : {A : Set} → Shish A → ...
Lastly, that function cannot be defined with a return type Bool. It can have type a though.
As an additional syntactic point, in Agda, underscores are placeholders for mixfix arguments: what_bottom is a mixfix name with one parameter between what and bottom. So you'll end up with a function that you'd use as what (Onion $ Lamb $ Bottom) bottom which is probably not what you intended. Just call it whatBottom or what‿bottom if you're feeling extra.

how to interpret REL in agda

I'm trying to understand some parts of the standard library of Agda, and I can't seem to figure out the definition of REL.
FWIW here's the definition of REL:
-- Binary relations
-- Heterogeneous binary relations
REL : ∀ {a b} → Set a → Set b → (ℓ : Level) → Set (a ⊔ b ⊔ suc ℓ)
REL A B ℓ = A → B → Set ℓ
I can't find any documentation online explaining this, which is why I'm asking here. How does this define a binary relation?
#RodrigoRibeiro's answer explains the Level bits, but once you get rid of universe levels, what does the type Set → Set → Set have to do with binary relations?
Suppose you have a binary relation R ⊆ A × B. The propositional way of modeling it is to create some indexed type R : A → B → Set such that for any a : A, b : B, R a b has inhabitants iff (a, b) ∈ R. So if you want to talk about all relations over A and B, you have to talk about all A- and B- indexed types, i.e. you have to talk about RelationOverAandB = A → B → Set.
If you then want to abstract over the relation's left- and right-hand base type, that means the choice of A and B are not fixed anymore. So you have to talk about REL, such that REL A B = A → B → Set.
What, then, is the type of REL? As we have seen from the REL A B example, it takes the choice of A and B as two arguments; and its result is the type A → B → Set.
So to summarize: given
A : Set
B : Set
we have
REL A B = A → B → Set
which itself has type Set (remember, we're ignoring universe levels here).
And thus,
REL : Set → Set → Set ∎
I guess it's easier to look at an example:
import Level
open import Relation.Binary
open import Data.Nat.Base hiding (_≤_)
data _≤_ : REL ℕ ℕ Level.zero where
z≤n : ∀ {n} -> 0 ≤ n
s≤s : ∀ {n m} -> n ≤ m -> suc n ≤ suc m
The type signature is equivalent to
data _≤_ : ℕ -> ℕ -> Set where
So _≤_ is a relation between two natural numbers. It contains all pairs of numbers such that the first number is less or equal than the second. In the same way we can write
open import Data.List.Base
data _∈_ {α} {A : Set α} : REL A (List A) Level.zero where
here : ∀ {x xs} -> x ∈ x ∷ xs
there : ∀ {x y xs} -> x ∈ xs -> x ∈ y ∷ xs
The type signature is equivalent to
data _∈_ {α} {A : Set α} : A -> List A -> Set where
_∈_ is a relation between elements of type A and lists of elements of type A.
The universe monomorphic variant of REL is itself a binary relation:
MonoREL : REL Set Set (Level.suc Level.zero)
MonoREL A B = A → B → Set
To define a binary relation we need two sets, so REL needs, at least, two types. The type of REL:
REL : ∀ {a b} → Set a → Set b → (ℓ : Level) → Set (a ⊔ b ⊔ suc ℓ)
This type is a bit polluted due to universe level stuff. If you allow yourself a bit of informality, you can disable universe levels (more information about this here) and write REL as:
REL : Set -> Set -> Set
which is a type that expects two types as parameters.
The use of universes in type theory is to avoid paradoxes like Russell's Paradox of set theory.
I'm not a type theory expert, but the interpretation of REL type can be summarised as:
Set a and Set b are parameters types of universe levels a andb, respectively.
The operator ⊔ returns the maximum of two parameter levels.
Hope that this can help you.

Resources