Skip to content

Commit

Permalink
Move internal parameter names from function interface to function hea…
Browse files Browse the repository at this point in the history
…d, and generics names entirely. Soon, we'll add interning, and for that the interfaces must be repeatable / comparable and thus minimal.
  • Loading branch information
Ivorforce committed Jul 16, 2024
1 parent 130bee0 commit 5fb93b4
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 105 deletions.
15 changes: 10 additions & 5 deletions src/interpreter/builtins/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ pub fn create_functions(runtime: &mut Runtime, module: &mut Module) {
add_function(&number_functions.negative, primitive_type, PrimitiveOperation::Negative, module, runtime);

let _parse_int_literal = FunctionHead::new_static(
FunctionInterface::new_operator(1, &TypeProto::unit_struct(&traits.String), &type_),
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_global_function("parse_int_literal"),
FunctionInterface::new_operator(1, &TypeProto::unit_struct(&traits.String), &type_),
);
add_function(&_parse_int_literal, primitive_type, PrimitiveOperation::ParseIntString, module, runtime);
module.trait_conformance.add_conformance_rule(TraitConformanceRule::manual(
Expand Down Expand Up @@ -164,8 +165,9 @@ pub fn create_functions(runtime: &mut Runtime, module: &mut Module) {
add_function(&real_functions.log, primitive_type, PrimitiveOperation::Log, module, runtime);

let _parse_real_literal = FunctionHead::new_static(
FunctionInterface::new_operator(1, &TypeProto::unit(TypeUnit::Struct(Rc::clone(&traits.String))), &type_),
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_global_function("parse_real_literal"),
FunctionInterface::new_operator(1, &TypeProto::unit(TypeUnit::Struct(Rc::clone(&traits.String))), &type_),
);
add_function(&_parse_real_literal, primitive_type, PrimitiveOperation::ParseRealString, module, runtime);
module.trait_conformance.add_conformance_rule(TraitConformanceRule::manual(
Expand All @@ -185,20 +187,23 @@ pub fn create_functions(runtime: &mut Runtime, module: &mut Module) {
}

let and_op = FunctionHead::new_static(
FunctionInterface::new_operator(2, &bool_type, &bool_type),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("and_f"),
FunctionInterface::new_operator(2, &bool_type, &bool_type),
);
add_function(&and_op, primitives::Type::Bool, PrimitiveOperation::And, module, runtime);

let or__op = FunctionHead::new_static(
FunctionInterface::new_operator(2, &bool_type, &bool_type),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("or_f"),
FunctionInterface::new_operator(2, &bool_type, &bool_type),
);
add_function(&or__op, primitives::Type::Bool, PrimitiveOperation::Or, module, runtime);

let not_op = FunctionHead::new_static(
FunctionInterface::new_operator(1, &bool_type, &bool_type),
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_global_function("not_f"),
FunctionInterface::new_operator(1, &bool_type, &bool_type),
);
add_function(&not_op, primitives::Type::Bool, PrimitiveOperation::Not, module, runtime);
}
54 changes: 36 additions & 18 deletions src/interpreter/builtins/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ pub struct AnyFunctions {
pub fn make_any_functions(type_: &Rc<TypeProto>) -> AnyFunctions {
AnyFunctions {
clone: FunctionHead::new_static(
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_member_function("clone"),
FunctionInterface::new_member(
type_.clone(),
[].into_iter(),
type_.clone()
),
FunctionRepresentation::new_member_function("clone")
),
}
}
Expand All @@ -79,12 +80,14 @@ pub struct EqFunctions {
pub fn make_eq_functions(type_: &Rc<TypeProto>, bool_type: &Rc<TypeProto>) -> EqFunctions {
EqFunctions {
equal_to: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, bool_type),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("is_equal"),
FunctionInterface::new_operator(2, type_, bool_type),
),
not_equal_to: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, bool_type),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("is_not_equal"),
FunctionInterface::new_operator(2, type_, bool_type),
),
}
}
Expand All @@ -100,20 +103,24 @@ pub struct OrdFunctions {
pub fn make_ord_functions(type_: &Rc<TypeProto>, bool_type: &Rc<TypeProto>) -> OrdFunctions {
OrdFunctions {
greater_than: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, bool_type),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("is_greater"),
FunctionInterface::new_operator(2, type_, bool_type),
),
greater_than_or_equal_to: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, bool_type),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("is_greater_or_equal"),
FunctionInterface::new_operator(2, type_, bool_type),
),
lesser_than: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, bool_type),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("is_lesser"),
FunctionInterface::new_operator(2, type_, bool_type),
),
lesser_than_or_equal_to: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, bool_type),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("is_lesser_or_equal"),
FunctionInterface::new_operator(2, type_, bool_type),
),
}
}
Expand All @@ -136,30 +143,36 @@ pub struct NumberFunctions {
pub fn make_number_functions(type_: &Rc<TypeProto>) -> NumberFunctions {
NumberFunctions {
add: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, type_),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("add"),
FunctionInterface::new_operator(2, type_, type_),
),
subtract: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, type_),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("subtract"),
FunctionInterface::new_operator(2, type_, type_),
),
multiply: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, type_),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("multiply"),
FunctionInterface::new_operator(2, type_, type_),
),
divide: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, type_),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("divide"),
FunctionInterface::new_operator(2, type_, type_),
),

