lean4-htt/tests/compiler/foreign/main.lean
Leonardo de Moura 2a1d9a0285 test: simple demo mixing C/C++ and Lean
@dselsam @kha
I did not have to create a new shared library.
The main limitation of this approach is that the new `extern`
functions are only available in compile code. That is, we cannot use
them with `#eval`.
2020-04-06 16:16:49 -07:00

21 lines
969 B
Text

constant SPointed : PointedType := arbitrary _
def S : Type := SPointed.type
instance : Inhabited S := ⟨SPointed.val⟩
@[extern "lean_mk_S"] constant mkS (x y : UInt32) (s : @& String) : S := arbitrary _
@[extern "lean_S_add_x_y"] constant S.addXY (s : @& S) : UInt32 := arbitrary _
@[extern "lean_S_string"] constant S.string (s : @& S) : String := arbitrary _
-- The following 3 externs have side effects. Thus, we put them in IO.
@[extern "lean_S_global_append"] constant appendToGlobalS (str : @& String) : IO Unit := arbitrary _
@[extern "lean_S_global_string"] constant getGlobalString : IO String := arbitrary _
@[extern "lean_S_update_global"] constant updateGlobalS (s : @& S) : IO Unit := arbitrary _
def main : IO Unit := do
IO.println (mkS 10 20 "hello").addXY;
IO.println (mkS 10 20 "hello").string;
appendToGlobalS "foo";
appendToGlobalS "bla";
getGlobalString >>= IO.println;
updateGlobalS (mkS 0 0 "world");
getGlobalString >>= IO.println;
pure ()