Skip to content

Commit

Permalink
start adding const parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Apr 11, 2022
1 parent 2a9fadc commit 21a40ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion book/src/layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ like `forall<T> { Implemented(T: Eq) => HasImpl(Vec<T>: Eq) }`.
(The distinction between `HasImpl` and `Implemented` is covered below.)

This layer also defines the well-formedness checks for those items.
For example, `struct Foo { f1: T1 }` is well-formed if `T` is well-formed.
For example, `struct Foo { f1: T }` is well-formed if `T` is well-formed.

## The MIR type system layer (`formality-mir`)

Expand Down
32 changes: 32 additions & 0 deletions src/decl/test/const-generics/basic.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#lang racket
(require redex/reduction-semantics
"../../decl-to-clause.rkt"
"../../decl-ok.rkt"
"../../grammar.rkt"
"../../prove.rkt"
"../../../util.rkt")

(module+ test
;; Program:
;;
;; struct Foo;
;; trait Bar<const N: usize> {}
;; impl<const M: usize> Bar<M> for Foo {}
(redex-let*
formality-decl
((KindedVarId_M (term ((CtKind (scalar-ty usize)) M)))
(TraitDecl (term (Bar (trait ((TyKind Self) ((CtKind (scalar-ty usize)) N)) () ()))))
(AdtDecl_Foo (term (Foo (struct () () ((Foo ()))))))
(CtKind_M (term M))
(Ct_M (term ((scalar-ty usize) CtKind_M)))
(TraitRef_Bar (term (Bar ((TyRigid Foo ()) Ct_M))))
(TraitImplDecl (term (impl (KindedVarId_M) TraitRef_Bar () ())))
(CrateDecl (term (TheCrate (crate (TraitDecl AdtDecl_Foo TraitImplDecl)))))
(Env (term (env-for-crate-decl CrateDecl)))

;; `TheCrate` can prove `forall<const M: usize> Bar<M>: Foo`
(Goal (term (ForAll (KindedVarId_M) (Implemented TraitRef_Bar))))
)
(traced '()
(decl:test-can-prove Env Goal)))
)
15 changes: 12 additions & 3 deletions src/ty/grammar.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
;; lifetime, etc)
;;
;; Overridden from formality-logic.
(ParameterKind ::= TyKind LtKind)
(ParameterKind ::= TyKind LtKind (CtKind Ty))

This comment has been minimized.

Copy link
@nikomatsakis

nikomatsakis Apr 11, 2022

I do wonder if we want CtKind Ty-- this is an interesting question. I somewhat think this should be encoded as a predicate (ConstHasTy Const Ty or something like that). Some parts of the code assume that ParameterKind is an atom, and I think that's a pretty valid assumption.

This comment has been minimized.

Copy link
@lcnr

lcnr Apr 12, 2022

Author Owner

this is an interesting question 😆

We have two places where the type of const parameters is relevant:
When defining them and for generic arguments.

Afaict you are thinking of:

  • use a predicate for the definition of generic parameters
  • keep the ty as part of generic arguments

The split between "definition" vs "generic argument" distinction isn't as clear in rustc, but it feels closer to the ty being part of definitions and generic arguments, i.e. the current status of my impl.

Do you expect there to be a semantic difference between these approaches?


;; Parameter: value for a generic parameter
;;
;; Overridden from formality-logic.
(Parameter ::= Ty Lt)
(Parameter ::= Ty Lt Ct)

;; ANCHOR:Predicates
;; `Predicate` -- the atomic items that we can prove
Expand Down Expand Up @@ -99,6 +99,15 @@
;; Treat ABIs as opaque strings (for now, at least)
(Abi ::= string)

;; Ct -- Rust constants in the type system.
;;
;; TODO: Also add non-leaf things here.
(Ct ::= (Ty CtKind))
(CtKind ::=
(Leaf number) ; A value tree leaf.
VarId ; Bound, existential (inference), or universal (placeholder) variable.
)

;; Lt -- Rust lifetimes
;;
;; Very similar to types `Ty` in terms of how they are represented
Expand All @@ -124,7 +133,7 @@
(VarInequality ::= (Parameters_lb <= VarId <= Parameters_ub))

;; Scalars -- numbers, booleans
(ScalarId ::= i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 bool)
(ScalarId ::= i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 isize usize bool)

;; Identifiers -- these are all equivalent, but we give them fresh names to help
;; clarify their purpose
Expand Down

0 comments on commit 21a40ac

Please sign in to comment.