negative: FunctionHead::new_static(
FunctionInterface::new_operator(1, type_, type_),
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_global_function("negative"),
FunctionInterface::new_operator(1, type_, type_),
),

modulo: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, type_),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("modulo"),
FunctionInterface::new_operator(2, type_, type_),
),
}
}
Expand All @@ -173,25 +186,28 @@ pub struct RealFunctions {
pub fn make_real_functions(type_: &Rc<TypeProto>) -> RealFunctions {
RealFunctions {
pow: FunctionHead::new_static(
FunctionInterface::new_operator(2, type_, type_),
FunctionHead::dummy_param_names(2),
FunctionRepresentation::new_global_function("pow"),
FunctionInterface::new_operator(2, type_, type_),
),
log: FunctionHead::new_static(
FunctionInterface::new_operator(1, type_, type_),
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_global_function("log"),
FunctionInterface::new_operator(1, type_, type_),
),
}
}

#[allow(non_snake_case)]
pub fn make_to_string_function(type_: &Trait, String: &Rc<Trait>) -> Rc<FunctionHead> {
FunctionHead::new_static(
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_member_function("to_string"),
FunctionInterface::new_member(
type_.create_generic_type("Self"),
[].into_iter(),
TypeProto::unit_struct(&String)
),
FunctionRepresentation::new_member_function("to_string"),
)
}

Expand Down Expand Up @@ -269,11 +285,12 @@ pub fn create(runtime: &mut Runtime, module: &mut Module) -> Traits {

let mut ConstructableByIntLiteral = Trait::new_with_self("ConstructableByIntLiteral");
let parse_int_literal_function = FunctionHead::new_static(
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_global_function("parse_int_literal"),
FunctionInterface::new_simple(
[TypeProto::unit_struct(&String)].into_iter(),
ConstructableByIntLiteral.create_generic_type("Self"),
),
FunctionRepresentation::new_global_function("parse_int_literal"),
);
insert_functions(&mut ConstructableByIntLiteral, [
&parse_int_literal_function
Expand All @@ -285,11 +302,12 @@ pub fn create(runtime: &mut Runtime, module: &mut Module) -> Traits {

let mut ConstructableByRealLiteral = Trait::new_with_self("ConstructableByRealLiteral");
let parse_real_literal_function = FunctionHead::new_static(
FunctionHead::dummy_param_names(1),
FunctionRepresentation::new_global_function("parse_real_literal"),
FunctionInterface::new_simple(
[TypeProto::unit_struct(&String)].into_iter(),
ConstructableByRealLiteral.create_generic_type("Self")
),
FunctionRepresentation::new_global_function("parse_real_literal"),
);
insert_functions(&mut ConstructableByRealLiteral, [
&parse_real_literal_function
Expand Down
4 changes: 0 additions & 4 deletions src/program/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ impl<'a> Display for MockFunctionInterface<'a> {
let signature = FunctionInterface {
parameters: self.argument_keys.iter().zip(&self.arguments).map(|(key, expression_id)| Parameter {
external_key: (*key).clone(),
internal_name: match key {
ParameterKey::Positional => "_".to_string(),
ParameterKey::Name(n) => n.clone(),
},
type_: self.types.prototype_binding_alias(expression_id),
}).collect_vec(),
return_type: TypeProto::unit(TypeUnit::Generic(Uuid::new_v4())),
Expand Down
26 changes: 21 additions & 5 deletions src/program/functions/head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::hash::{Hash, Hasher};
use std::rc::Rc;

use display_with_options::with_options;
use itertools::Itertools;
use uuid::Uuid;

use crate::program::functions::{FunctionInterface, FunctionRepresentation};
use crate::program::functions::{FunctionInterface, FunctionRepresentation, Parameter, ParameterKey};
use crate::program::traits::TraitBinding;

#[derive(Clone, PartialEq, Eq)]
Expand All @@ -24,19 +25,23 @@ pub struct FunctionHead {
pub function_type: FunctionType,
pub interface: Rc<FunctionInterface>,
pub declared_representation: FunctionRepresentation,
pub declared_internal_parameter_names: Vec<String>,
}

impl FunctionHead {
pub fn new_static(interface: Rc<FunctionInterface>, declared_representation: FunctionRepresentation) -> Rc<FunctionHead> {
Self::new(interface, FunctionType::Static, declared_representation)
pub fn new_static(declared_internal_parameter_names: Vec<String>, declared_representation: FunctionRepresentation, interface: Rc<FunctionInterface>) -> Rc<FunctionHead> {
Self::new(declared_internal_parameter_names, declared_representation, interface, FunctionType::Static)
}

pub fn new(interface: Rc<FunctionInterface>, function_type: FunctionType, declared_representation: FunctionRepresentation) -> Rc<FunctionHead> {
pub fn new(declared_internal_parameter_names: Vec<String>, declared_representation: FunctionRepresentation, interface: Rc<FunctionInterface>, function_type: FunctionType) -> Rc<FunctionHead> {
assert_eq!(declared_internal_parameter_names.len(), interface.parameters.len());

Rc::new(FunctionHead {
function_id: Uuid::new_v4(),
interface,
function_type,
declared_representation
declared_representation,
declared_internal_parameter_names,
})
}

Expand All @@ -46,6 +51,17 @@ impl FunctionHead {
FunctionType::Polymorphic { .. } => panic!("Cannot unwrap polymorphic implementation ID"),
}
}

pub fn dummy_param_names(count: usize) -> Vec<String> {
(0..count).map(|p| format!("p{}", count)).collect_vec()
}

pub fn infer_param_names(params: &Vec<Parameter>) -> Vec<String> {
params.iter().enumerate().map(|(idx, p)| match &p.external_key {
ParameterKey::Positional => format!("p{}", idx),
ParameterKey::Name(n) => n.clone(),
}).collect_vec()
}
}

impl PartialEq for FunctionHead {
Expand Down
19 changes: 4 additions & 15 deletions src/program/functions/interface.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::fmt::{Debug, Display, Formatter};
use std::rc::Rc;

Expand All @@ -21,7 +21,6 @@ pub enum ParameterKey {
#[derive(Clone, PartialEq, Eq)]
pub struct Parameter {
pub external_key: ParameterKey,
pub internal_name: String,
pub type_: Rc<TypeProto>,
}

Expand All @@ -37,7 +36,7 @@ pub struct FunctionInterface {
pub requirements: HashSet<Rc<TraitBinding>>,
/// All internally used generics. These are not guaranteed to not exist elsewhere,
/// but for the purposes of this interface, they are to be regarded as generics.
pub generics: HashMap<String, Rc<Trait>>,
pub generics: HashSet<Rc<Trait>>,
}

impl FunctionInterface {
Expand All @@ -54,7 +53,6 @@ impl FunctionInterface {
Rc::new(FunctionInterface {
parameters: vec![Parameter {
external_key: ParameterKey::Positional,
internal_name: "arg".to_string(),
type_: parameter_type.clone(),
}],
return_type: TypeProto::void(),
Expand All @@ -67,7 +65,6 @@ impl FunctionInterface {
let parameters: Vec<Parameter> = (0..count)
.map(|x| { Parameter {
external_key: ParameterKey::Positional,
internal_name: format!("p{}", x),
type_: parameter_type.clone(),
}
}).collect();
Expand All @@ -85,7 +82,6 @@ impl FunctionInterface {
.enumerate()
.map(|(i, x)| Parameter {
external_key: ParameterKey::Positional,
internal_name: format!("p{}", i),
type_: x.clone(),
})
.collect();
Expand All @@ -101,13 +97,11 @@ impl FunctionInterface {
pub fn new_member<'a, I>(self_type: Rc<TypeProto>, parameter_types: I, return_type: Rc<TypeProto>) -> Rc<FunctionInterface> where I: Iterator<Item=Rc<TypeProto>> {
let parameters: Vec<Parameter> = [Parameter {
external_key: ParameterKey::Positional,
internal_name: "self".to_string(),
type_: self_type,
}].into_iter().chain(parameter_types
.enumerate()
.map(|(i, x)| Parameter {
external_key: ParameterKey::Positional,
internal_name: format!("p{}", i),
type_: x.clone(),
}))
.collect();
Expand All @@ -125,7 +119,6 @@ impl Parameter {
pub fn mapping_type(&self, map: &dyn Fn(&Rc<TypeProto>) -> Rc<TypeProto>) -> Parameter {
Parameter {
external_key: self.external_key.clone(),
internal_name: self.internal_name.clone(),
type_: map(&self.type_),
}
}
Expand All @@ -144,14 +137,10 @@ impl Debug for Parameter {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.external_key {
ParameterKey::Positional => {
write!(f, "{} '{:?}", self.internal_name, self.type_)
write!(f, "_ '{:?}", self.type_)
}
ParameterKey::Name(n) => {
if n != &self.internal_name {
write!(f, "{}: {} '{:?}", n, self.internal_name, self.type_)
} else {
write!(f, "{}: '{:?}", n, self.type_)
}
write!(f, "{}: '{:?}", n, self.type_)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/program/traits/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ impl TraitGraph {

for abstract_fun in requirement.trait_.abstract_functions.iter() {
let mapped_head = FunctionHead::new(
abstract_fun.declared_internal_parameter_names.clone(),
abstract_fun.declared_representation.clone(),
Rc::new(FunctionInterface {
parameters: abstract_fun.interface.parameters.iter().map(|x| {
x.mapping_type(&|type_| type_.replacing_structs(&requirement.generic_to_type))
Expand All @@ -280,7 +282,6 @@ impl TraitGraph {
assumed_requirement: Rc::clone(&requirement),
abstract_function: Rc::clone(abstract_fun)
},
abstract_fun.declared_representation.clone(),
);
binding_resolution.insert(
Rc::clone(&abstract_fun),
Expand Down
Loading

0 comments on commit 5fb93b4

Please sign in to comment.