forked from tydeu/lean4-alloy
-
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.
A quick-and-dirty POC of writing C++ with alloy
- Loading branch information
Showing
7 changed files
with
214 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import Alloy.C | ||
open scoped Alloy.C | ||
|
||
/-! | ||
# `Cpp.lean` | ||
An adaption of Lean 4's ['foreign'][1] example for Alloy. | ||
[1]: https://github.com/leanprover/lean4/tree/b278a20ac22adcbfde11db386f2dc874d4a215ad/tests/compiler/foreign | ||
-/ | ||
|
||
alloy c include <stdint.h> <stdlib.h> <string.h> <lean/lean.h> <string> | ||
|
||
-------------------------------------------------------------------------------- | ||
/-! ## Definition of S -/ | ||
-------------------------------------------------------------------------------- | ||
|
||
alloy c section | ||
|
||
typedef struct { | ||
uint32_t m_x; | ||
uint32_t m_y; | ||
lean_object * m_s; | ||
} S; | ||
|
||
static void S_finalize(void* ptr) { | ||
lean_dec(((S*)ptr)->m_s); | ||
delete (S*)ptr; | ||
} | ||
|
||
static void S_foreach(void* ptr, b_lean_obj_arg f) { | ||
lean_apply_1(f, ((S*)ptr)->m_s); | ||
} | ||
|
||
static S g_s = {0, 0, NULL}; | ||
|
||
end | ||
|
||
alloy c extern_type S => S := { | ||
foreach := `S_foreach | ||
finalize := `S_finalize | ||
} | ||
|
||
-------------------------------------------------------------------------------- | ||
/-! ## Lean Interface -/ | ||
-------------------------------------------------------------------------------- | ||
|
||
alloy cpp extern "lean_mk_S" | ||
def mkS (x y : UInt32) (string : String) : S := { | ||
S* s = new S; | ||
s->m_x = x; | ||
s->m_y = y; | ||
s->m_s = string; | ||
return to_lean<S>(s); | ||
} | ||
|
||
-- alloy c extern "lean_S_add_x_y" | ||
-- def S.addXY (s : @& S) : UInt32 := { | ||
-- return of_lean<S>(s)->m_x + of_lean<S>(s)->m_y; | ||
-- } | ||
|
||
alloy cpp extern "lean_S_string" | ||
def S.string (s : @& S) : String := { | ||
lean_inc(of_lean<S>(s)->m_s); | ||
return of_lean<S>(s)->m_s; | ||
} | ||
|
||
-- alloy c extern "lean_S_global_append" | ||
-- def appendToGlobalS (string : String) : BaseIO PUnit := { | ||
-- if (g_s.m_s == NULL) { | ||
-- g_s.m_s = string; | ||
-- } else { | ||
-- g_s.m_s = lean_string_append(g_s.m_s, string); | ||
-- } | ||
-- return lean_io_result_mk_ok(lean_box(0)); | ||
-- } | ||
|
||
-- alloy c extern "lean_S_global_string" | ||
-- def getGlobalString : BaseIO String := { | ||
-- if (g_s.m_s == NULL) { | ||
-- g_s.m_s = lean_mk_string(""); | ||
-- } | ||
-- lean_inc(g_s.m_s); | ||
-- return lean_io_result_mk_ok(g_s.m_s); | ||
-- } | ||
|
||
-- alloy c extern "lean_S_update_global" | ||
-- def updateGlobalS (s : @& S) : BaseIO Unit := { | ||
-- if (g_s.m_s != NULL) { | ||
-- lean_dec(g_s.m_s); | ||
-- } | ||
-- lean_inc(of_lean<S>(s)->m_s); | ||
-- g_s.m_x = of_lean<S>(s)->m_x; | ||
-- g_s.m_y = of_lean<S>(s)->m_y; | ||
-- g_s.m_s = of_lean<S>(s)->m_s; | ||
-- return lean_io_result_mk_ok(lean_box(0)); | ||
-- } |
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import S | ||
import Cpp | ||
|
||
#eval show IO _ from do | ||
getGlobalString >>= IO.println | ||
-- #eval show IO _ from do | ||
-- getGlobalString >>= IO.println | ||
|
||
#eval show IO _ from do | ||
IO.println (mkS 10 20 "hello").addXY | ||
IO.println (mkS 10 20 "hello").string | ||
updateGlobalS (mkS 0 0 "") | ||
appendToGlobalS "foo" | ||
appendToGlobalS "bla" | ||
getGlobalString >>= IO.println | ||
updateGlobalS (mkS 0 0 "world") | ||
getGlobalString >>= IO.println | ||
-- IO.println (mkS 10 20 "hello").string | ||
-- updateGlobalS (mkS 0 0 "") | ||
-- appendToGlobalS "foo" | ||
-- appendToGlobalS "bla" | ||
-- getGlobalString >>= IO.println | ||
-- updateGlobalS (mkS 0 0 "world") | ||
-- getGlobalString >>= IO.println | ||
|
||
#eval show IO _ from do | ||
getGlobalString >>= IO.println | ||
-- #eval show IO _ from do | ||
-- getGlobalString >>= IO.println |
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 |
---|---|---|
@@ -1,21 +1,28 @@ | ||
import Lake | ||
open Lake DSL | ||
|
||
package s { | ||
package Cpp where | ||
buildType := .debug | ||
} | ||
precompileModules := true | ||
moreLinkArgs := #[s!"-L{__dir__}/build/lib", | ||
"-lstdc++"] -- "-v", --, "-lc++", "-lc++abi", "-lunwind"] -- "-lstdc++"] | ||
weakLeanArgs := #[ | ||
-- s!"--load-dynlib={__dir__}/build/lib/" ++ nameToSharedLib "xxx", | ||
] | ||
|
||
require alloy from ".."/".." | ||
|
||
module_data alloy.c.o : BuildJob FilePath | ||
lean_lib S { | ||
module_data alloy.cpp.o : BuildJob FilePath | ||
lean_lib Cpp { | ||
precompileModules := true | ||
nativeFacets := #[Module.oFacet, `alloy.c.o] | ||
nativeFacets := #[Module.oFacet, `alloy.cpp.o] | ||
} | ||
|
||
@[default_target] | ||
lean_exe s { | ||
root := `Main | ||
} | ||
-- @[default_target] | ||
-- lean_exe alloycpp { | ||
-- root := `Main | ||
-- } | ||
|
||
-- For now, test with `lake -R build Test -v` | ||
|
||
lean_lib Test |
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