leanprover / leanprover/fp-lean

Inconvenient definition for NDBType.asType

Open
#154 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Lean
Stars
192
Forks
73
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Typed Queries exercise and compare the conditional NDBType.asType definition with the pattern-matching alternative shown in the issue. Check the related NDBType.beq implementation and the rest of the nullable-types exercise. Done means the definition supports direct pattern-matched comparisons without the reported casts or tactic workarounds.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.