forked from leanprover/lean4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'leanprover:master' into master
- Loading branch information
Showing
537 changed files
with
195,686 additions
and
90,435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import Lean | ||
|
||
open Lean Meta | ||
|
||
def ctor (mvarId : MVarId) (idx : Nat) : MetaM (List MVarId) := do | ||
/- Set `MetaM` context using `mvarId` -/ | ||
withMVarContext mvarId do | ||
/- Fail if the metavariable is already assigned. -/ | ||
checkNotAssigned mvarId `ctor | ||
/- Retrieve the target type, instantiateMVars, and use `whnf`. -/ | ||
let target ← getMVarType' mvarId | ||
let .const declName us := target.getAppFn | ||
| throwTacticEx `ctor mvarId "target is not an inductive datatype" | ||
let .inductInfo { ctors, .. } ← getConstInfo declName | ||
| throwTacticEx `ctor mvarId "target is not an inductive datatype" | ||
if idx = 0 then | ||
throwTacticEx `ctor mvarId "invalid index, it must be > 0" | ||
else if h : idx - 1 < ctors.length then | ||
apply mvarId (.const ctors[idx - 1] us) | ||
else | ||
throwTacticEx `ctor mvarId "invalid index, inductive datatype has only {ctors.length} contructors" | ||
|
||
open Elab Tactic | ||
|
||
elab "ctor" idx:num : tactic => | ||
liftMetaTactic (ctor · idx.getNat) | ||
|
||
example (p : Prop) : p := by | ||
ctor 1 -- Error | ||
|
||
example (h : q) : p ∨ q := by | ||
ctor 0 -- Error | ||
exact h | ||
|
||
example (h : q) : p ∨ q := by | ||
ctor 3 -- Error | ||
exact h | ||
|
||
example (h : q) : p ∨ q := by | ||
ctor 2 | ||
exact h | ||
|
||
example (h : q) : p ∨ q := by | ||
ctor 1 | ||
exact h -- Error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import Lean | ||
|
||
open Lean Meta | ||
|
||
def ex1 (declName : Name) : MetaM Unit := do | ||
let info ← getConstInfo declName | ||
IO.println s!"{declName} : {← ppExpr info.type}" | ||
if let some val := info.value? then | ||
IO.println s!"{declName} : {← ppExpr val}" | ||
|
||
#eval ex1 ``Nat | ||
|
||
def ex2 (declName : Name) : MetaM Unit := do | ||
let info ← getConstInfo declName | ||
trace[Meta.debug] "{declName} : {info.type}" | ||
if let some val := info.value? then | ||
trace[Meta.debug] "{declName} : {val}" | ||
|
||
#eval ex2 ``Add.add | ||
|
||
set_option trace.Meta.debug true in | ||
#eval ex2 ``Add.add | ||
|
||
def ex3 (declName : Name) : MetaM Unit := do | ||
let info ← getConstInfo declName | ||
forallTelescope info.type fun xs type => do | ||
trace[Meta.debug] "hypotheses : {xs}" | ||
trace[Meta.debug] "resultType : {type}" | ||
for x in xs do | ||
trace[Meta.debug] "{x} : {← inferType x}" | ||
|
||
def myMin [LT α] [DecidableRel (α := α) (·<·)] (a b : α) : α := | ||
if a < b then | ||
a | ||
else | ||
b | ||
|
||
set_option trace.Meta.debug true in | ||
#eval ex3 ``myMin | ||
|
||
def ex4 : MetaM Unit := do | ||
let nat := mkConst ``Nat | ||
withLocalDeclD `a nat fun a => | ||
withLocalDeclD `b nat fun b => do | ||
let e ← mkAppM ``HAdd.hAdd #[a, b] | ||
trace[Meta.debug] "{e} : {← inferType e}" | ||
let e ← mkAdd a (mkNatLit 5) | ||
trace[Meta.debug] "added 5: {e}" | ||
let e ← whnf e | ||
trace[Meta.debug] "whnf: {e}" | ||
let e ← reduce e | ||
trace[Meta.debug] "reduced: {e}" | ||
let a_plus_1 ← mkAdd a (mkNatLit 1) | ||
let succ_a := mkApp (mkConst ``Nat.succ) a | ||
trace[Meta.debug] "({a_plus_1} =?= {succ_a}) == {← isDefEq a_plus_1 succ_a}" | ||
let m ← mkFreshExprMVar nat | ||
let m_plus_1 ← mkAdd m (mkNatLit 1) | ||
trace[Meta.debug] "m_plus_1: {m_plus_1}" | ||
unless (← isDefEq m_plus_1 succ_a) do throwError "isDefEq failed" | ||
trace[Meta.debug] "m_plus_1: {m_plus_1}" | ||
|
||
set_option trace.Meta.debug true in | ||
#eval ex4 | ||
|
||
open Elab Term | ||
|
||
def ex5 : TermElabM Unit := do | ||
let nat := Lean.mkConst ``Nat | ||
withLocalDeclD `a nat fun a => do | ||
withLocalDeclD `b nat fun b => do | ||
let ab ← mkAppM ``HAdd.hAdd #[a, b] | ||
let stx ← `(fun x => if x < 10 then $(← exprToSyntax ab) + x else x + $(← exprToSyntax a)) | ||
let e ← elabTerm stx none | ||
trace[Meta.debug] "{e} : {← inferType e}" | ||
let e := mkApp e (mkNatLit 5) | ||
let e ← whnf e | ||
trace[Meta.debug] "{e}" | ||
|
||
set_option trace.Meta.debug true in | ||
#eval ex5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Lean | ||
|
||
def f (x y : Nat) := x * y + 1 | ||
|
||
infixl:65 " *' " => f | ||
|
||
#check 2 *' 3 | ||
|
||
notation "unitTest " x => Prod.mk x () | ||
|
||
#check unitTest 42 | ||
|
||
notation "parenthesisTest " x => Nat.sub (x) | ||
#check parenthesisTest 12 | ||
|
||
def Set (α : Type u) := α → Prop | ||
def setOf {α : Type} (p : α → Prop) : Set α := p | ||
notation "{ " x " | " p " }" => setOf (fun x => p) | ||
|
||
#check { x | x ≤ 1 } | ||
|
||
notation "cdotTest " "(" x ", " y ")" => Prod.map (· + 1) (1 + ·) (x, y) | ||
|
||
#check cdotTest (13, 12) | ||
|
||
notation "tupleFunctionTest " "(" x ", " y ")"=> Prod.map (Nat.add 1) (Nat.add 2) (x, y) | ||
|
||
#check tupleFunctionTest (15, 12) | ||
|
||
notation "diag " x => Prod.mk x x | ||
|
||
#check diag 12 | ||
|
||
open Lean Meta PrettyPrinter Delaborator SubExpr in | ||
@[delab app.Prod.mk] def delabDoubleRhsTest : Delab := do | ||
let e ← getExpr | ||
let #[_, _, x, y] := e.getAppArgs | failure | ||
guard (← isDefEq x y) | ||
let stx ← withAppArg delab | ||
`(diag $stx) | ||
|
||
#check diag 3 | ||
#check (3, 3) | ||
#check (3, 4) | ||
#check (2+1, 3) | ||
#check (true, true) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.