aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/ffi.lux
blob: f76ab1675e8230bf6bcd9fb5a349409fcfb868b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
... This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
... If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

(.require
 [library
  [lux (.except Symbol Alias Global Declaration Pattern global function type_of undefined alias)
   [abstract
    ["[0]" monad (.only do)]]
   [control
    ["<>" parser (.use "[1]#[0]" monad)]
    ["[0]" io]
    ["[0]" maybe (.use "[1]#[0]" functor)]
    ["[0]" try (.only Try)]]
   [data
    ["[0]" product]
    ["[0]" text (.use "[1]#[0]" equivalence)
     ["%" \\format]]
    [collection
     ["[0]" list (.use "[1]#[0]" monoid monad mix)]]]
   ["[0]" meta (.only)
    ["[0]" location]
    ["[0]" code (.only)
     ["<[1]>" \\parser (.only Parser)]]
    ["[0]" macro (.only with_symbols)
     [syntax (.only syntax)]
     ["[0]" template]]
    [type (.only sharing)
     ["[0]" nominal (.except #name def)]]
    [compiler
     ["@" target (.only)
      ["[0]" js]
      ["[0]" python]]
     [arity (.only Arity)]
     [reference (.only Reference)
      [variable (.only Register)]]
     [language
      [lux
       ["[0]" analysis
        ["[1]/[0]" simple]
        [complex (.only Complex)]
        [pattern (.only Pattern)]]
       ["[0]" synthesis
        ["[1]/[0]" simple]
        [access (.only Access)
         [side (.only Side)]
         ["[1]/[0]" member]]]]]]]]])

(for @.js (these (type (Analysis_Branch of)
                   [Pattern of])

                 (type (Analysis_Match of)
                   [(Analysis_Branch of) (List (Analysis_Branch of))])

                 (type (Environment of)
                   (List of))

                 (type (Extension of)
                   [.Symbol (List of)])

                 (with_expansions [@ ($ (Analysis~' $))]
                   (type (Analysis~' $)
                     (Or analysis/simple.Simple
                         (Complex @)
                         Reference
                         [@ (Analysis_Match @)]
                         [(Environment @) @]
                         [@ @]
                         (Extension @))))

                 (type Analysis~
                   (Ann Location
                        (Analysis~' (Ann Location))))

                 (def extension_analysis
                   (template (_ <name> <parameter>)
                     [(is Analysis~
                          [location.dummy {5 #1 [<name> <parameter>]}])]))

                 (def text_analysis
                   (template (_ <it>)
                     [(is Analysis~
                          [location.dummy {0 #0 {5 #1 <it>}}])]))

                 (def analysis
                   (template (_ <name> <bindings> <parser> <inputs> <body>)
                     [(def .public <name>
                        (<| (as .Analysis)
                            (.function (_ phase archive inputs))
                            (.function (_ state))
                            (let [<bindings> [phase archive state]]
                              (when (<code>.result <parser> inputs)
                                {try.#Success <inputs>}
                                <body>

                                {try.#Failure error}
                                {try.#Failure (%.format "Invalid inputs for extension..."
                                                        text.\n error)}))))]))

                 (type (Synthesis_Road value next)
                   [value next])

                 (type (Synthesis_Fork value next)
                   [(Synthesis_Road value next)
                    (List (Synthesis_Road value next))])

                 (type (Synthesis_Path s)
                   (Or Any
                       Register
                       Access
                       [Bit (Synthesis_Path s) (Maybe (Synthesis_Path s))]
                       (Synthesis_Fork I64 (Synthesis_Path s))
                       (Synthesis_Fork Frac (Synthesis_Path s))
                       (Synthesis_Fork Text (Synthesis_Path s))
                       [(Synthesis_Path s) (Synthesis_Path s)]
                       [(Synthesis_Path s) (Synthesis_Path s)]
                       s))

                 (type (Synthesis_Abstraction s)
                   [(Environment s) Arity s])

                 (type (Synthesis_Apply s)
                   [s (List s)])

                 (type (Synthesis_Function s)
                   (Or (Synthesis_Abstraction s)
                       (Synthesis_Apply s)))

                 (type (Synthesis_Branch s)
                   (Or [s s]
                       [s Register s]
                       [s s s]
                       [(List synthesis/member.Member) s]
                       [s (Synthesis_Path s)]))

                 (type (Synthesis_Scope s)
                   [Register (List s) s])

                 (type (Synthesis_Loop s)
                   (Or (Synthesis_Scope s)
                       (List s)))

                 (type (Synthesis_Control s)
                   (Or (Synthesis_Branch s)
                       (Synthesis_Loop s)
                       (Synthesis_Function s)))

                 (with_expansions [@ ($ (Synthesis~' $))]
                   (type (Synthesis~' $)
                     (Or synthesis/simple.Simple
                         (Complex @)
                         Reference
                         (Synthesis_Control @)
                         (Extension @))))

                 (type Synthesis~
                   (Ann Location
                        (Synthesis~' (Ann Location))))

                 (def text_synthesis
                   (template (_ <@> <it>)
                     [[<@> {0 #0 {2 #1 <it>}}]]))

                 (def translation
                   (syntax (_ [<name> <code>.any
                               <bindings> <code>.any
                               <inputs> (<>.or <code>.local
                                               <code>.any)
                               <body> <code>.any])
                     (with_symbols ['_ 'phase 'archive 'inputs 'state]
                       (in (list (` (def .public (, <name>)
                                      (<| (as .Translation)
                                          (.function ((, '_) (, 'phase) (, 'archive) (, 'inputs)))
                                          (.function ((, '_) (, 'state)))
                                          (let [(, <bindings>) [(, 'phase) (, 'archive) (, 'state)]]
                                            (, (when <inputs>
                                                 {.#Left <inputs>}
                                                 (` (when (is (List Synthesis~) (, 'inputs))
                                                      (, (code.local <inputs>))
                                                      (, <body>)))
                                                 
                                                 {.#Right <inputs>}
                                                 (` (when (is (List Synthesis~) (, 'inputs))
                                                      (, <inputs>)
                                                      (, <body>)

                                                      (, '_)
                                                      {try.#Failure "Invalid inputs for extension."})))))))))))))

                 (translation undefined?|translation
                   [phase archive state]
                   (list it)
                   (do try.monad
                     [.let [phase (sharing [archive it state]
                                    (is [archive it state]
                                        [archive it state])
                                    (is (-> archive it state
                                            (Try [state js.Expression]))
                                        (as_expected phase)))]
                      [state it] (phase archive it state)]
                     (in [state (js.= js.undefined it)])))

                 (analysis undefined?|analysis
                   [phase archive state]
                   <code>.any
                   it
                   (do try.monad
                     [.let [phase (sharing [archive state]
                                    (is [archive state]
                                        [archive state])
                                    (is (-> archive Code state
                                            (Try [state Analysis~]))
                                        (as_expected phase)))]
                      [state it] (phase archive (` (.is .Any (, it))) state)]
                     (in [state (extension_analysis (symbol ..undefined?|translation)
                                                    (list it))])))

                 (def .public undefined?
                   (template (undefined? <it>)
                     [(.as .Bit (.is .Any (undefined?|analysis <it>)))]))

                 (translation undefined|translation
                   [phase archive state]
                   (list)
                   {try.#Success [state js.undefined]})

                 (analysis undefined|analysis
                   [phase archive state]
                   <code>.end
                   _
                   {try.#Success [state (extension_analysis (symbol ..undefined|translation)
                                                            (list))]})

                 (def .public undefined
                   (template (_)
                     [(.is ..Undefined (undefined|analysis))]))

                 (def (pairs it)
                   (All (_ a) (-> (List a) (List [a a])))
                   (when it
                     (list.partial left right tail)
                     (list.partial [left right] (pairs tail))
                     
                     (list)
                     (list)

                     _
                     (.undefined)))

                 (translation object|translation
                   [phase archive state]
                   it
                   (do [! try.monad]
                     [.let [phase (sharing [archive state]
                                    (is [archive state]
                                        [archive state])
                                    (is (-> archive Synthesis~ state
                                            (Try [state js.Expression]))
                                        (as_expected phase)))]
                      [state output] (monad.mix !
                                                (sharing [state]
                                                  (is state
                                                      state)
                                                  (is (-> [Synthesis~ Synthesis~] [state (List [Text js.Expression])]
                                                          (Try [state (List [Text js.Expression])]))
                                                      (.function (_ [key value] [state output])
                                                        (when key
                                                          (text_synthesis @ key)
                                                          (do try.monad
                                                            [[state value] (phase archive value state)]
                                                            (in [state (list.partial [key value] output)]))
                                                          
                                                          _
                                                          (.undefined)))))
                                                [state (list)]
                                                (pairs it))]
                     (in [state (js.object (list.reversed output))])))

                 (analysis object|analysis
                   [phase archive state]
                   (<>.some (<>.and <code>.text <code>.any))
                   it
                   (do [! try.monad]
                     [.let [phase (sharing [archive state]
                                    (is [archive state]
                                        [archive state])
                                    (is (-> archive Code state
                                            (Try [state Analysis~]))
                                        (as_expected phase)))]
                      [state output] (monad.mix ! (.function (_ [key value] [state output])
                                                    (do !
                                                      [[state value] (phase archive (` (.is .Any (, value))) state)]
                                                      (in [state (list.partial value (text_analysis key) output)])))
                                                [state (list)]
                                                it)]
                     (in [state (extension_analysis (symbol ..object|translation)
                                                    (list.reversed output))])))

                 (def .public object
                   (syntax (_ [it (<>.some <code>.any)])
                     (in (list (` (.as (..Object .Any)
                                       (object|analysis (,* it))))))))

                 (translation set|translation
                   [phase archive state]
                   (list (text_synthesis @ field) value object)
                   (do try.monad
                     [.let [phase (sharing [archive state]
                                    (is [archive state]
                                        [archive state])
                                    (is (-> archive Synthesis~ state
                                            (Try [state js.Expression]))
                                        (as_expected phase)))]
                      [state value] (phase archive value state)
                      [state object] (phase archive object state)]
                     (in [state (js.set (js.the field object) value)])))

                 (analysis set|analysis
                   [phase archive state]
                   (all <>.and <code>.text <code>.any <code>.any)
                   [field value object]
                   (do try.monad
                     [.let [phase (sharing [archive state]
                                    (is [archive state]
                                        [archive state])
                                    (is (-> archive Code state
                                            (Try [state Analysis~]))
                                        (as_expected phase)))]
                      [state value] (phase archive (` (.is .Any (, value))) state)
                      [state object] (phase archive (` (.is (..Object .Any) (, object))) state)]
                     (in [state (extension_analysis (symbol ..set|translation)
                                                    (list (text_analysis field) value object))])))

                 (def .public set
                   (syntax (_ [field <code>.any
                               value <code>.any
                               object <code>.any])
                     (in (list (` (.as .Any (set|analysis (, field) (, value) (, object))))))))
                 )
     ... else
     (these))

(with_expansions [<constant> (for @.js .js_constant#
                                  @.python .python_constant#
                                  @.lua .lua_constant#
                                  @.ruby .ruby_constant#)
                  <apply> (for @.js .js_apply#
                               @.python .python_apply#
                               @.lua .lua_apply#
                               @.ruby .ruby_apply#)
                  <new> (for @.js .js_object_new#
                             @.python .python_apply#
                             (these))
                  <do> (for @.js .js_object_do#
                            @.python .python_object_do#
                            @.lua .lua_object_do#
                            @.ruby .ruby_object_do#)
                  <get> (for @.js .js_object_get#
                             @.python .python_object_get#
                             @.lua .lua_object_get#
                             @.ruby .ruby_object_get#
                             (these))
                  <set> (for @.lua .lua_object_set#
                             @.ruby .ruby_object_set#
                             (these))
                  <import> (for @.python .python_import#
                                @.lua .lua_import#
                                @.ruby .ruby_import#
                                (these))
                  <function> (for @.js .js_function#
                                  @.python .python_function#
                                  @.lua .lua_function#
                                  (these))]
  (nominal.def .public (Object of) Any)

  (with_expansions [<un_common> (for @.js (these [Symbol]
                                                 [Null]
                                                 [Undefined])
                                     @.python (these [None]
                                                     [Dict])
                                     @.lua (these [Nil]
                                                  [Table])
                                     @.ruby (these [Nil]))
                    <un_common> <un_common>]
    (with_template [<name>]
      [(with_expansions [<of> (template.symbol [<name> "'"])]
         (nominal.def <of>
           Any
           
           (type .public <name>
             (Object <of>))))]

      [Function]
      <un_common>
      ))

  (with_expansions [<un_common> (for @.js (these [Number  Frac])
                                     @.python (these [Integer Int]
                                                     [Float   Frac])
                                     @.lua (these [Integer Int]
                                                  [Float   Frac])
                                     @.ruby (these [Integer Int]
                                                   [Float   Frac]))
                    <un_common> <un_common>]
    (with_template [<name> <type>]
      [(type .public <name>
         <type>)]

      [Boolean Bit]
      [String  Text]
      <un_common>
      ))

  (type Alias
    (Maybe Text))

  (def alias
    (Parser Alias)
    (<>.maybe (<>.after (<code>.this (' "as")) <code>.local)))

  (type Optional
    (Record
     [#optional? Bit
      #mandatory Code]))

  (def optional
    (Parser Optional)
    (let [token "?"]
      (<| (<>.and (<>.parses? (<code>.this_text token)))
          (<>.after (<>.not (<code>.this_text token)))
          <code>.any)))

  (type (Named a)
    (Record
     [#name Text
      #alias Alias
      #anonymous a]))

  (with_template [<case> <name>]
    [(def <case>
       (All (_ a) (-> (Parser a) (Parser (Named a))))
       (|>> (all <>.and
                 <name>
                 ..alias
                 )))]

    [named <code>.local]
    [anonymous (<>#in "")]
    )

  (type Output
    Optional)

  (def output
    (Parser Output)
    ..optional)

  (type Global
    (Named Output))

  (def variables
    (Parser (List Text))
    (<>.else (list) (<code>.tuple (<>.some <code>.local))))

  (def (generalized $ it)
    (All (_ a)
      (-> (-> (List Text) a a)
          (-> (Parser a) (Parser a))))
    (do <>.monad
      [variables ..variables
       it it]
      (in ($ variables it))))

  (type Input
    (Record
     [#variables (List Text)
      #parameters (List Optional)
      #io? Bit
      #try? Bit]))

  (def input
    (Parser Input)
    (all <>.and
         (<>#in (list))
         (<code>.tuple (<>.some ..optional))
         (<>.parses? (<code>.this_text "io"))
         (<>.parses? (<code>.this_text "try"))))

  (type Constructor
    (Named Input))

  (def constructor
    (Parser Constructor)
    (<| <code>.form
        (..generalized (has [#anonymous #variables]))
        (<>.after (<code>.this (' new)))
        (..anonymous ..input)))

  (type (Member a)
    (Record
     [#static? Bit
      #member a]))

  (def static!
    (Parser Any)
    (<code>.this_text "static"))

  (def (member it)
    (All (_ a)
      (-> (Parser a)
          (Parser (Member a))))
    (do [! <>.monad]
      [static? (<>.parses? ..static!)]
      (of ! each
          (|>> [#static? static?
                #member])
          it)))

  (type Field
    (Member (Named Output)))

  (def field
    (Parser Field)
    (<| <code>.form
        ..member
        ..named
        ..output))

  (type Procedure
    (Record
     [#input Input
      #output Optional]))

  (def procedure
    (Parser (Named Procedure))
    (<| (..generalized (has [#anonymous #input #variables]))
        ..named
        (all <>.and
             ..input
             ..optional
             )))

  (type Method
    (Member (Named Procedure)))

  (def method
    (Parser Method)
    (<| <code>.form
        ..member
        ..procedure))

  (`` (`` (type Sub
            (Variant
             (,, (for @.lua (,, (these))
                      @.ruby (,, (these))
                      {#Constructor Constructor}))
             {#Field Field}
             {#Method Method}))))

  (`` (`` (def sub
            (Parser Sub)
            (all <>.or
                 (,, (for @.lua (,, (these))
                          @.ruby (,, (these))
                          ..constructor))
                 ..field
                 ..method
                 ))))

  (def parameters
    (-> (List Optional) (List Optional))
    (|>> list.enumeration
         (list#each (.function (_ [idx [optional? type]])
                      [#optional? optional?
                       #mandatory (|> idx %.nat code.local)]))))

  (def (output_type it)
    (-> Optional Code)
    (if (the #optional? it)
      (` (.Maybe (, (the #mandatory it))))
      (the #mandatory it)))

  (`` (with_template [<lux_it> <host_it>
                      <lux_?> <host_?>]
        [(def .public (<lux_it> _)
           (-> Any Nothing)
           (as_expected (<host_it>)))

         (def .public <lux_?>
           (-> Any Bit)
           (|>> <host_?>))

         (template.with_locals [g!it]
           (these (def g!it' (' g!it))
                  (def (host_optional it)
                    (-> Optional Code)
                    (.if (.the #optional? it)
                      (` (.when (, (the #mandatory it))
                           {.#Some (, g!it')}
                           (, g!it')

                           {.#None}
                           (<host_it>)))
                      (the #mandatory it)))

                  (def (lux_optional it output)
                    (-> Optional Code Code)
                    (` (.let [(, g!it') (, output)]
                         (, (if (the #optional? it)
                              (` (.if (<host_?> (, g!it'))
                                   {.#None}
                                   {.#Some (, g!it')}))
                              (` (.if (.not (<host_?> (, g!it')))
                                   (, g!it')
                                   (.panic! "Invalid output."))))))))))]

        (,, (for @.js [null .js_object_null#
                       null? .js_object_null?#]
                 @.python [none .python_object_none#
                           none? .python_object_none?#]
                 @.lua [nil .lua_object_nil#
                        nil? .lua_object_nil?#]
                 @.ruby [nil .ruby_object_nil#
                         nil? .ruby_object_nil?#]))
        ))

  (type Declaration
    [Text (List Text)])

  (type Namespace
    Text)

  (type Class
    (Record
     [#declaration Declaration
      #class_alias Alias
      #namespace Namespace
      #members (List Sub)]))

  (def class
    (Parser Class)
    (all <>.and
         (<>.either (<>.and <code>.local
                            (<>#in (list)))
                    (<code>.form (<>.and <code>.local
                                         (<>.some <code>.local))))
         ..alias
         <code>.text
         (<>.some ..sub)))

  (type Import
    (Variant
     {#Class Class}
     {#Procedure (Named Procedure)}
     {#Global Global}))

  (def importP
    (Parser Import)
    (all <>.or
         ..class
         (<code>.form ..procedure)
         (<code>.form (..named ..output))))

  (def (input_type input :it:)
    (-> Input Code Code)
    (let [:it: (if (the #try? input)
                 (` (Try (, :it:)))
                 :it:)]
      (if (the #io? input)
        (` (io.IO (, :it:)))
        :it:)))

  (def (input_term input term)
    (-> Input Code Code)
    (let [term (if (the #try? input)
                 (` (.try (, term)))
                 term)]
      (if (the #io? input)
        (` (io.io (, term)))
        term)))

  (def (procedure_definition import! source it)
    (-> (List Code) Code (Named Procedure) Code)
    (let [g!it (|> (the #alias it)
                   (maybe.else (the #name it))
                   code.local)
          g!variables (list#each code.local (the [#anonymous #input #variables] it))
          input (the [#anonymous #input] it)
          :parameters: (the #parameters input)
          g!parameters (..parameters :parameters:)
          :output: (the [#anonymous #output] it)
          :input:/* (when :parameters:
                      {.#End}
                      (list (` []))
                      
                      parameters
                      (list#each ..output_type :parameters:))]
      (` (.def ((, g!it) (,* (when g!parameters
                               {.#End} (list g!it)
                               _ (list#each (the #mandatory) g!parameters))))
           (.All ((, g!it) (,* g!variables))
             (-> (,* :input:/*)
                 (, (|> :output:
                        ..output_type
                        (..input_type input)))))
           (.exec
             (,* import!)
             (.as_expected
              (, (<| (..input_term input)
                     (..lux_optional :output:)
                     (` (<apply> (.as_expected (, source))
                                 [(,* (list#each ..host_optional g!parameters))]))))))))))

  (def (namespaced namespace class alias member)
    (-> Namespace Text Alias Text Text)
    (|> namespace
        (text.replaced "[1]" (maybe.else class alias))
        (text.replaced "[0]" member)))

  (def class_separator ".")

  (def host_path
    (text.replaced .module_separator ..class_separator))

  (for @.js (these)
       (def (imported class)
         (-> Text Code)
         (when (text.all_split_by .module_separator class)
           {.#Item head tail}
           (list#mix (.function (_ sub super)
                       (` (<get> (, (code.text sub))
                                 (.as (..Object .Any)
                                      (, super)))))
                     (` (<import> (, (code.text head))))
                     tail)
           
           {.#End}
           (` (<import> (, (code.text class)))))))

  (def (global_definition import! it)
    (-> (List Code) Global Code)
    (let [g!name (|> (the #alias it)
                     (maybe.else (the #name it))
                     code.local)
          :output: (the #anonymous it)]
      (` (.def (, g!name)
           (, (..output_type :output:))
           (.exec
             (,* import!)
             (.as_expected
              (, (<| (lux_optional :output:)
                     (` (<constant> (, (code.text (..host_path (the #name it))))))))))))))
  
  (for @.lua (these)
       @.ruby (these)
       (def (constructor_definition [class_name class_parameters] alias namespace it)
         (-> Declaration Alias Namespace Constructor Code)
         (let [g!it (|> it
                        (the #alias)
                        (maybe.else "new")
                        (..namespaced namespace class_name alias)
                        code.local)
               input (the #anonymous it)
               g!input_variables (list#each code.local (the #variables input))
               :parameters: (the #parameters input)
               g!parameters (..parameters :parameters:)
               g!class_variables (list#each code.local class_parameters)
               g!class (` ((, (code.local (maybe.else class_name alias))) (,* g!class_variables)))
               :output: [#optional? false #mandatory g!class]
               unquantified_type (` (.-> (,* (when :parameters:
                                               (list)
                                               (list (` .Any))
                                               
                                               _
                                               (list#each ..output_type :parameters:)))
                                         (, (|> :output:
                                                ..output_type
                                                (..input_type input)))))
               quantified_type (when (list#composite g!class_variables g!input_variables)
                                 (list)
                                 unquantified_type

                                 _
                                 (` (.All ((, g!it) (,* g!class_variables) (,* g!input_variables))
                                      (, unquantified_type))))]
           (` (.def ((, g!it) (,* (when g!parameters
                                    {.#End} (list g!it)
                                    _ (list#each (the #mandatory) g!parameters))))
                (, quantified_type)
                (.as_expected
                 (, (<| (..input_term input)
                        (..lux_optional :output:)
                        (` (<new> (, (for @.js (` (<constant> (, (code.text (..host_path class_name)))))
                                          @.python (` (.as ..Function
                                                           (, (..imported class_name))))))
                                  [(,* (list#each ..host_optional g!parameters))]))))))))))

  (def (optional_value type value)
    (-> Optional Code Optional)
    [#optional? (the #optional? type)
     #mandatory value])
  
  (def (static_field_definition import! [class_name class_parameters] alias namespace it)
    (-> (List Code) Declaration Alias Namespace (Named Output) Code)
    (let [field (the #name it)
          g!it (|> (the #alias it)
                   (maybe.else field)
                   (..namespaced namespace class_name alias)
                   code.local)
          :field: (the #anonymous it)
          get (` (.as (io.IO (, (..output_type :field:)))
                      (io.io (, (<| (lux_optional :field:)
                                    (for @.js (` (<constant> (, (code.text (%.format (..host_path class_name) "." field)))))
                                         @.ruby (` (<constant> (, (code.text (%.format (..host_path class_name) "::" field)))))
                                         ... else
                                         (` (<get> (, (code.text field))
                                                   (, (..imported class_name))))))))))
          set (` (.as (io.IO .Any)
                      (io.io (, (for @.js (` (..set (, (code.text field))
                                                    (, (host_optional (optional_value :field: (` ((,' ,) (, g!it))))))
                                                    (.as (..Object .Any)
                                                         (<constant> (, (code.text (..host_path class_name)))))))
                                     @.ruby (` (<set> (, (code.text field))
                                                      (, (host_optional (optional_value :field: (` ((,' ,) (, g!it))))))
                                                      (<constant> (, (code.text (..host_path class_name))))))
                                     @.python (` (<apply> (<constant> "setattr")
                                                          [(, (..imported class_name))
                                                           (, (code.text field))
                                                           (, (host_optional (optional_value :field: (` ((,' ,) (, g!it))))))]))
                                     ... else
                                     (` (<set> (, (code.text field))
                                               (, (host_optional (optional_value :field: (` ((,' ,) (, g!it))))))
                                               (, (..imported class_name)))))))))]
      (` (def (, g!it)
           (syntax ((, g!it) [(, g!it) (<>.maybe <code>.any)])
             (.of meta.monad (,' in)
                  (.list (`' (.exec
                               (,* import!)
                               ((,' ,) (when (, g!it)
                                         {.#None}
                                         (`' (, get))

                                         {.#Some (, g!it)}
                                         (`' (, set)))))))))))))

  (def (virtual_field_definition [class_name class_parameters] alias namespace it)
    (-> Declaration Alias Namespace (Named Output) Code)
    (let [name (the #name it)
          g!it (|> (the #alias it)
                   (maybe.else name)
                   (..namespaced namespace class_name alias)
                   code.local)
          path (%.format (..host_path class_name) "." name)
          :field: (the #anonymous it)
          g!variables (list#each code.local class_parameters)
          g!class (` ((, (code.local (maybe.else class_name alias))) (,* g!variables)))]
      (` (.def ((, g!it) (, g!it))
           (.All ((, g!it) (,* g!variables))
             (.-> (, g!class)
                  (, (..output_type :field:))))
           (.as_expected
            (, (<| (lux_optional :field:)
                   (` (<get> (, (code.text name)) (, g!it))))))))))

  (def (field_definition import! class alias namespace it)
    (-> (List Code) Declaration Alias Namespace Field Code)
    (if (the #static? it)
      (..static_field_definition import! class alias namespace (the #member it))
      (..virtual_field_definition class alias namespace (the #member it))))

  (def (static_method_definition import! [class_name class_parameters] alias namespace it)
    (-> (List Code) Declaration Alias Namespace (Named Procedure) Code)
    (let [method (the #name it)
          name (|> (the #alias it)
                   (maybe.else (the #name it))
                   (..namespaced namespace class_name alias))]
      (|> it
          (has #alias {.#Some name})
          (..procedure_definition import!
                                  (for @.js (` (<constant> (, (code.text (%.format (..host_path class_name) "." method)))))
                                       @.ruby (` (<constant> (, (code.text (%.format (..host_path class_name) "::" method)))))
                                       (` (<get> (, (code.text method))
                                                 (.as (..Object .Any)
                                                      (, (..imported class_name))))))))))

  (def (virtual_method_definition [class_name class_parameters] alias namespace it)
    (-> Declaration Alias Namespace (Named Procedure) Code)
    (let [method (the #name it)
          g!it (|> (the #alias it)
                   (maybe.else method)
                   (..namespaced namespace class_name alias)
                   code.local)
          procedure (the #anonymous it)
          input (the #input procedure)
          g!input_variables (list#each code.local (the #variables input))
          :parameters: (the #parameters input)
          g!parameters (..parameters :parameters:)
          g!class_variables (list#each code.local class_parameters)
          g!class (` ((, (code.local (maybe.else class_name alias))) (,* g!class_variables)))
          :output: (the #output procedure)]
      (` (.def ((, g!it) (,* (list#each (the #mandatory) g!parameters)) (, g!it))
           (.All ((, g!it) (,* g!class_variables) (,* g!input_variables))
             (.-> (,* (list#each ..output_type :parameters:))
                  (, g!class)
                  (, (|> :output:
                         ..output_type
                         (..input_type input)))))
           (.as_expected
            (, (<| (..input_term input)
                   (..lux_optional :output:)
                   (` (<do> (, (code.text method))
                            (, g!it)
                            [(,* (list#each ..host_optional g!parameters))])))))))))

  (def (method_definition import! class alias namespace it)
    (-> (List Code) Declaration Alias Namespace Method Code)
    (if (the #static? it)
      (static_method_definition import! class alias namespace (the #member it))
      (virtual_method_definition class alias namespace (the #member it))))

  (def .public import
    (syntax (_ [host_module (<>.maybe <code>.text)
                it ..importP])
      (let [host_module_import! (is (List Code)
                                    (when host_module
                                      {.#Some host_module}
                                      (list (` (<import> (, (code.text host_module)))))

                                      {.#None}
                                      (list)))]
        (when it
          {#Global it}
          (in (list (..global_definition host_module_import! it)))

          {#Procedure it}
          (in (list (..procedure_definition host_module_import!
                                            (` (<constant> (, (code.text (..host_path (the #name it))))))
                                            it)))

          {#Class it}
          (let [class (the #declaration it)
                alias (the #class_alias it)
                [class_name class_parameters] class
                namespace (the #namespace it)
                g!class_variables (list#each code.local class_parameters)
                declaration (` ((, (code.local (maybe.else class_name alias)))
                                (,* g!class_variables)))]
            (in (list.partial (` (.type (, declaration)
                                   (..Object (.Nominal (, (code.text (..host_path class_name)))
                                                       [(,* g!class_variables)]))))
                              (list#each (.function (_ member)
                                           (`` (`` (when member
                                                     (,, (for @.lua (,, (these))
                                                              @.ruby (,, (these))
                                                              (,, (these {#Constructor it}
                                                                         (..constructor_definition class alias namespace it)))))
                                                     
                                                     {#Field it}
                                                     (..field_definition host_module_import! class alias namespace it)
                                                     
                                                     {#Method it}
                                                     (..method_definition host_module_import! class alias namespace it)))))
                                         (the #members it)))))
          ))))

  (for @.ruby (these)
       (def .public function
         (syntax (_ [[self inputs] (<code>.form
                                    (all <>.and
                                         <code>.local
                                         (<code>.tuple (<>.some (<>.and <code>.any <code>.any)))))
                     type <code>.any
                     term <code>.any])
           (in (list (` (.<| (.as ..Function)
                             (<function> (, (code.nat (list.size inputs))))
                             (.as (.-> [(,* (list.repeated (list.size inputs) (` .Any)))]
                                       .Any))
                             (.is (.-> [(,* (list#each product.right inputs))]
                                       (, type)))
                             (.function ((, (code.local self)) [(,* (list#each product.left inputs))])
                               (, term)))))))))

  (for @.js (these (def .public type_of
                     (template (type_of object)
                       [(.js_type_of# object)]))

                   (def .public global
                     (syntax (_ [type <code>.any
                                 [head tail] (<code>.tuple (<>.and <code>.local (<>.some <code>.local)))])
                       (with_symbols [g!_]
                         (let [global (` (.js_constant# (, (code.text head))))]
                           (when tail
                             {.#End}
                             (in (list (` (is (.Maybe (, type))
                                              (when (..type_of (, global))
                                                "undefined"
                                                {.#None}

                                                (, g!_)
                                                {.#Some (as (, type) (, global))})))))
                             
                             {.#Item [next tail]}
                             (let [separator "."]
                               (in (list (` (is (.Maybe (, type))
                                                (when (..type_of (, global))
                                                  "undefined"
                                                  {.#None}

                                                  (, g!_)
                                                  (..global (, type) [(, (code.local (%.format head "." next)))
                                                                      (,* (list#each code.local tail))]))))))))))))

                   (def !defined?
                     (template (_ <global>)
                       [(.when (..global Any <global>)
                          {.#None}
                          .false

                          {.#Some _}
                          .true)]))

                   (with_template [<name> <global>]
                     [(def .public <name>
                        Bit
                        (!defined? <global>))]

                     [on_browser? [window]]
                     [on_nashorn? [java lang Object]]
                     )

                   (def .public on_node_js?
                     Bit
                     (|> (..global (Object Any) [process])
                         (maybe#each (|>> []
                                          (.js_apply# (.js_constant# "Object.prototype.toString.call"))
                                          (as Text)
                                          (text#= "[object process]")))
                         (maybe.else false))))
       (these))
  )