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

Inconvenient definition for NDBType.asType #154

Open
michens opened this issue Mar 19, 2024 · 0 comments
Open

Inconvenient definition for NDBType.asType #154

michens opened this issue Mar 19, 2024 · 0 comments

Comments

@michens
Copy link

michens commented Mar 19, 2024

The following is given in the Typed Queries exercise

Nullable Types
Add support for nullable columns to the query language by representing database types with the following structure:

 structure NDBType where
   underlying : DBType
   nullable : Bool
 
 abbrev NDBType.asType (t : NDBType) : Type :=
   if t.nullable then
     Option t.underlying.asType
   else
     t.underlying.asType

Use this type in place of DBType in Column and DBExpr, and look up SQL's rules for NULL and comparison operators to determine the types of DBExpr's constructors.

It seems like Lean has issues seeing through this definition of .asType, which led to needing to use convoluted casting and tactics for the rest of the implementation.

For example, you can try to write NDBType.beq as:

def NDBType.beq (t : NDBType) (x y : t.asType) : Bool :=
  match t with
  | ⟨_, true⟩ => x == y
  | ⟨_, false⟩ => x == y

However, Lean gives the errors:

failed to synthesize instance
  BEq (asType { underlying := underlying✝, nullable := true })

failed to synthesize instance
  BEq (asType { underlying := underlying✝, nullable := false })

Instead, I needed to write it as:

def NDBType.beq (t : NDBType) (x y : t.asType) : Bool :=
  match t with
  | ⟨t', true⟩ =>
    let x' : Option t'.asType := x
    x' == y
  | ⟨t', false⟩ =>
    let x' : t'.asType := x
    x' == y

On the other hand, you can define NDBType.asType with:

abbrev NDBType.asType (t : NDBType) : Type :=
  match t with
  | ⟨t', true⟩ => Option t'.asType
  | ⟨t', false⟩ => t'.asType

At this point, the first declaration of NDBType.beq works, as do the natural modifications needed for the rest of the exercise.

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