Lean make a library
Posted on August 2, 2019
Tags: lean
1 Setup
elan is used to install/update lean
elan show #shows current lean versions
elan self update
elan default leanprover/lean4:stable #sets lean 4 as default
elan override set leanprover/lean4:stable #set lean 4 for this dir only
2 About lake
lake help init
# Create a Lean package in the current directory
# USAGE:
# lake init <name> [<template>]
# The initial configuration and starter files are based on the template:
# std library and executable; default
# exe executable only
# lib library only
# math library only with a mathlib dependency
Go to https://github.com/userJY/demolibLeanLocal to see how to make a lean library and use it.
3 Example of a Customlib WITHIN the dir of the executable
import Customlib.X
import Customlib.Y
import Lake
Lake DSL
open
Customlib {
package := PackageFacet.oleans
defaultFacet }
To use the mycustomlib, with Consumer.lean, we just throw the
import Lake
System Lake DSL
open
package consumer {
:= #[{ name := `customlib, src := Source.path (FilePath.mk "customlib") }]
dependencies }
4 Installing git dependencies like mathlib
import Lake
Lake DSL
open
require mathlib from git "https://github.com/leanprover-community/mathlib4"
package helloWorld {-- add configuration options here
}
: FilePath) (depTargets : List (BuildTarget i))
def fileTargetWithDepList (file : List i → BuildM PUnit) (extraDepTrace : BuildM _ := pure BuildTrace.nil) : FileTarget :=
(build fileTargetWithDep file (Target.collectList depTargets) build extraDepTrace
5 FFI
import Lake
System Lake DSL
open
--fileTargetWithDep : {i : Type} → FilePath → BuildTarget i → (i → BuildM PUnit) → optParam (BuildM BuildTrace) (pure BuildTrace.nil) → FileTarget
/-
: Type} is FilePath:Type
{i FilePath is `("./build/myfuns.o")`
BuildTarget FilePath is `("./myfuns.cpp")`
FilePath → BuildM PUnit) is `(λ srcFile => do compileO ...`
(-/
/-
: FilePath → FilePath → optParam (Array String) #[] → optParam FilePath { toString := "cc" } → BuildM PUnit
compileO FilePath is `("./build/myfuns.o")`
-/
: FileTarget :=
def ffIOTarget "./build/myfuns.o") ("./myfuns.cpp") (λ srcFile => do
fileTargetWithDep ("./build/myfuns.o") srcFile #["-I", (← getLeanIncludeDir).toString, "-fPIC"] "c++")
compileO (--`(← getLeanIncludeDir).toString` is "/home/USERNAME/.elan/toolchains/leanprover--lean4---nightly-2022-07-29/include"
--staticLibTarget : FilePath → Array FileTarget → optParam (Option FilePath) none → FileTarget
/-
FilePath is `"./build/leanffi.a"`
Array FileTarget is `#[ffIOTarget]`
-/
:= ffIOTarget
extern_lib cLib /-staticLibTarget ("./build" / nameToStaticLib "leanffi") #[ffIOTarget]
-- `nameToStaticLib "leanffi"` is "leanffi.a"
-/
--Below is not relevant for FFI
--script doesnt run on lakebuild. Use script to investigate variables.
--script can be ran with `lake script run logVar`
do
script logVar IO.println __dir__
IO.println defaultBuildDir
IO.println (← getLeanIncludeDir).toString
IO.println (nameToStaticLib "leanffi")
return 0
package t1 {--srcDir is dir where the entrypoint Main.lean is located
:= "." --entry points to ./srcDir/./Main.lean
srcDir
}