Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .mnesiastore/LATEST.LOG
Binary file not shown.
Binary file modified .mnesiastore/schema.DAT
Binary file not shown.
251 changes: 189 additions & 62 deletions lib/AL.ex

Large diffs are not rendered by default.

132 changes: 10 additions & 122 deletions lib/AL/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ defmodule AL.Application do
"""

use Application
use AL

@impl true
def start(_type, _args) do
AL.Command.setup()
AL.Objects.setup()
AL.Object.setup()

opts = [strategy: :one_for_one, name: Al.Supervisor]
{:ok, pid} = Supervisor.start_link([AL.Scheduler], opts)
Expand All @@ -22,126 +21,15 @@ defmodule AL.Application do

def bootstrap() do
case :mnesia.table_info(:command, :size) do
0 -> do_bootstrap()
_ -> :ok
end
end

defp do_bootstrap() do
run do
set_class(:class, :class)
set_class(:object, :class)
set_class(:behaviour, :class)

set_super(:class, :object)
set_super(:behaviour, :object)

set_method(:object, :lookup, :lookup)
set_method(:object, :send, :send)
set_method(:object, :meta, :metaclass)
set_method(:object, :defmethod, :defmethod)

set_class(:metaclass, :behaviour)

set_oapply(:metaclass, [self, class, meta]) do
class(self, class)
class(class, meta)
end

set_class(:lookup, :behaviour)
set_oapply(
:lookup,
[self, name, id]
) do
alternative([method(self, name, id)],
[super(self, super),
lookup(super, name, id)])
end

set_class(:send, :behaviour)

set_oapply(
:send,
[self, method, args]
) do
class(self, class)
implies(
[lookup(class, method, id)],
[
print(["calling", id, "from", class, "with args", [self | args]]),
oapply(id, [self | args])
],
[:fail]
)
end

set_class(:defmethod, :behaviour)
set_oapply(:defmethod, [self, method_name, head, body]) do
fresh_id(impl)
set_method(self, method_name, impl)
set_class(impl, :behaviour)
set_oapply(impl, head, body)
end

set_class(:map_get, :behaviour)
set_method(:map, :map_get, :map_get)

defmethod(:class, :construct, [self, %{class: self}]) do
end

set_method(:class, :allocate, :allocate_class)
set_class(:allocate_class, :behaviour)

set_oapply(
:allocate_class,
[self, args, name]
) do
map_get(args, :name, name)
map_get(args, :super, super)
map_get(args, :slots, slots)

class(self, meta)

set_class(name, meta)
set_super(name, super)
set_slots(name, slots)
end

defmethod(:object, :allocate, [self, _, self]) do
print(["allocate", self])
end

defmethod(:object, :init, [self, _, self]) do
print(["initialise", self])
end

defmethod(:class, :new, [self, args, new]) do
send(self, :construct, [construct])
send(construct, :allocate, [args, alloc])
send(alloc, :init, [args, new])
end

defmethod(:object, :examine, [self, %{classes: classes,
objects: objects,
supers: supers,
subs: subs,
methods: methods,
clauses: clauses}]) do
findall(c, [class(self, c)], classes)
findall(c, [class(c, self)], objects)
findall(s, [super(self, s)], supers)
findall(sub, [super(sub, self)], subs)
findall([n, id], [method(self, n, id)], methods)
findall([head, body], [clause(self, head, body)], clauses)
end

send(:class, :new, [%{name: :process, super: :object, slots: []}, _])
defmethod(:process, :init, [self, args, new_obj]) do
map_get(args, :head, head)
map_get(args, :body, body)
gensym(new_obj)
spawn_process(new_obj, head, body)
end
0 ->
AL.Bootstrap.Core.setup()
AL.Bootstrap.Users.setup()
AL.Bootstrap.Lists.setup()
AL.Bootstrap.ElixirProcess.setup()
AL.Bootstrap.Process.setup()
AL.Bootstrap.Constraints.setup()
_ ->
:ok
end
end
end
74 changes: 74 additions & 0 deletions lib/AL/bootstrap/constraints.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
defmodule AL.Bootstrap.Constraints do
use AL

def setup() do
run do
new(:class, %{name: :cell, super: :object, slots: [:subscribers, :value, :name]}, _)

defmethod(:cell, :allocate, [self, args, cell_name]) do
class(self, meta)
map_get(args, :name, cell_name)

set_class(cell_name, meta)
set_super(cell_name, :object)
end

defmethod(:cell, :init, [self, args, self]) do
map_get(args, :name, cell_name)
set_slots(cell_name, %{name: cell_name, subscribers: [], value: :absent})
end

defmethod(:cell, :constrain, [self, value]) do
get_slot(self, :value, :absent)
set_slots(self, %{value: value})

forall([get_slot(self, :subscribers, subscribers),
member(subscribers, subscriber)],
[send_async(subscriber, :cell_updated, [self, value])])
cut
end

defmethod(:cell, :subscribe, [self, subscriber]) do
get_slot(self, :subscribers, subscribers)
set_slots(self, %{subscribers: [subscriber | subscribers]})
end

new(:class, %{name: :propagator, super: :object, slots: [:input_cells, :output_cell]}, _)

defmethod(:propagator, :allocate, [self, args, propagator]) do
class(self, meta)
gensym(propagator)

set_class(propagator, meta)
set_super(propagator, :object)
end

defmethod(:propagator, :init, [self, args, self]) do
map_get(args, :input_cells, input_cells)
map_get(args, :output_cell, output_cell)

set_slots(self, %{input_cells: input_cells, output_cell: output_cell})

forall([member(input_cells, input_cell)],
[subscribe(input_cell, self)])

send_async(self, :cell_updated, [:none, :none])
end

defmethod(:propagator, :cell_updated, [self, _cell_name, _value]) do
get_slot(self, :input_cells, input_cells)
get_slot(self, :output_cell, output_cell)

findall(input_cell_value,
[member(input_cells, input_cell),
get_slot(input_cell, :value, input_cell_value)],
input_cell_values)
forall([member(input_cell_values, input_cell_value)],
[not([unify(input_cell_value, :absent)])])

constrain(self, input_cell_values, output_value)
send_async(output_cell, :constrain, [output_value])
end
end
end
end
119 changes: 119 additions & 0 deletions lib/AL/bootstrap/core.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
defmodule AL.Bootstrap.Core do
use AL

def setup() do
run do
set_class(:class, :class)
set_class(:object, :class)
set_class(:behaviour, :class)

set_super(:class, :object)
set_super(:behaviour, :object)

set_method(:object, :lookup, :lookup)
set_method(:object, :send, :send)
set_method(:object, :meta, :metaclass)
set_method(:object, :defmethod, :defmethod)

set_class(:metaclass, :behaviour)

set_oapply(:metaclass, [self, class, meta]) do
class(self, class)
class(class, meta)
end

set_class(:lookup, :behaviour)
set_oapply(:lookup, [self, name, id]) do
alternative([method(self, name, id)],
[super(self, super),
lookup(super, name, id)])
end

set_class(:send, :behaviour)
set_oapply(:send, [self, method, args]) do
class(self, class)
implies(
[method(self, method, id)],
[
oapply(id, [self | args])
],
[implies(
[lookup(class, method, id)],
[
oapply(id, [self | args])
],
[:fail]
)])
end

set_class(:defmethod, :behaviour)
set_oapply(:defmethod, [self, method_name, head, body]) do
fresh_id(impl)
set_method(self, method_name, impl)
set_class(impl, :behaviour)
set_oapply(impl, head, body)
end

set_class(:map, :class)
set_super(:map, :object)

set_class(:map_get, :behaviour)
set_method(:map, :get, :map_get)

set_class(:map_put, :behaviour)
set_method(:map, :put, :map_put)

defmethod(:class, :construct, [self, %{class: self}]) do
end

set_method(:class, :allocate, :allocate_class)
set_class(:allocate_class, :behaviour)
set_oapply(:allocate_class, [self, args, name]) do
map_get(args, :name, name)
map_get(args, :super, super)
map_get(args, :slots, slots)

class(self, meta)

set_class(name, meta)
set_super(name, super)
set_slots(name, slots)
end

defmethod(:object, :allocate, [self, _, self]) do
# print(["allocate", self])
end

defmethod(:object, :init, [self, _, self]) do
# print(["initialise", self])
end

defmethod(:class, :new, [self, args, new]) do
construct(self, construct)
allocate(construct, args, alloc)
init(alloc, args, new)
end

defmethod(:object, :examine, [self, %{
id: self,
classes: classes,
objects: objects,
supers: supers,
subs: subs,
methods: methods,
providers: providers,
clauses: clauses,
slots: slots}]) do
findall(c, [class(self, c)], classes)
findall(c, [class(c, self)], objects)
findall(s, [super(self, s)], supers)
findall(sub, [super(sub, self)], subs)
findall([n, id], [method(self, n, id)], methods)
findall([provider, n], [method(provider, n, self)], providers)
findall([head, body], [clause(self, head, body)], clauses)
findall([slot_name, slot_value], [get_slot(self, slot_name, slot_value)], slots)
end

end
end
end
19 changes: 19 additions & 0 deletions lib/AL/bootstrap/elixir_process.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
defmodule AL.Bootstrap.ElixirProcess do
use AL

def setup() do
run do
new(:class, %{name: :elixir_process, super: :object, slots: []}, _)
defmethod(:elixir_process, :allocate, [self, args, new_obj]) do
class(self, meta)
map_get(args, :name, new_obj)
set_class(new_obj, meta)
set_super(new_obj, :elixir_process)
end
defmethod(:elixir_process, :init, [self, args, self]) do
map_get(args, :pid, pid)
set_slots(self, %{pid: pid})
end
end
end
end
Loading