-
Notifications
You must be signed in to change notification settings - Fork 463
/
Builtins.hs
1512 lines (1248 loc) · 63.6 KB
/
Builtins.hs
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
-- editorconfig-checker-disable-file
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module PlutusCore.Default.Builtins where
import PlutusPrelude
import PlutusCore.Builtin
import PlutusCore.Data
import PlutusCore.Default.Universe
import PlutusCore.Evaluation.Machine.BuiltinCostModel
import PlutusCore.Evaluation.Machine.ExBudgetStream
import PlutusCore.Evaluation.Machine.ExMemoryUsage
import PlutusCore.Evaluation.Result
import PlutusCore.Pretty
import Codec.Serialise (serialise)
import Crypto (verifyEcdsaSecp256k1Signature, verifyEd25519Signature_V1, verifyEd25519Signature_V2,
verifySchnorrSecp256k1Signature)
import Data.ByteString qualified as BS
import Data.ByteString.Hash qualified as Hash
import Data.ByteString.Lazy qualified as BSL
import Data.Char
import Data.Ix
import Data.Text (Text)
import Data.Text.Encoding (decodeUtf8', encodeUtf8)
import Flat hiding (from, to)
import Flat.Decoder
import Flat.Encoder as Flat
import Prettyprinter (viaShow)
-- See Note [Pattern matching on built-in types].
-- TODO: should we have the commonest built-in functions at the front to have more compact encoding?
-- | Default built-in functions.
--
-- When updating these, make sure to add them to the protocol version listing!
-- See Note [New builtins and protocol versions]
data DefaultFun
-- Integers
= AddInteger
| SubtractInteger
| MultiplyInteger
| DivideInteger
| QuotientInteger
| RemainderInteger
| ModInteger
| EqualsInteger
| LessThanInteger
| LessThanEqualsInteger
-- Bytestrings
| AppendByteString
| ConsByteString
| SliceByteString
| LengthOfByteString
| IndexByteString
| EqualsByteString
| LessThanByteString
| LessThanEqualsByteString
-- Cryptography and hashes
| Sha2_256
| Sha3_256
| Blake2b_256
| VerifyEd25519Signature -- formerly verifySignature
| VerifyEcdsaSecp256k1Signature
| VerifySchnorrSecp256k1Signature
-- Strings
| AppendString
| EqualsString
| EncodeUtf8
| DecodeUtf8
-- Bool
| IfThenElse
-- Unit
| ChooseUnit
-- Tracing
| Trace
-- Pairs
| FstPair
| SndPair
-- Lists
| ChooseList
| MkCons
| HeadList
| TailList
| NullList
-- Data
-- See Note [Pattern matching on built-in types].
-- It is convenient to have a "choosing" function for a data type that has more than two
-- constructors to get pattern matching over it and we may end up having multiple such data
-- types, hence we include the name of the data type as a suffix.
| ChooseData
| ConstrData
| MapData
| ListData
| IData
| BData
| UnConstrData
| UnMapData
| UnListData
| UnIData
| UnBData
| EqualsData
| SerialiseData
-- Misc monomorphized constructors.
-- We could simply replace those with constants, but we use built-in functions for consistency
-- with monomorphic built-in types. Polymorphic built-in constructors are generally problematic,
-- See note [Representable built-in functions over polymorphic built-in types].
| MkPairData
| MkNilData
| MkNilPairData
deriving stock (Show, Eq, Ord, Enum, Bounded, Generic, Ix)
deriving anyclass (NFData, Hashable, PrettyBy PrettyConfigPlc)
{- Note [Textual representation of names of built-in functions]. The plc parser
parses builtin names by looking at an enumeration of all of the built-in
functions and checking whether the given name matches the pretty-printed name,
obtained using the instance below. Thus the definitive forms of the names of
the built-in functions are obtained by applying the function below to the
constructor names above. -}
instance Pretty DefaultFun where
pretty fun = pretty $ case show fun of
"" -> "" -- It's really weird to have a function's name displayed as an empty string,
-- but if it's what the 'Show' instance does, the user has asked for it.
c : s -> toLower c : s
instance ExMemoryUsage DefaultFun where
memoryUsage _ = singletonRose 1
-- | Turn a function into another function that returns 'EvaluationFailure' when its second argument
-- is 0 or calls the original function otherwise and wraps the result in 'EvaluationSuccess'.
-- Useful for correctly handling `div`, `mod`, etc.
nonZeroArg :: (Integer -> Integer -> Integer) -> Integer -> Integer -> EvaluationResult Integer
nonZeroArg _ _ 0 = EvaluationFailure
nonZeroArg f x y = EvaluationSuccess $ f x y
{- Note [Constants vs built-in functions]
A constant is any value of a built-in type. For example, 'Integer' is a built-in type, so anything
of type 'Integer' is a constant.
On the contrary a built-in function can't be of a built-in type, because the type of a built-in
function is always of either the @all a. b@ form or the @a -> b@ one, none of which is a built-in
type. This is checked by the machinery, so if the user tries to add a built-in function that is not
of one of these forms, they'll get a nice custom type error.
A built-in function is associated with its Haskell implementation: there can be many built-in
functions of the same type, all doing different things, and there can be infinitely more _definable_
built-in functions of the same type that are not built-in functions nonetheless, because we didn't
register them as such by providing a Haskell implementation for each of them. This is the difference
between constants and built-in functions: the set of constants (infinite in our case) depends solely
on the set of available built-in types (also infinite in our case, because we have @Integer@,
@[Integer]@, @[[Integer]]@ etc), while the set of built-in functions is defined by explicitly
assigning each member a specific name and an associated with it Haskell implementation. It is
theoretically possible to have an infinite set of built-in functions, but we neither do that nor
need it, hence our set of built-in functions is finite.
The rule of thumb is: constants are raw data and built-in functions are, well, functions.
@(:)@ works as follows: it takes two constants wrapped as values, extracts an integer from the first
constant and a list of integers from the second one, prepends the former to the latter and wraps the
resulting list back into a constant, which gets wrapped into a value.
Why does @(:)@ have to be a built-in function? Because its type is
all a. a -> list a -> list a
and if we tried to make @(:)@ a constant we'd have to somehow make this type a built-in type and
promise that every value (i.e. every definable function) of this type can be used as a Plutus term,
which doesn't make any sense. Only the particular Haskell implementation that prepends an element to
a list is what we're interested in.
Why may @[]@ not be a built-in function? If its type is hardcoded to @[Integer]@, then that's a
built-in type and we know that anything of a built-in type can be embedded into a term as a
constant. I.e. @[] :: [Integer]@ is perfectly fine as a constant and does not need to be a built-in
function.
Why may @[]@ be a built-in function? If it's polymorphic over the type of the elements, then its
Plutus Core type is @all a. list a@ and that is not a built-in type, hence we have to make that a
built-in function.
-}
{- Note [How to add a built-in function: simple cases]
This Notes explains how to add a built-in function and how to read definitions of existing built-in
functions. It does not attempt to explain why things the way they are, that is explained in comments
in relevant files (will have a proper overview doc on that, but for now you can check out this
comment: https://github.com/input-output-hk/plutus/issues/4306#issuecomment-1003308938).
In order to add a new built-in function one needs to add a constructor to 'DefaultFun' and handle
it within the @ToBuiltinMeaning uni DefaultFun@ instance like this:
toBuiltinMeaning <Name> =
makeBuiltinMeaning
<denotation>
<costingFunction>
'makeBuiltinMeaning' creates a Plutus builtin out of its denotation (i.e. Haskell implementation)
and a costing function for it. Once a builtin is added, its Plutus type is kind-checked and printed
to a golden file automatically (consult @git status@).
Below we will enumerate what kind of denotations are accepted by 'makeBuiltinMeaning' without
touching any costing stuff.
1. The simplest example of an accepted denotation is a monomorphic function that takes values of
built-in types and returns a value of a built-in type as well. For example
encodeUtf8 :: Text -> BS.ByteString
You can feed 'encodeUtf8' directly to 'makeBuiltinMeaning' without specifying any types:
toBuiltinMeaning EncodeUtf8 =
makeBuiltinMeaning
encodeUtf8
<costingFunction>
This will add the builtin, the only two things that remain are implementing costing for this
builtin (out of the scope of this Note) and handling it within the @Flat DefaultFun@ instance
(see Note [Stable encoding of PLC]).
2. If the type of the denotation has any constrained type variables in it, all of them need to be
instantiated. For example feeding @(+)@ directly to 'makeBuiltinMeaning' will give you an error
message asking to instantiate constrained type variables, which you can do via an explicit type
annotation or type application or using any other way of specifying types.
Here's how it looks with a type application instantiating the type variable of @(+)@:
toBuiltinMeaning AddInteger =
makeBuiltinMeaning
((+) @Integer)
<costingFunction>
Or we can specify the whole type of the denotation by type-applying 'makeBuiltinMeaning':
toBuiltinMeaning AddInteger =
makeBuiltinMeaning
@(Integer -> Integer -> Integer)
(+)
<costingFunction>
Or we can simply annotate @(+)@ with its monomorphized type:
toBuiltinMeaning AddInteger =
makeBuiltinMeaning
((+) :: Integer -> Integer -> Integer)
<costingFunction>
All of these are equivalent.
It works the same way for a built-in function that has monomorphized polymorphic built-in types in
its type signature, for example:
toBuiltinMeaning SumInteger =
makeBuiltinMeaning
(sum :: [Integer] -> Integer)
<costingFunction>
3. Unconstrained type variables are fine, you don't need to instantiate them (but you may want to if
you want some builtin to be less general than what Haskell infers for its denotation). For example
toBuiltinMeaning IfThenElse =
makeBuiltinMeaning
(\b x y -> if b then x else y)
<costingFunction>
works alright. The inferred Haskell type of the denotation is
forall a. Bool -> a -> a -> a
whose counterpart in Plutus is
all a. bool -> a -> a -> a
and unsurprisingly it's the exact Plutus type of the added builtin.
It may seem like getting the latter from the former is entirely trivial, however
'makeBuiltinMeaning' jumps through quite a few hoops to achieve that and below we'll consider those
of them that are important to know to be able to use 'makeBuiltinMeaning' in cases that are more
complicated than a simple monomorphic or polymorphic function. But for now let's talk about a few
more simple cases.
4. Certain types are not built-in, but can be represented via built-in ones. For example, we don't
have 'Int' as a built-in, but we have 'Integer' and we can represent the former in terms of the
latter. The conversions between the two types are handled by 'makeBuiltinMeaning', so that the user
doesn't need to write them themselves and can just write
toBuiltinMeaning LengthOfByteString =
makeBuiltinMeaning
BS.length
<costingFunction>
directly (where @BS.length :: BS.ByteString -> Int@).
Note however that while it's always safe to convert an 'Int' to an 'Integer', doing the opposite is
not safe in general, because an 'Integer' may not fit into the range of 'Int'. For this reason
YOU MUST NEVER USE 'fromIntegral' AND SIMILAR FUNCTIONS THAT CAN SILENTLY UNDER- OR OVERFLOW
WHEN DEFINING A BUILT-IN FUNCTION
For example defining a builtin that takes an 'Integer' and converts it to an 'Int' using
'fromIntegral' is not allowed under any circumstances and can be a huge vulnerability.
It's completely fine to define a builtin that takes an 'Int' directly, though. How so? That's due
to the fact that the builtin application machinery checks that an 'Integer' is in the bounds of
'Int' before doing the conversion. If the bounds check succeeds, then the 'Integer' gets converted
to the corresponding 'Int', and if it doesn't, then the builtin application fails.
For the list of types that can be converted to/from built-in ones look into the file with the
default universe. If you need to add a new such type, just copy-paste what's done for an existing
one and adjust.
Speaking of builtin application failing:
5. A built-in function can fail. Whenever a builtin fails, evaluation of the whole program fails.
There's a number of ways a builtin can fail:
- as we've just seen a type conversion can fail due to an unsuccessful bounds check
- if the builtin expects, say, a 'Text' argument, but gets fed an 'Integer' argument
- if the builtin expects any constant, but gets fed a non-constant
- if its denotation runs in the 'EvaluationResult' and an 'EvaluationFailure' gets returned
Most of these are not a concern to the user defining a built-in function (conversions are handled
within the builtin application machinery, type mismatches are on the type checker and the person
writing the program etc), however explicitly returning 'EvaluationFailure' from a builtin is
something that happens commonly.
One simple example is a monomorphic function matching on a certain constructor and failing in all
other cases:
toBuiltinMeaning UnIData =
makeBuiltinMeaning
(\case
I i -> EvaluationSuccess i
_ -> EvaluationFailure)
<costingFunction>
The inferred type of the denotation is
Data -> EvaluationResult Integer
and the Plutus type of the builtin is
data -> integer
because the error effect is implicit in Plutus.
Returning @EvaluationResult a@ for a type variable @a@ is also fine, i.e. it doesn't matter whether
the denotation is monomorphic or polymorphic w.r.t. failing.
But note that
'EvaluationResult' MUST BE EXPLICITLY USED FOR ANY FAILING BUILTIN AND THROWING AN EXCEPTION
VIA 'error' OR 'throw' OR ELSE IS NOT ALLOWED AND CAN BE A HUGE VULNERABILITY. MAKE SURE THAT
NONE OF THE FUNCTIONS THAT YOU USE TO DEFINE A BUILTIN THROW EXCEPTIONS
An argument of a builtin can't have 'EvaluationResult' in its type -- only the result.
6. A builtin can emit log messages. For that it needs to run in the 'Emitter' monad. The ergonomics
are the same as with 'EvaluationResult': 'Emitter' can't appear in the type of an argument and
polymorphism is fine. For example:
toBuiltinMeaning Trace =
makeBuiltinMeaning
(\text a -> a <$ emit text)
<costingFunction>
The inferred type of the denotation is
forall a. Text -> a -> Emitter a
and the Plutus type of the builtin is
all a. text -> a -> a
because just like with the error effect, whether a function logs anything or not is not reflected
in its type.
'makeBuiltinMeaning' allows one to nest 'EvaluationResult' inside of 'Emitter' and vice versa,
but as always nesting monads inside of each other without using monad transformers doesn't have good
ergonomics, since computations of such a type can't be chained with a simple @(>>=)@.
This concludes the list of simple cases. Before we jump to the hard ones, we need to talk about how
polymorphism gets elaborated, so read Note [Elaboration of polymorphism] next.
-}
{- Note [Elaboration of polymorphism]
In Note [How to add a built-in function: simple cases] we defined the following builtin:
toBuiltinMeaning IfThenElse =
makeBuiltinMeaning
(\b x y -> if b then x else y)
<costingFunction>
whose inferred Haskell type is
forall a. Bool -> a -> a -> a
The way 'makeBuiltinMeaning' handles such a type is by traversing it and instantiating every type
variable. What a type variable gets instantiated to depends on where it appears. When the entire
type of an argument is a single type variable, it gets instantiated to @Opaque val VarN@ where
@VarN@ is pseudocode for "a Haskell type representing a Plutus type variable with 'Unique' N"
For the purpose of this explanation it doesn't matter what @VarN@ actually is and the representation
is subject to change anyway. 'Opaque' however is more fundamental and so we need to talk about it.
Here's how it's defined:
newtype Opaque val (rep :: GHC.Type) = Opaque
{ unOpaque :: val
}
I.e. @Opaque val rep@ is a wrapper around @val@, which stands for the type of value that an
evaluator uses (the builtins machinery is designed to work with any evaluator and different
evaluators define their type of values differently, for example 'CkValue' if the type of value for
the CK machine). The idea is simple: in order to apply the denotation of a builtin expecting, say,
an 'Integer' constant we need to actually extract that 'Integer' from the AST of the given value,
but if the denotation is polymorphic over the type of its argument, then we don't need to extract
anything, we can just pass the AST of the value directly to the denotation (which means the value
doesn't have to be a 'Constant', it can be completely arbitrary). I.e. in order for a polymorphic
function to become a monomorphic denotation (denotations are always monomorpic) all type variables
in the type of that function need to be instantiated at the type of value that a given evaluator
uses.
If we used just @val@ rather than @Opaque val rep@, we'd specialize
forall a. Bool -> a -> a -> a
to
Bool -> val -> val -> val
however then we'd need to separately specify the Plutus type of this builtin, since we can't infer
it from all these @val@s in the general case, for example does
val -> val -> val
stand for
all a. a -> a -> a
or
all a b. a -> b -> a
or something else?
So we use the @Opaque val rep@ wrapper, which is basically a @val@ with a @rep@ attached to it where
@rep@ represents the Plutus type of the argument/result, which is how we arrive at
Bool -> Opaque val Var0 -> Opaque val Var0 -> Opaque val Var0
Not only does this encoding allow us to specify both the Haskell and the Plutus types of the
builtin simultaneously, but it also makes it possible to infer such a type from a regular
polymorphic Haskell function (how that is done is a whole another story), so that we don't even need
to specify any types when creating built-in functions out of simple polymorphic denotations.
If we wanted to specify the type explicitly, we could do it like this (leaving out the @Var0@ thing
for the elaboration machinery to figure out):
toBuiltinMeaning IfThenElse =
makeBuiltinMeaning
@(Bool -> Opaque val _ -> Opaque val _ -> Opaque val _)
(\b x y -> if b then x else y)
<costingFunction>
and it would be equivalent to the original definition. We didn't do that, because why bother if
the correct thing gets inferred anyway.
Another thing we could do is define an auxiliary function with a type signature and explicit
'Opaque' while still having explicit polymorphism:
ifThenElse :: Bool -> Opaque val a -> Opaque val a -> Opaque val a
ifThenElse b x y = if b then x else y
toBuiltinMeaning IfThenElse =
makeBuiltinMeaning
ifThenElse
<costingFunction>
This achieves the same, but note how @a@ is now an argument to 'Opaque' rather than the entire type
of an argument. In order for this definition to elaborate to the same type as before @a@ needs to be
instantiated to just @Var0@, as opposed to @Opaque val Var0@, because the 'Opaque' part is
already there, so this is what the elaboration machinery does.
So regardless of which method of defining 'IfThenElse' we choose, the type of its denotation gets
elaborated to the same
Bool -> Opaque val Var0 -> Opaque val Var0 -> Opaque val Var0
which then gets digested, so that we can compute what Plutus type it corresponds to. The procedure
is simple: collect all distinct type variables, @all@-bind them and replace the usages with the
bound variables. This turns the type above into
all a. bool -> a -> a -> a
which is the Plutus type of the 'IfThenElse' builtin.
It's of course allowed to have multiple type variables, e.g. in the following snippet:
toBuiltinMeaning Const =
makeBuiltinMeaning
Prelude.const
<costingFunction>
the Haskell type of 'const' gets inferred as
forall a b. a -> b -> a
and the elaboration machinery turns that into
Opaque val Var0 -> Opaque val Var1 -> Opaque val Var0
The elaboration machinery respects the explicitly specified parts of the type and does not attempt
to argue with them. For example if the user insisted that the instantiated type of 'const' had
@Var0@ and @Var1@ swapped:
Opaque val Var1 -> Opaque val Var0 -> Opaque val Var1
the elaboration machinery wouldn't make a fuss about that.
As a final simple example, consider
toBuiltinMeaning Trace =
makeBuiltinMeaning
(\text a -> a <$ emit text)
<costingFunction>
from [How to add a built-in function: simple cases]. The inferred type of the denotation is
forall a. Text -> a -> Emitter a
which elaborates to
Text -> Opaque val Var0 -> Emitter (Opaque val Var0)
Elaboration machinery is able to look under 'Emitter' and 'EvaluationResult' even if there's a type
variable inside that does not appear anywhere else in the type signature, for example the inferred
type of the denotation in
toBuiltinMeaning ErrorPrime =
makeBuiltinMeaning
EvaluationFailure
<costingFunction>
is
forall a. EvaluationResult a
which gets elaborated to
EvaluationResult (Opaque val Var0)
from which the final Plutus type of the builtin is computed:
all a. a
Read Note [How to add a built-in function: complicated cases] next.
-}
{- Note [How to add a built-in function: complicated cases]
Now let's talk about more complicated built-in functions.
1. In Note [Elaboration of polymorphism] we saw how a Haskell type variable gets elaborated to an
@Opaque val VarN@ and we learned that this type can be used directly as opposed to being inferred.
However there exist more ways to use 'Opaque' explicitly. Here's a simple example:
toBuiltinMeaning IdAssumeBool =
makeBuiltinMeaning
(Prelude.id :: Opaque val Bool -> Opaque val Bool)
<costingFunction>
This creates a built-in function whose Plutus type is
id : bool -> bool
i.e. the Plutus type signature of the built-in function is the same as with
toBuiltinMeaning IdBool =
makeBuiltinMeaning
(Prelude.id :: Bool -> Bool)
<costingFunction>
but the two evaluate differently: the former takes a value and returns it right away while the
latter takes a value, extracts a 'Bool' constant out of it and then lifts that constant back into
@val@. The difference is not only in performance (obviously returning something right away is
cheaper than unlifting-then-lifting-back), but also in semantics: the former returns its argument
during evaluation regardless of what that argument is, so if someone generates Untyped Plutus Core
directly, they can apply @IdAssumeBool@ to a term that doesn't evaluate to a 'Bool' constant or
even a constant at all and that won't be a runtime error, while the latter has to be applied to
a term evaluating to a 'Bool' constant in order not to fail at runtime.
2. @val@ in @Opaque val rep@ is not completely arbitrary, it has to implement 'HasConstant', which
makes it possible to unlift @val@ as a constant or lift a constant back into @val@. There's a
'HasConstant' instance for @Opaque val rep@ whenever there's one for @val@, so if we, for some
reason, wanted to have 'Opaque' in the type signature of the denotation, but still unlift the
argument as a 'Bool', we could do that:
toBuiltinMeaning IdAssumeCheckBool =
makeBuiltinMeaning
idAssumeCheckBoolPlc
<costingFunction>
where
idAssumeCheckBoolPlc :: Opaque val Bool -> EvaluationResult Bool
idAssumeCheckBoolPlc val =
case asConstant val of
Right (Some (ValueOf DefaultUniBool b)) -> EvaluationSuccess b
_ -> EvaluationFailure
Here in the denotation we unlift the given value as a constant, check that its type tag is
'DefaultUniBool' and return the unlifted 'Bool'. If any of that fails, we return an explicit
'EvaluationFailure'.
This achieves almost the same as 'IdBool', which keeps all the bookkeeping behind the scenes, but
there is a minor difference: in case of error its message is ignored. It would be easy to allow for
returning an unlifting error from a builtin explicitly, but we don't need that for anything, hence
it's not implemented.
We call this style of manually calling 'asConstant' and matching on the type tag "manual unlifting".
As opposed to "automatic unlifting" that we were using before where 'Bool' in the type of the
denotation of a builtin causes the builtins machinery to convert the given argument to a 'Bool'
constant automatically behind the scenes.
3. There's a middle ground between automatic and manual unlifting to 'Bool', one can unlift a value
automatically as a constant and then unlift the result manually to 'Bool' using the 'SomeConstant'
wrapper:
newtype SomeConstant uni (rep :: GHC.Type) = SomeConstant
{ unSomeConstant :: Some (ValueOf uni)
}
'SomeConstant' is similar to 'Opaque' in that it has a @rep@ representing a Plutus type.
The difference is that 'Opaque' is a wrapper around an arbitrary value and 'SomeConstant' is a
wrapper around a constant. 'SomeConstant' allows one to automatically unlift an argument of a
built-in function as a constant with all 'asConstant' business kept behind the scenes, for example:
toBuiltinMeaning IdSomeConstantBool =
makeBuiltinMeaning
idSomeConstantBoolPlc
<costingFunction>
where
idSomeConstantBoolPlc :: SomeConstant uni Bool -> EvaluationResult Bool
idSomeConstantBoolPlc = \case
SomeConstant (Some (ValueOf DefaultUniBool b)) -> EvaluationSuccess b
_ -> EvaluationFailure
Note how we no longer call 'asConstant' manually, but still manually match on 'DefaultUniBool'.
So there's a whole range of how "low-level" one can choose to be when defining a built-in function.
However it's not always possible to use automatic unlifting, see next.
4. If we try to define the following built-in function:
toBuiltinMeaning NullList =
makeBuiltinMeaning
(null :: [a] -> Bool)
<costingFunction>
we'll get an error, saying that a polymorphic built-in type can't be applied to a type variable.
It's not impossible to make it work, see Note [Unlifting values of built-in types], but not in the
general case, plus it has to be very inefficient.
Instead we have to use 'SomeConstant' to automatically unlift the argument as a constant and then
check that the value inside of it is a list (by matching on the type tag):
toBuiltinMeaning NullList =
makeBuiltinMeaning
nullPlc
<costingFunction>
where
nullPlc :: SomeConstant uni [a] -> EvaluationResult Bool
nullPlc (SomeConstant (Some (ValueOf uniListA xs))) = do
DefaultUniList _ <- pure uniListA
pure $ null xs
('EvaluationResult' has a 'MonadFail' instance allowing us to use the @<pat> <- pure <expr>@ idiom)
As before, we have to match on the type tag, because there's no relation between @rep@ from
@SomeConstant uni rep@ and the constant that the built-in function actually receives at runtime
(someone could generate Untyped Plutus Core directly and apply 'nullPlc' to an 'Integer' or
whatever). @rep@ is only for the Plutus type checker to look at, it doesn't influence evaluation
in any way.
Here's a similar built-in function:
toBuiltinMeaning FstPair =
makeBuiltinMeaning
fstPlc
<costingFunction>
where
fstPlc :: SomeConstant uni (a, b) -> EvaluationResult (Opaque val a)
fstPlc (SomeConstant (Some (ValueOf uniPairAB xy))) = do
DefaultUniPair uniA _ <- pure uniPairAB -- [1]
pure . fromValueOf uniA $ fst xy -- [2]
In this definition we extract the first element of a pair by checking that the given constant is
indeed a pair [1] and lifting its first element into @val@ using the type tag for the first
element [2] (extracted from the type tag for the whole pair constant [1]).
Note that it's fine to mix automatic unlifting for polymorphism not related to built-in types and
manual unlifting for arguments having non-monomorphized polymorphic built-in types, for example:
toBuiltinMeaning ChooseList =
makeBuiltinMeaning
choosePlc
<costingFunction>
where
choosePlc :: SomeConstant uni [a] -> b -> b -> EvaluationResult b
choosePlc (SomeConstant (Some (ValueOf uniListA xs))) a b = do
DefaultUniList _ <- pure uniListA
pure $ case xs of
[] -> a
_ : _ -> b
Here @a@ appears inside @[]@, which is a polymorphic built-in type, and so we have to use
'SomeConstant' and check that the given constant is indeed a list, while @b@ doesn't appear inside
of any built-in type and so we don't need to instantiate it to 'Opaque' manually, the elaboration
machinery will do it for us.
Our final example is this:
toBuiltinMeaning MkCons =
makeBuiltinMeaning
consPlc
<costingFunction>
where
consPlc
:: SomeConstant uni a -> SomeConstant uni [a] -> EvaluationResult (Opaque val [a])
consPlc
(SomeConstant (Some (ValueOf uniA x)))
(SomeConstant (Some (ValueOf uniListA xs))) = do
DefaultUniList uniA' <- pure uniListA -- [1]
Just Refl <- pure $ uniA `geq` uniA' -- [2]
pure . fromValueOf uniListA $ x : xs -- [3]
Here we prepend an element to a list [3] after checking that the second argument is indeed a
list [1] and that the type tag of the element being prepended equals the type tag for elements of
the list [2] (extracted from the type tag for the whole list constant [1]).
-}
{- Note [Builtins and Plutus type checking]
There's a direct correspondence between the Haskell type of the denotation of a builtin and the
Plutus type of the builtin:
1. elaboration turns a Haskell type variable into a concrete Haskell type representing a Plutus type
variable, which later becomes demoted (in the regular @singletons@ sense via 'KnownSymbol' etc)
to a regular Haskell value representing a Plutus type variable (as a part of the AST)
2. a builtin head (i.e. a completely uninstantiated built-in type such as @Bool@ and @[]@) is
considered abstract by the Plutus type checker. All the type checker cares about is being able to
get the (Plutus) kind of a builtin head and check two builtin heads for equality
3. Plutus type normalization tears partially or fully instantiated built-in types (such as
@[Integer]@) apart and creates a Plutus type application for each Haskell type application
4. 'Emitter' and 'EvaluationResult' do not appear on the Plutus side, since the logging and failure
effects are implicit in Plutus as was discussed above
5. 'Opaque' and 'SomeConstant' both carry a Haskell @rep@ type argument representing some Plutus
type to be used for Plutus type checking
This last part means that one can attach any (legal) @rep@ to an 'Opaque' or 'SomeConstant' and
it'll be used by the Plutus type checker completely regardless of what the built-in function
actually does. Let's look at some examples.
1. The following built-in function unlifts to 'Bool' and lifts the result back:
toBuiltinMeaning IdIntegerAsBool =
makeBuiltinMeaning
idIntegerAsBool
<costingFunction>
where
idIntegerAsBool :: SomeConstant uni Integer -> EvaluationResult (SomeConstant uni Integer)
idIntegerAsBool = \case
con@(SomeConstant (Some (ValueOf DefaultUniBool _))) -> EvaluationSuccess con
_ -> EvaluationFailure
but on the Plutus side its type is
integer -> integer
because the @rep@ that 'SomeConstant' carries is 'Integer' in both the cases (in the type of the
argument, as well as in the type of the result).
This means that for this built-in function the Plutus type checker will accept a program that fails
at runtime due to a type mismatch and will reject a program that runs successfully. Other built-in
functions also can fail, e.g. the type of @ifThenElse@ says that the builtin expects a @Bool@ and
feeding it something else will result in evaluation failure, but 'idIntegerAsBool' is different:
it's respecting its type signature is what causes a failure, not disrespecting it.
2. Another example of an unsafe built-in function is this one that checks whether an argument is a
constant or not:
toBuiltinMeaning IsConstant =
makeBuiltinMeaning
isConstantPlc
<costingFunction>
where
-- The type signature is just for clarity, it's not required.
isConstantPlc :: Opaque val a -> Bool
isConstantPlc = isRight . asConstant
Its type on the Plutus side is
all a. a -> bool
By parametricity any inhabitant of this type has to be either bottom or a function ignoring its
argument, but @IsConstant@ actually uses the argument and so we break parametricity with this
built-in function.
3. Finally, we can have a Plutus version of @unsafeCoerce@:
toBuiltinMeaning UnsafeCoerce =
makeBuiltinMeaning
unsafeCoercePlc
<costingFunction>
where
-- The type signature is just for clarity, it's not required.
unsafeCoercePlc :: Opaque val a -> Opaque val b
unsafeCoercePlc = Opaque . unOpaque
Its type on the Plutus side is
all a b. a -> b
and thus this built-in function allows for viewing any Plutus expression as having an arbitrary
type. Which is of course not nearly as bad as @unsafeCoerce@ in Haskell, because in Plutus a
blob of memory representing an @Integer@ is not going to be viewed as a @[Bool]@ and an attempt to
actually extract that @[Bool]@ will result in evaluation failure, but this built-in function is
still not a good citizen of the Plutus type system.
One could of course simply wrap Haskell's @unsafeCoerce@ as a built-in function in Plutus, but it
goes without saying that this is not supposed to be done.
So overall one needs to be very careful when defining built-in functions that have explicit
'Opaque' and 'SomeConstant' arguments. Expressiveness doesn't come for free.
Read Note [Pattern matching on built-in types] next.
-}
{- Note [Pattern matching on built-in types]
At the moment we really only support direct pattern matching on enumeration types: 'Void', 'Unit',
'Bool' etc. This is because the denotation of a builtin cannot construct general terms (as opposed
to constants), only juggle the ones that were provided as arguments without changing them.
So e.g. if we wanted to add the following data type:
newtype AnInt = AnInt Int
as a built-in type, we wouldn't be able to add the following function as its pattern matcher:
matchAnInt :: AnInt -> (Int -> r) -> r
matchAnInt (AnInt i) f = f i
because currently we cannot express the @f i@ part using the builtins machinery as that would
require applying an arbitrary Plutus Core function in the denotation of a builtin, which would
allow us to return arbitrary terms from the builtin application machinery, which is something
that we originally had, but decided to abandon due to performance concerns.
But it's still possible to have @AnInt@ as a built-in type, it's just that instead of trying to
make its pattern matcher into a builtin we can have the following builtin:
anIntToInt :: AnInt -> Int
anIntToInt (AnInt i) = i
which fits perfectly well into the builtins machinery.
Although that becomes annoying for more complex data types. For tuples we need to provide two
projection functions ('fst' and 'snd') instead of a single pattern matcher, which is not too bad,
but to get pattern matching on lists we need a more complicated setup. For example we can have three
built-in functions: @null@, @head@ and @tail@, plus require `Bool` to be in the universe, so that we
can define an equivalent of
matchList :: [a] -> r -> (a -> [a] -> r) -> r
matchList xs z f = if null xs then z else f (head xs) (tail xs)
If a constructor stores more than one value, the corresponding projection function packs them
into a (possibly nested) pair, for example for
data Data
= Constr Integer [Data]
| <...>
we have (pseudocode):
unConstrData (Constr i ds) = (i, ds)
In order to get pattern matching over 'Data' we need a projection function per constructor as well
as with lists, but writing (where the @Data@ suffix indicates that a function is a builtin that
somehow corresponds to a constructor of 'Data')
if isConstrData d
then uncurry fConstr $ unConstrData d
else if isMapData d
then fMap $ unMapData d
else if isListData d
then fList $ unListData d
else <...>
is tedious and inefficient and so instead we have a single @chooseData@ builtin that matches on
its @Data@ argument and chooses the appropriate branch (type instantiations and strictness concerns
are omitted for clarity):
chooseData
(uncurry fConstr $ unConstrData d)
(fMap $ unMapData d)
(fList $ unListData d)
<...>
d
which, for example, evaluates to @fMap es@ when @d@ is @Map es@
We decided to handle lists the same way by using @chooseList@ rather than @null@ for consistency.
On the bright side, this encoding of pattern matchers does work, so maybe it's indeed worth to
prioritize performance over convenience, especially given the fact that performance is of a concern
to every single end user while the inconvenience is only a concern for the compiler writers and
we don't add complex built-in types too often.
It is not however clear if we can't get more performance gains by defining matchers directly as
higher-order built-in functions compared to forbidding them. Particularly since if higher-order
built-in functions were allowed, we could define not only matches, but also folds and keep recursion
on the Haskell side for conversions from 'Data', which can potentially have a huge positive impact
on performance.
Read Note [Representable built-in functions over polymorphic built-in types] next.
-}
{- Note [Representable built-in functions over polymorphic built-in types]
In Note [Pattern matching on built-in types] we discussed how general higher-order polymorphic
built-in functions are troubling, but polymorphic built-in functions can be troubling even in
the first-order case. In a Plutus program we always pair constants of built-in types with their
tags from the universe, which means that in order to produce a constant embedded into a program
we need the tag of the type of that constant. We can't get that tag from a Plutus type -- those
are gone at runtime, so the only place we can get a type tag from during evaluation is some already
existing constant. I.e. the following built-in function is representable:
tail : all a. [a] -> [a]
because for constructing the result we need a type tag for @[a]@, but we have a value of that type
as an argument and so we can extract the type tag from it. Same applies to
swap : all a b. (a, b) -> (b, a)
since 'SomeConstantOf' always contains a type tag for each type that a polymorphic built-in type is
instantiated with and so constructing a type tag for @(b, a)@ given type tags for @a@ and @b@ is
unproblematic.
And so neither
cons : all a. a -> [a] -> [a]
is troubling (even though that ones requires checking at runtime that the element to be prepended
is of the same type as the type of the elements of the list as it's impossible to enforce this kind
of type safety in Haskell over possibly untyped PLC).
However consider the following imaginary builtin:
nil : all a. [a]
we can't represent it for two reasons:
1. we don't have any argument providing us a type tag for @a@ and hence we can't construct a type
tag for @[a]@
2. it would be a very unsound builtin to have. We can only instantiate built-in types with other
built-in types and so allowing @nil {some_non_built_in_type}@ would be a lie that couldn't reduce
to anything since it's not even possible to represent a built-in list with non-built-in elements
(even if there's zero of them)
"Wait, but wouldn't @cons {some_non_built_in_type}@ be a lie as well?" -- No! Since @cons@ does not
just construct a list filled with elements of a non-built-in type but also expects one as an
argument and providing such an argument is impossible, 'cause it's pretty much the same thing as
populating 'Void' -- both values are equally unrepresentable. And so @cons {some_non_built_in_type}@
is a way to say @absurd@, which is perfectly fine to have.
Finally,
comma :: all a b. a -> b -> (a, b)
is representable (because we can require arguments to be constants carrying universes with them,
which we can use to construct the resulting universe), but is still a lie, because instantiating
that builtin with non-built-in types is possible and so the PLC type checker won't throw on such
an instantiation, which will become 'EvalutionFailure' at runtime the moment unlifting of a
non-constant is attempted when a constant is expected.
So could we still get @nil@ or a safe version of @comma@ somehow? Well, we could have this
weirdness:
nilOfTypeOf : all a. [a] -> [a]
i.e. ask for an already existing list, but ignore the actual list and only use the type tag.
But since we're ignoring the actual list, can't we just not pass it in the first place? And instead
pass around our good old friends, singletons. We should be able to do that, but it hasn't been
investigated. Perhaps something along the lines of adding the following constructor to 'DefaultUni':
DefaultUniProtoSing :: DefaultUni (Esc (Proxy @GHC.Type))
and then defining