Datatypes, typed #
A declared datatype is used at a Lean index. Give a Lean type a ToTyp instance naming the
datatype, declare the datatype in the scope (Srt.datatype, Solver.declareDatatype), and
Term Lst is a term like any other:
structure Lst where private mk ::
instance : ToTyp Lst := ⟨.datatype "Lst"⟩
-- in `Env`, once `Lst := nil | cons (head : Int) (tail : Lst)` is declared
let cons ← Datatype.ctor Lst "cons" -- `Ctor Lst`
let nil ← Datatype.ctor Lst "nil"
let head ← cons.field Int "head" -- `Field Lst Int`
let l ← cons.apply2 (← Term.mkInt 1) (← nil.apply)
let h ← head.get l -- `Term Int`
Which constructors a datatype has, and the sorts of their fields, are facts about its declaration:
Datatype.ctor and Ctor.field look a name up and check the sorts once, and the Ctor α or
Field α β they answer carries the index from there on.
The constructor arguments of one application, each carrying its own index.
Equations
- Cvc.Typed.Datatype.Args = List ((β : Type) × Cvc.ToTyp β × Cvc.Typed.Term β)
Instances For
No arguments.
Equations
Instances For
The sort-erased terms, in field order.
Equations
- One or more equations did not get rendered due to their size.
Instances For
The sort-erased terms, in field order.
Instances For
A datatype constructor, at the index of the datatype it builds.
- ctor : Cvc.Datatype.Ctor
- term : Cvc.Untyped.Term
- tester : Cvc.Untyped.Term
Instances For
One field of a datatype constructor, at the datatype's index and the field's.
The typed counterpart of the selector Cvc.Datatype.Selector, which reflects a field sort-erased.
- sel : Cvc.Datatype.Selector
- term : Cvc.Untyped.Term
- updater : Cvc.Untyped.Term
Instances For
The sort-erased constructor it reflects, for free.
Equations
Instances For
The sort-erased constructor it reflects, for free.
Instances For
Types a reflected constructor at the datatype α, checking that α is what it builds.
The reflection counterpart of Datatype.ctor, which looks a constructor up by name. A constructor
of a parametric datatype read off the uninstantiated declaration builds the parametric sort, not an
instance of it, and is refused. toTyped infers α from the expected type; typeCheckAs and
typeCheck are the same two functions under the names that say what they do.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Types a reflected constructor at the datatype α, checking that α is what it builds.
The reflection counterpart of Datatype.ctor, which looks a constructor up by name. A constructor
of a parametric datatype read off the uninstantiated declaration builds the parametric sort, not an
instance of it, and is refused. toTyped infers α from the expected type; typeCheckAs and
typeCheck are the same two functions under the names that say what they do.
Equations
Instances For
Types a reflected constructor at the datatype α, checking that α is what it builds.
The reflection counterpart of Datatype.ctor, which looks a constructor up by name. A constructor
of a parametric datatype read off the uninstantiated declaration builds the parametric sort, not an
instance of it, and is refused. toTyped infers α from the expected type; typeCheckAs and
typeCheck are the same two functions under the names that say what they do.
Instances For
Types a reflected constructor at the datatype α, checking that α is what it builds.
The reflection counterpart of Datatype.ctor, which looks a constructor up by name. A constructor
of a parametric datatype read off the uninstantiated declaration builds the parametric sort, not an
instance of it, and is refused. toTyped infers α from the expected type; typeCheckAs and
typeCheck are the same two functions under the names that say what they do.
Instances For
How many fields the constructor takes.
Equations
Instances For
Applies the constructor, failing unless its arguments' number and sorts are its fields'.
Equations
- c.apply1 fst = c.apply (Cvc.Typed.Datatype.Args.cons fst Cvc.Typed.Datatype.Args.nil)
Instances For
Applies the constructor, failing unless its arguments' number and sorts are its fields'.
Equations
- c.apply2 fst snd = c.apply (Cvc.Typed.Datatype.Args.cons fst (Cvc.Typed.Datatype.Args.cons snd Cvc.Typed.Datatype.Args.nil))
Instances For
Applies the constructor, failing unless its arguments' number and sorts are its fields'.
Equations
- c.apply3 fst snd thd = c.apply (Cvc.Typed.Datatype.Args.cons fst (Cvc.Typed.Datatype.Args.cons snd (Cvc.Typed.Datatype.Args.cons thd Cvc.Typed.Datatype.Args.nil)))
Instances For
Whether a value was built by this constructor.
Equations
- c.is t = (Cvc.Typed.Datatype.Ctor.tester✝ c).applyTester t.erase
Instances For
Matching #
A match picks a body by the constructor its scrutinee was built with: Term.mkMatch takes the
scrutinee and an array of Case α β, whose bodies all have the index β. Ctor.case binds the
constructor's fields to bound variables, pushed in field order as for Solver.defineFun, and checks
their sorts. Every constructor needs a case, unless a Datatype.caseAny catches the rest; cvc5
checks that.
The sort-erased case term it wraps, for free, as the sort-erased Term.mkMatch takes one.
There is no way back: a sort-erased case is an ordinary Term, with no type of its own to hang a
conversion on.
Equations
Instances For
The sort-erased case term it wraps, for free, as the sort-erased Term.mkMatch takes one.
There is no way back: a sort-erased case is an ordinary Term, with no type of its own to hang a
conversion on.
Instances For
Fails unless the constructor takes exactly count fields.
Equations
- c.checkArity count = (Cvc.Typed.Datatype.Ctor.ctor✝ c).checkArity count
Instances For
A fresh sort-erased bound variable at the sort of the constructor's field at idx.
Untyped.BVar.typeCheck puts an index on it.
Equations
- c.mkBVarAt idx name = (Cvc.Typed.Datatype.Ctor.ctor✝ c).mkBVarAt idx name
Instances For
The case taken when the scrutinee was built by this constructor.
args binds the constructor's fields, in field order, and body may mention them. Fails unless
their number and sorts are the fields'.
Equations
- c.case args body = c.caseErased args.erase body
Instances For
The case taken when the scrutinee was built by this constructor.
args binds the constructor's fields, in field order, and body may mention them. Fails unless
their number and sorts are the fields'.
Instances For
The case taken when the scrutinee was built by this constructor.
args binds the constructor's fields, in field order, and body may mention them. Fails unless
their number and sorts are the fields'.
Equations
- c.case2 fst snd body = c.case (Cvc.Typed.BVars.push snd (Cvc.Typed.BVars.push fst [])) body
Instances For
The case taken when the scrutinee was built by this constructor.
args binds the constructor's fields, in field order, and body may mention them. Fails unless
their number and sorts are the fields'.
Equations
- c.case3 fst snd thd body = c.case (Cvc.Typed.BVars.push thd (Cvc.Typed.BVars.push snd (Cvc.Typed.BVars.push fst []))) body
Instances For
The case taken when no earlier one was.
v stands for the scrutinee itself, so body may mention it. A match needs one of these unless its
cases already cover every constructor.
Equations
- Cvc.Typed.Datatype.caseAny v body = do let __do_lift ← Cvc.Untyped.Term.matchBindCase (Cvc.Typed.BVars.push v []).erase v.toTerm.erase body.erase pure { term := __do_lift }
Instances For
Datatype.ctor at the datatype of a term's index: the constructor of the given name.
Equations
- _scrutinee.ctorOf name = Cvc.Typed.Datatype.ctor α name
Instances For
The body of whichever case matches the scrutinee.
Fails unless every constructor of α has a case, or one of them is a Datatype.caseAny.
Equations
Instances For
The sort-erased selector it was built from, for free.
Equations
- sel.toUntyped = Cvc.Typed.Datatype.Field.sel✝ sel
Instances For
The sort-erased selector it was built from, for free.
Instances For
Reads the field out of a value.
Undefined where the value was not built by the selector's own constructor, which is what
Ctor.is is for.
Equations
- sel.get t = (Cvc.Typed.Datatype.Field.term✝ sel).applySelector t.erase
Instances For
The value with this field replaced.
Equations
- sel.update t v = (Cvc.Typed.Datatype.Field.updater✝ sel).applyUpdater t.erase v.erase
Instances For
Types a reflected selector at the datatype α and the field β, checking both sorts.
The reflection counterpart of Datatype.Ctor.field, which looks a selector up by name. toTyped
infers α and β from the expected type; typeCheckAs and typeCheck are the same two functions
under the names that say what they do.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Types a reflected selector at the datatype α and the field β, checking both sorts.
The reflection counterpart of Datatype.Ctor.field, which looks a selector up by name. toTyped
infers α and β from the expected type; typeCheckAs and typeCheck are the same two functions
under the names that say what they do.
Equations
- sel.toTyped = Cvc.Datatype.Selector.toTypedAs α β sel
Instances For
Types a reflected selector at the datatype α and the field β, checking both sorts.
The reflection counterpart of Datatype.Ctor.field, which looks a selector up by name. toTyped
infers α and β from the expected type; typeCheckAs and typeCheck are the same two functions
under the names that say what they do.
Instances For
Types a reflected selector at the datatype α and the field β, checking both sorts.
The reflection counterpart of Datatype.Ctor.field, which looks a selector up by name. toTyped
infers α and β from the expected type; typeCheckAs and typeCheck are the same two functions
under the names that say what they do.
Instances For
Viewing a datatype operation #
Generic over every datatype: constructors and fields come back by name, and the kids' sorts as a
list of Typs that matching refines. Records, tuples and nullables are datatypes to cvc5, so this
covers them too; Term.recordFields?, Term.pairComponents? and Term.nullableCases? take them
apart at a known index instead.
A datatype operation of sort τ, taken apart one level.
- construct
[Ω]
{τ : Typ}
(ctor : String)
(doms : List Typ)
(args : Fields doms)
: View.Datatype τ
A constructor applied to its arguments, none for a constant constructor.
- selectField
[Ω]
{τ : Typ}
(field : String)
(σ : Typ)
(arg : Term σ.Denote)
: View.Datatype τ
A field read off
arg, a term of the datatype sortσ. - updateField
[Ω]
{τ : Typ}
(field : String)
(σ : Typ)
(arg : Term τ.Denote)
(value : Term σ.Denote)
: View.Datatype τ
argwith its field replaced byvalue, a term of sortσ. - testCtor
[Ω]
(ctor : String)
(σ : Typ)
(arg : Term σ.Denote)
: View.Datatype Typ.bool
Whether
arg, of the datatype sortσ, was built by the constructorctor.
Instances For
Takes a datatype operation of sort τ apart one level, none for any other term.
Also none when a kid's sort is one no Typ describes.
Equations
- One or more equations did not get rendered due to their size.
Instances For
smt! match #
smt! match l with
| cons h t => h * 2
| nil => 0
A bare identifier in a pattern is a constructor; _ alone is the catch-all. A field's index is
inferred from its uses in the body; a nested match's scrutinee needs an ascription,
| cons h (t : Lst) => match t with ….
Expands one alternative of a typed match into a term building that case.
Equations
- One or more equations did not get rendered due to their size.